23
23
use Illuminate \Contracts \Validation \Validator ;
24
24
use Illuminate \Database \Eloquent \Model ;
25
25
use Illuminate \Http \Response ;
26
- use Illuminate \Support \Collection ;
27
26
use LaravelJsonApi \Contracts \Auth \Authorizer ;
28
27
use LaravelJsonApi \Contracts \Resources \Container as ResourceContainer ;
29
28
use LaravelJsonApi \Core \Document \ResourceObject ;
42
41
class ResourceRequest extends FormRequest
43
42
{
44
43
44
+ /**
45
+ * @var bool
46
+ */
47
+ protected bool $ validateExisting = true ;
48
+
45
49
/**
46
50
* @var callable|null
47
51
*/
@@ -101,7 +105,7 @@ public function isUpdating(): bool
101
105
*
102
106
* @return bool
103
107
*/
104
- public function isUpdatingRelation (): bool
108
+ public function isUpdatingRelationship (): bool
105
109
{
106
110
return $ this ->isMethod ('PATCH ' ) && $ this ->isRelationship ();
107
111
}
@@ -111,7 +115,7 @@ public function isUpdatingRelation(): bool
111
115
*
112
116
* @return bool
113
117
*/
114
- public function isAttachingRelation (): bool
118
+ public function isAttachingRelationship (): bool
115
119
{
116
120
return $ this ->isMethod ('POST ' ) && $ this ->isRelationship ();
117
121
}
@@ -121,7 +125,7 @@ public function isAttachingRelation(): bool
121
125
*
122
126
* @return bool
123
127
*/
124
- public function isDetachingRelation (): bool
128
+ public function isDetachingRelationship (): bool
125
129
{
126
130
return $ this ->isMethod ('DELETE ' ) && $ this ->isRelationship ();
127
131
}
@@ -133,7 +137,9 @@ public function isDetachingRelation(): bool
133
137
*/
134
138
public function isModifyingRelationship (): bool
135
139
{
136
- return $ this ->isUpdatingRelation () || $ this ->isAttachingRelation () || $ this ->isDetachingRelation ();
140
+ return $ this ->isUpdatingRelationship () ||
141
+ $ this ->isAttachingRelationship () ||
142
+ $ this ->isDetachingRelationship ();
137
143
}
138
144
139
145
/**
@@ -164,11 +170,11 @@ public function authorizeResource(Authorizer $authorizer): bool
164
170
$ model = $ this ->modelOrFail ();
165
171
$ fieldName = $ this ->jsonApi ()->route ()->fieldName ();
166
172
167
- if ($ this ->isAttachingRelation ()) {
173
+ if ($ this ->isAttachingRelationship ()) {
168
174
return $ authorizer ->attachRelationship ($ this , $ model , $ fieldName );
169
175
}
170
176
171
- if ($ this ->isDetachingRelation ()) {
177
+ if ($ this ->isDetachingRelationship ()) {
172
178
return $ authorizer ->detachRelationship ($ this , $ model , $ fieldName );
173
179
}
174
180
@@ -198,16 +204,6 @@ public function document(): array
198
204
return $ document ;
199
205
}
200
206
201
- /**
202
- * Get the field name for a relationship request.
203
- *
204
- * @return string
205
- */
206
- public function fieldName (): string
207
- {
208
- return $ this ->jsonApi ()->route ()->fieldName ();
209
- }
210
-
211
207
/**
212
208
* @inheritDoc
213
209
*/
@@ -275,7 +271,7 @@ public function validationDataForDelete(): array
275
271
public function validatedForRelation ()
276
272
{
277
273
$ data = $ this ->validated ();
278
- $ fieldName = $ this ->fieldName ();
274
+ $ fieldName = $ this ->getFieldName ();
279
275
280
276
if (array_key_exists ($ fieldName , $ data )) {
281
277
return $ data [$ fieldName ];
@@ -388,8 +384,14 @@ protected function createDeleteValidator(ValidationFactory $factory): Validator
388
384
return $ factory ->make (
389
385
$ this ->validationDataForDelete (),
390
386
method_exists ($ this , 'deleteRules ' ) ? $ this ->container ->call ([$ this , 'deleteRules ' ]) : [],
391
- method_exists ($ this , 'deleteMessages ' ) ? $ this ->deleteMessages () : $ this ->messages (),
392
- method_exists ($ this , 'deleteAttributes ' ) ? $ this ->deleteAttributes () : $ this ->attributes ()
387
+ array_merge (
388
+ $ this ->messages (),
389
+ method_exists ($ this , 'deleteMessages ' ) ? $ this ->deleteMessages () : []
390
+ ),
391
+ array_merge (
392
+ $ this ->attributes (),
393
+ method_exists ($ this , 'deleteAttributes ' ) ? $ this ->deleteAttributes () : []
394
+ )
393
395
);
394
396
}
395
397
@@ -418,24 +420,24 @@ protected function dataForCreate(array $document): array
418
420
* merge attributes provided by the client over the top of the existing attribute
419
421
* values.
420
422
*
421
- * @param Model|object $record
422
- * the record being updated.
423
+ * @param Model|object $model
424
+ * the model being updated.
423
425
* @param array $document
424
426
* the JSON API document to validate.
425
427
* @return array
426
428
*/
427
- protected function dataForUpdate (object $ record , array $ document ): array
429
+ protected function dataForUpdate (object $ model , array $ document ): array
428
430
{
429
431
$ data = $ document ['data ' ] ?? [];
430
432
431
- if ($ this ->mustValidateExisting ($ record , $ data )) {
433
+ if ($ this ->mustValidateExisting ($ model , $ document )) {
432
434
$ data ['attributes ' ] = $ this ->extractAttributes (
433
- $ record ,
435
+ $ model ,
434
436
$ data ['attributes ' ] ?? []
435
437
);
436
438
437
439
$ data ['relationships ' ] = $ this ->extractRelationships (
438
- $ record ,
440
+ $ model ,
439
441
$ data ['relationships ' ] ?? []
440
442
);
441
443
}
@@ -575,7 +577,7 @@ private function extractRelationships(object $model, array $new): array
575
577
private function relationshipRules (): array
576
578
{
577
579
$ rules = $ this ->container ->call ([$ this , 'rules ' ]);
578
- $ fieldName = $ this ->fieldName ();
580
+ $ fieldName = $ this ->getFieldName ();
579
581
580
582
return collect ($ rules )
581
583
->filter (fn ($ v , $ key ) => Str::startsWith ($ key , $ fieldName ))
0 commit comments