|
24 | 24 | use LaravelJsonApi\Eloquent\Fields\Concerns\Hideable; |
25 | 25 | use LaravelJsonApi\Eloquent\Fields\Concerns\IsReadOnly; |
26 | 26 | use LaravelJsonApi\Eloquent\Fields\Concerns\OnRelated; |
| 27 | +use LaravelJsonApi\Validation\Fields\FieldRuleMap; |
27 | 28 | use LaravelJsonApi\Validation\Fields\IsValidated; |
28 | 29 | use LogicException; |
29 | 30 |
|
@@ -222,43 +223,17 @@ public function serialize(object $model) |
222 | 223 | */ |
223 | 224 | public function rulesForCreation(?Request $request): ?array |
224 | 225 | { |
225 | | - $fields = []; |
226 | | - $rules = []; |
227 | | - |
228 | | - /** @var AttributeContract $attr */ |
229 | | - foreach ($this->map as $attr) { |
230 | | - if ($attr instanceof IsValidated) { |
231 | | - $fields[] = $name = $attr->name(); |
232 | | - $rules[$name] = $attr->rulesForCreation($request); |
233 | | - } |
234 | | - } |
235 | | - |
236 | | - return !empty($fields) ? [ |
237 | | - '.' => 'array:' . implode(',', $fields), |
238 | | - ...$rules, |
239 | | - ] : null; |
| 226 | + return FieldRuleMap::make($this->map) |
| 227 | + ->creation($request); |
240 | 228 | } |
241 | 229 |
|
242 | 230 | /** |
243 | 231 | * @inheritDoc |
244 | 232 | */ |
245 | 233 | public function rulesForUpdate(?Request $request, object $model): ?array |
246 | 234 | { |
247 | | - $fields = []; |
248 | | - $rules = []; |
249 | | - |
250 | | - /** @var AttributeContract $attr */ |
251 | | - foreach ($this->map as $attr) { |
252 | | - if ($attr instanceof IsValidated) { |
253 | | - $fields[] = $name = $attr->name(); |
254 | | - $rules[$name] = $attr->rulesForUpdate($request, $model); |
255 | | - } |
256 | | - } |
257 | | - |
258 | | - return !empty($fields) ? [ |
259 | | - '.' => 'array:' . implode(',', $fields), |
260 | | - ...$rules, |
261 | | - ] : null; |
| 235 | + return FieldRuleMap::make($this->map) |
| 236 | + ->update($request, $model); |
262 | 237 | } |
263 | 238 |
|
264 | 239 | /** |
|
0 commit comments