Skip to content

Commit d2ae7b7

Browse files
author
Lupacescu Eduard
authored
Interceptors and conditional displaying
Interceptors and conditional displaying
2 parents b171e82 + 1088ed9 commit d2ae7b7

20 files changed

+520
-71
lines changed

src/Fields/BaseField.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Binaryk\LaravelRestify\Fields;
44

5+
use Illuminate\Http\Request;
6+
57
/**
68
* @author Eduard Lupacescu <[email protected]>
79
*/
@@ -31,10 +33,11 @@ public function when($condition, $default = false)
3133
/**
3234
* Conditionally load the field.
3335
*
36+
* @param Request $request
3437
* @return bool|callable|mixed
3538
*/
36-
public function filter()
39+
public function filter(Request $request)
3740
{
38-
return is_callable($this->when) ? call_user_func($this->when) : $this->when;
41+
return is_callable($this->when) ? call_user_func($this->when, $request) : $this->when;
3942
}
4043
}

src/Fields/Field.php

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class Field extends OrganicField implements JsonSerializable
2424
*/
2525
public $storeCallback;
2626

27+
/**
28+
* @var Closure
29+
*/
30+
public $showCallback;
31+
2732
/**
2833
* Callback called when trying to fill this attribute, this callback will override the fill action, so make
2934
* sure you assign the attribute to the model over this callback.
@@ -35,7 +40,7 @@ class Field extends OrganicField implements JsonSerializable
3540
/**
3641
* Create a new field.
3742
*
38-
* @param string|callable|null $attribute
43+
* @param string|callable|null $attribute
3944
*/
4045
public function __construct($attribute)
4146
{
@@ -45,7 +50,7 @@ public function __construct($attribute)
4550
/**
4651
* Create a new element.
4752
*
48-
* @param array $arguments
53+
* @param array $arguments
4954
* @return static
5055
*/
5156
public static function make(...$arguments)
@@ -58,14 +63,16 @@ public static function make(...$arguments)
5863
*/
5964
public function jsonSerialize()
6065
{
61-
return [];
66+
return [
67+
'value' => $this->value,
68+
];
6269
}
6370

6471
/**
6572
* Callback called when the value is filled, this callback will do not override the fill action. If fillCallback is defined
6673
* this will do not be called.
6774
*
68-
* @param Closure $callback
75+
* @param Closure $callback
6976
* @return Field
7077
*/
7178
public function storeCallback(Closure $callback)
@@ -75,11 +82,22 @@ public function storeCallback(Closure $callback)
7582
return $this;
7683
}
7784

85+
/**
86+
* @param Closure $callback
87+
* @return $this
88+
*/
89+
public function showCallback(Closure $callback)
90+
{
91+
$this->showCallback = $callback;
92+
93+
return $this;
94+
}
95+
7896
/**
7997
* Callback called when trying to fill this attribute, this callback will override the fill action, so make
8098
* sure you assign the attribute to the model over this callback.
8199
*
82-
* @param Closure $callback
100+
* @param Closure $callback
83101
* @return $this
84102
*/
85103
public function fillCallback(Closure $callback)
@@ -92,7 +110,7 @@ public function fillCallback(Closure $callback)
92110
/**
93111
* Fill attribute with value from the request or delegate this action to the user defined callback.
94112
*
95-
* @param RestifyRequest $request
113+
* @param RestifyRequest $request
96114
* @param $model
97115
* @return mixed|void
98116
*/
@@ -112,7 +130,7 @@ public function fillAttribute(RestifyRequest $request, $model)
112130
/**
113131
* Fill the model with value from the request.
114132
*
115-
* @param RestifyRequest $request
133+
* @param RestifyRequest $request
116134
* @param $model
117135
* @param $attribute
118136
*/
@@ -135,7 +153,7 @@ public function getAttribute()
135153

136154
/**
137155
* Validation rules for store.
138-
* @param callable|array|string $rules
156+
* @param callable|array|string $rules
139157
* @return Field
140158
*/
141159
public function storingRules($rules)
@@ -148,7 +166,7 @@ public function storingRules($rules)
148166
/**
149167
* Validation rules for update.
150168
*
151-
* @param callable|array|string $rules
169+
* @param callable|array|string $rules
152170
* @return Field
153171
*/
154172
public function updatingRules($rules)
@@ -160,7 +178,7 @@ public function updatingRules($rules)
160178

161179
/**
162180
* Validation rules for store.
163-
* @param callable|array|string $rules
181+
* @param callable|array|string $rules
164182
* @return Field
165183
*/
166184
public function rules($rules)
@@ -173,7 +191,7 @@ public function rules($rules)
173191
/**
174192
* Validation messages.
175193
*
176-
* @param array $messages
194+
* @param array $messages
177195
* @return Field
178196
*/
179197
public function messages(array $messages)
@@ -200,4 +218,35 @@ public function getUpdatingRules()
200218
{
201219
return array_merge($this->rules, $this->updatingRules);
202220
}
221+
222+
/**
223+
* Resolve the field's value for display.
224+
*
225+
* @param mixed $repository
226+
* @param string|null $attribute
227+
* @return callable|string
228+
*/
229+
public function resolveForShow($repository, $attribute = null)
230+
{
231+
$attribute = $attribute ?? $this->attribute;
232+
233+
if (is_callable($this->showCallback)) {
234+
$value = $this->resolveAttribute($repository, $attribute);
235+
$attribute = call_user_func($this->showCallback, $value, $repository, $attribute);
236+
}
237+
238+
return $attribute;
239+
}
240+
241+
/**
242+
* Resolve the given attribute from the given resource.
243+
*
244+
* @param mixed $repository
245+
* @param string $attribute
246+
* @return mixed
247+
*/
248+
protected function resolveAttribute($repository, $attribute)
249+
{
250+
return data_get($repository, str_replace('->', '.', $attribute));
251+
}
203252
}

src/Fields/OrganicField.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Binaryk\LaravelRestify\Fields;
44

5+
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
6+
57
/**
68
* @author Eduard Lupacescu <[email protected]>
79
*/
@@ -31,4 +33,124 @@ abstract class OrganicField extends BaseField
3133
* @var array
3234
*/
3335
public $messages = [];
36+
37+
/**
38+
* Indicates if the element should be shown on the index view.
39+
*
40+
* @var \Closure|bool
41+
*/
42+
public $showOnIndex = true;
43+
44+
/**
45+
* Indicates if the element should be shown on the detail view.
46+
*
47+
* @var \Closure|bool
48+
*/
49+
public $showOnDetail = true;
50+
51+
/**
52+
* Specify that the element should be hidden from the detail view.
53+
*
54+
* @param \Closure|bool $callback
55+
* @return $this
56+
*/
57+
public function showOnDetail($callback = true)
58+
{
59+
$this->showOnDetail = $callback;
60+
61+
return $this;
62+
}
63+
64+
/**
65+
* Specify that the element should be hidden from the detail view.
66+
*
67+
* @param \Closure|bool $callback
68+
* @return $this
69+
*/
70+
public function showOnIndex($callback = true)
71+
{
72+
$this->showOnIndex = $callback;
73+
74+
return $this;
75+
}
76+
77+
/**
78+
* Specify that the element should be hidden from the detail view.
79+
*
80+
* @param \Closure|bool $callback
81+
* @return $this
82+
*/
83+
public function hideFromDetail($callback = true)
84+
{
85+
$this->showOnDetail = is_callable($callback) ? function () use ($callback) {
86+
return ! call_user_func_array($callback, func_get_args());
87+
}
88+
: ! $callback;
89+
90+
return $this;
91+
}
92+
93+
/**
94+
* Specify that the element should be hidden from the index view.
95+
*
96+
* @param \Closure|bool $callback
97+
* @return $this
98+
*/
99+
public function hideFromIndex($callback = true)
100+
{
101+
$this->showOnIndex = is_callable($callback) ? function () use ($callback) {
102+
return ! call_user_func_array($callback, func_get_args());
103+
}
104+
: ! $callback;
105+
106+
return $this;
107+
}
108+
109+
/**
110+
* Check showing on detail.
111+
*
112+
* @param RestifyRequest $request
113+
* @param mixed $repository
114+
* @return bool
115+
*/
116+
public function isShownOnDetail(RestifyRequest $request, $repository): bool
117+
{
118+
if (is_callable($this->showOnDetail)) {
119+
$this->showOnDetail = call_user_func($this->showOnDetail, $request, $repository);
120+
}
121+
122+
return $this->showOnDetail;
123+
}
124+
125+
/**
126+
* Check hidden on detail.
127+
*
128+
* @param RestifyRequest $request
129+
* @param mixed $repository
130+
* @return bool
131+
*/
132+
public function isHiddenOnDetail(RestifyRequest $request, $repository): bool
133+
{
134+
if (is_callable($this->showOnDetail)) {
135+
$this->showOnDetail = call_user_func($this->showOnDetail, $request, $repository);
136+
}
137+
138+
return ! $this->showOnDetail;
139+
}
140+
141+
/**
142+
* Check hidden on index.
143+
*
144+
* @param RestifyRequest $request
145+
* @param mixed $repository
146+
* @return bool
147+
*/
148+
public function isHiddenOnIndex(RestifyRequest $request, $repository): bool
149+
{
150+
if (is_callable($this->showOnIndex)) {
151+
$this->showOnIndex = call_user_func($this->showOnIndex, $request, $repository);
152+
}
153+
154+
return ! $this->showOnIndex;
155+
}
34156
}

src/Http/Requests/RestifyRequest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Binaryk\LaravelRestify\Http\Requests;
44

5+
use Binaryk\LaravelRestify\Restify;
56
use Illuminate\Foundation\Http\FormRequest;
67
use Illuminate\Support\Facades\App;
78

@@ -27,4 +28,26 @@ public function isDev()
2728
{
2829
return false === $this->isProduction();
2930
}
31+
32+
/**
33+
* Determine if the request is on repository index e.g. restify-api/users.
34+
*
35+
* @return bool
36+
*/
37+
public function isIndexRequest()
38+
{
39+
$path = trim(Restify::path($this->route('repository')), '/') ?: '/';
40+
41+
return $this->is($path);
42+
}
43+
44+
/**
45+
* Determine if the request is on repository detail e.g. restify-api/users/1
46+
* This will match any verbs (PATCH, DELETE or GET).
47+
* @return bool
48+
*/
49+
public function isDetailRequest()
50+
{
51+
return ! $this->isIndexRequest();
52+
}
3053
}

0 commit comments

Comments
 (0)