2
2
3
3
namespace Binaryk \LaravelRestify \Fields ;
4
4
5
+ use Binaryk \LaravelRestify \Fields \Concerns \HasAction ;
5
6
use Binaryk \LaravelRestify \Http \Requests \RestifyRequest ;
6
7
use Binaryk \LaravelRestify \Repositories \Repository ;
7
8
use Binaryk \LaravelRestify \Traits \Make ;
8
9
use Closure ;
9
10
use Illuminate \Contracts \Validation \Rule ;
11
+ use Illuminate \Database \Eloquent \Model ;
10
12
use Illuminate \Support \Str ;
11
13
use Illuminate \Validation \Rules \Unique ;
12
14
use JsonSerializable ;
13
15
14
16
class Field extends OrganicField implements JsonSerializable
15
17
{
16
18
use Make;
19
+ use HasAction;
17
20
18
21
/**
19
22
* The resource associated with the field.
@@ -132,8 +135,8 @@ class Field extends OrganicField implements JsonSerializable
132
135
/**
133
136
* Create a new field.
134
137
*
135
- * @param string|callable|null $attribute
136
- * @param callable|null $resolveCallback
138
+ * @param string|callable|null $attribute
139
+ * @param callable|null $resolveCallback
137
140
*/
138
141
public function __construct ($ attribute , callable $ resolveCallback = null )
139
142
{
@@ -162,7 +165,7 @@ public function indexCallback(Closure $callback)
162
165
}
163
166
164
167
/**
165
- * @param Closure $callback
168
+ * @param Closure $callback
166
169
* @return $this
167
170
*/
168
171
public function showCallback (Closure $ callback )
@@ -197,7 +200,7 @@ public function updateCallback(Closure $callback)
197
200
* Callback called when trying to fill this attribute, this callback will override the fill action, so make
198
201
* sure you assign the attribute to the model over this callback.
199
202
*
200
- * @param Closure $callback
203
+ * @param Closure $callback
201
204
* @return $this
202
205
*/
203
206
public function fillCallback (Closure $ callback )
@@ -210,9 +213,9 @@ public function fillCallback(Closure $callback)
210
213
/**
211
214
* Fill attribute with value from the request or delegate this action to the user defined callback.
212
215
*
213
- * @param RestifyRequest $request
216
+ * @param RestifyRequest $request
214
217
* @param $model
215
- * @param int|null $bulkRow
218
+ * @param int|null $bulkRow
216
219
* @return mixed|void
217
220
*/
218
221
public function fillAttribute (RestifyRequest $ request , $ model , int $ bulkRow = null )
@@ -263,10 +266,10 @@ public function fillAttribute(RestifyRequest $request, $model, int $bulkRow = nu
263
266
/**
264
267
* Fill the model with value from the request.
265
268
*
266
- * @param RestifyRequest $request
269
+ * @param RestifyRequest $request
267
270
* @param $model
268
271
* @param $attribute
269
- * @param int|null $bulkRow
272
+ * @param int|null $bulkRow
270
273
*/
271
274
protected function fillAttributeFromRequest (RestifyRequest $ request , $ model , $ attribute , int $ bulkRow = null )
272
275
{
@@ -281,18 +284,18 @@ protected function fillAttributeFromRequest(RestifyRequest $request, $model, $at
281
284
tap (
282
285
($ request ->input ($ attribute ) ?? $ request [$ attribute ]),
283
286
fn ($ value ) => $ model ->{$ this ->attribute } = $ request ->has ($ attribute )
284
- ? $ value
285
- : $ model ->{$ this ->attribute }
287
+ ? $ value
288
+ : $ model ->{$ this ->attribute }
286
289
);
287
290
}
288
291
289
292
/**
290
293
* Fill the model with value from the callback.
291
294
*
292
- * @param RestifyRequest $request
295
+ * @param RestifyRequest $request
293
296
* @param $model
294
297
* @param $attribute
295
- * @param int|null $bulkRow
298
+ * @param int|null $bulkRow
296
299
*/
297
300
protected function fillAttributeFromCallback (RestifyRequest $ request , $ model , $ attribute , int $ bulkRow = null )
298
301
{
@@ -304,7 +307,7 @@ protected function fillAttributeFromCallback(RestifyRequest $request, $model, $a
304
307
/**
305
308
* Fill the model with the value from value.
306
309
*
307
- * @param RestifyRequest $request
310
+ * @param RestifyRequest $request
308
311
* @param $model
309
312
* @param $attribute
310
313
* @return Field
@@ -469,8 +472,8 @@ public function isSortable()
469
472
/**
470
473
* Resolve the attribute's value for display.
471
474
*
472
- * @param mixed $repository
473
- * @param string|null $attribute
475
+ * @param mixed $repository
476
+ * @param string|null $attribute
474
477
* @return Field|void
475
478
*/
476
479
public function resolveForShow ($ repository , $ attribute = null )
@@ -486,9 +489,12 @@ public function resolveForShow($repository, $attribute = null)
486
489
if (! $ this ->showCallback ) {
487
490
$ this ->resolve ($ repository , $ attribute );
488
491
} elseif (is_callable ($ this ->showCallback )) {
489
- tap ($ this ->value ?? $ this ->resolveAttribute ($ repository , $ attribute ), function ($ value ) use ($ repository , $ attribute ) {
490
- $ this ->value = call_user_func ($ this ->showCallback , $ value , $ repository , $ attribute );
491
- });
492
+ tap (
493
+ $ this ->value ?? $ this ->resolveAttribute ($ repository , $ attribute ),
494
+ function ($ value ) use ($ repository , $ attribute ) {
495
+ $ this ->value = call_user_func ($ this ->showCallback , $ value , $ repository , $ attribute );
496
+ }
497
+ );
492
498
}
493
499
494
500
return $ this ;
@@ -509,9 +515,12 @@ public function resolveForIndex($repository, $attribute = null)
509
515
if (! $ this ->indexCallback ) {
510
516
$ this ->resolve ($ repository , $ attribute );
511
517
} elseif (is_callable ($ this ->indexCallback )) {
512
- tap ($ this ->value ?? $ this ->resolveAttribute ($ repository , $ attribute ), function ($ value ) use ($ repository , $ attribute ) {
513
- $ this ->value = call_user_func ($ this ->indexCallback , $ value , $ repository , $ attribute );
514
- });
518
+ tap (
519
+ $ this ->value ?? $ this ->resolveAttribute ($ repository , $ attribute ),
520
+ function ($ value ) use ($ repository , $ attribute ) {
521
+ $ this ->value = call_user_func ($ this ->indexCallback , $ value , $ repository , $ attribute );
522
+ }
523
+ );
515
524
}
516
525
517
526
return $ this ;
@@ -543,8 +552,8 @@ public function resolve($repository, $attribute = null)
543
552
/**
544
553
* Resolve the given attribute from the given repository.
545
554
*
546
- * @param mixed $repository
547
- * @param string $attribute
555
+ * @param mixed $repository
556
+ * @param string $attribute
548
557
* @return mixed
549
558
*/
550
559
protected function resolveAttribute ($ repository , $ attribute )
@@ -599,7 +608,7 @@ public function default($callback)
599
608
/**
600
609
* Resolve the default value for the field.
601
610
*
602
- * @param RestifyRequest $request
611
+ * @param RestifyRequest $request
603
612
* @return callable|mixed
604
613
*/
605
614
protected function resolveDefaultValue (RestifyRequest $ request )
@@ -614,7 +623,7 @@ protected function resolveDefaultValue(RestifyRequest $request)
614
623
/**
615
624
* Define the callback that should be used to resolve the field's value.
616
625
*
617
- * @param callable $resolveCallback
626
+ * @param callable $resolveCallback
618
627
* @return $this
619
628
*/
620
629
public function resolveCallback (callable $ resolveCallback )
@@ -638,21 +647,50 @@ public function afterStore(Closure $callback)
638
647
return $ this ;
639
648
}
640
649
641
- public function invokeAfter (RestifyRequest $ request , $ repository )
642
- {
643
- if ($ request ->isStoreRequest () && is_callable ($ this ->afterStoreCallback )) {
644
- call_user_func ($ this ->afterStoreCallback , data_get ($ repository , $ this ->attribute ), $ repository , $ request );
650
+ public function invokeAfter (RestifyRequest $ request , Model $ model ): void
651
+ {
652
+ if ($ request ->isStoreRequest ()) {
653
+ $ request ->repository ()
654
+ ->collectFields ($ request )
655
+ ->forStore ($ request , $ request ->repository ())
656
+ ->withActions ($ request , $ this )
657
+ ->authorizedStore ($ request )
658
+ ->each (fn (Field $ field ) => $ field ->actionHandler ->handle ($ request , $ model ));
659
+
660
+ if (is_callable ($ this ->afterStoreCallback )) {
661
+ call_user_func (
662
+ $ this ->afterStoreCallback ,
663
+ data_get ($ model , $ this ->attribute ),
664
+ $ model ,
665
+ $ request
666
+ );
667
+ }
645
668
}
646
669
647
- if ($ request ->isUpdateRequest () && is_callable ($ this ->afterUpdateCallback )) {
648
- call_user_func ($ this ->afterUpdateCallback , $ this ->resolveAttribute ($ repository , $ this ->attribute ), $ this ->valueBeforeUpdate , $ repository , $ request );
670
+ if ($ request ->isUpdateRequest ()) {
671
+ $ request ->repository ()
672
+ ->collectFields ($ request )
673
+ ->forUpdate ($ request , $ request ->repository ())
674
+ ->withActions ($ request , $ this )
675
+ ->authorizedUpdate ($ request )
676
+ ->each (fn (Field $ field ) => $ field ->actionHandler ->handle ($ request , $ model ));
677
+
678
+ if (is_callable ($ this ->afterUpdateCallback )) {
679
+ call_user_func (
680
+ $ this ->afterUpdateCallback ,
681
+ $ this ->resolveAttribute ($ model , $ this ->attribute ),
682
+ $ this ->valueBeforeUpdate ,
683
+ $ model ,
684
+ $ request
685
+ );
686
+ }
649
687
}
650
688
}
651
689
652
690
/**
653
691
* Indicate whatever the input is hidden or not.
654
692
*
655
- * @param bool $callback
693
+ * @param bool $callback
656
694
* @return $this
657
695
*/
658
696
public function hidden ($ callback = true )
@@ -668,7 +706,7 @@ public function hidden($callback = true)
668
706
/**
669
707
* Force set values when store/update.
670
708
*
671
- * @param callable|string $value
709
+ * @param callable|string $value
672
710
* @return $this
673
711
*/
674
712
public function value ($ value )
0 commit comments