File tree Expand file tree Collapse file tree 5 files changed +95
-4
lines changed
Expand file tree Collapse file tree 5 files changed +95
-4
lines changed Original file line number Diff line number Diff line change 66use Illuminate \Support \Collection ;
77use Illuminate \Support \Str ;
88use Lomkit \Rest \Concerns \Makeable ;
9+ use Lomkit \Rest \Concerns \Metable ;
910use Lomkit \Rest \Http \Requests \OperateRequest ;
1011use Lomkit \Rest \Http \Requests \RestRequest ;
1112
1213class Action implements \JsonSerializable
1314{
14- use Makeable;
15+ use Makeable, Metable ;
1516
1617 /**
1718 * The name of the connection the job should be sent to.
@@ -73,7 +74,8 @@ public function jsonSerialize(): array
7374 return [
7475 'name ' => $ this ->name (),
7576 'uriKey ' => $ this ->uriKey (),
76- 'fields ' => $ this ->fields ($ request )
77+ 'fields ' => $ this ->fields ($ request ),
78+ 'meta ' => $ this ->meta ()
7779 ];
7880 }
7981
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Lomkit \Rest \Concerns ;
4+
5+ trait Metable
6+ {
7+ /**
8+ * The meta array
9+ *
10+ * @var array<string, mixed>
11+ */
12+ public $ meta = [];
13+
14+ /**
15+ * Get the meta data
16+ *
17+ * @return array
18+ */
19+ public function meta ()
20+ {
21+ return $ this ->meta ;
22+ }
23+
24+ /**
25+ * Set additional meta information for the element.
26+ *
27+ * @param array $meta
28+ * @return $this
29+ */
30+ public function withMeta (array $ meta )
31+ {
32+ return tap ($ this , function () use ($ meta ) {
33+ $ this ->meta = array_merge ($ this ->meta , $ meta );
34+ });
35+ }
36+ }
Original file line number Diff line number Diff line change @@ -33,7 +33,7 @@ public function test_getting_actions(): void
3333 '/api/models/actions ' ,
3434 ['Accept ' => 'application/json ' ]
3535 );
36-
36+
3737 $ response ->assertJson (
3838 ['data ' => collect (new ModelResource )->each ->jsonSerialize ()->toArray ()]
3939 );
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Lomkit \Rest \Tests \Support \Rest \Actions ;
4+
5+ use Illuminate \Database \Eloquent \Model ;
6+ use Illuminate \Support \Collection ;
7+ use Lomkit \Rest \Actions \Action ;
8+ use Lomkit \Rest \Http \Requests \RestRequest ;
9+
10+ class WithMetaModifyNumberAction extends Action
11+ {
12+ public function __construct ()
13+ {
14+ $ this ->withMeta ([
15+ 'color ' => '#FFFFFF '
16+ ]);
17+ }
18+
19+ /**
20+ * Perform the action on the given models.
21+ *
22+ * @param array $fields
23+ * @param \Illuminate\Support\Collection $models
24+ * @return mixed
25+ */
26+ public function handle (array $ fields , Collection $ models )
27+ {
28+ foreach ($ models as $ model ) {
29+ /** @var Model $model */
30+ $ model ->forceFill (
31+ ['number ' => $ fields ['number ' ] ?? 100000000 ]
32+ )
33+ ->save ();
34+ }
35+ }
36+
37+ /**
38+ * Called in an action failed.
39+ *
40+ * @param RestRequest $request
41+ * @return array
42+ */
43+ public function fields (RestRequest $ request )
44+ {
45+ return [
46+ 'number ' => [
47+ 'min: 100 '
48+ ]
49+ ];
50+ }
51+ }
Original file line number Diff line number Diff line change 2323use Lomkit \Rest \Tests \Support \Models \MorphOneRelation ;
2424use Lomkit \Rest \Tests \Support \Rest \Actions \ModifyNumberAction ;
2525use Lomkit \Rest \Tests \Support \Rest \Actions \QueueableModifyNumberAction ;
26+ use Lomkit \Rest \Tests \Support \Rest \Actions \WithMetaModifyNumberAction ;
2627
2728class ModelResource extends Resource
2829{
@@ -124,7 +125,8 @@ public function defaultOrderBy(RestRequest $request): array
124125 public function actions (RestRequest $ request ): array {
125126 return [
126127 ModifyNumberAction::make (),
127- QueueableModifyNumberAction::make ()
128+ QueueableModifyNumberAction::make (),
129+ WithMetaModifyNumberAction::make ()
128130 ];
129131 }
130132}
You can’t perform that action at this time.
0 commit comments