Skip to content

Commit c91e448

Browse files
committed
refactor: update map field to use new rules utility class
1 parent b2f6dcc commit c91e448

File tree

2 files changed

+7
-32
lines changed

2 files changed

+7
-32
lines changed

src/Fields/Map.php

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use LaravelJsonApi\Eloquent\Fields\Concerns\Hideable;
2525
use LaravelJsonApi\Eloquent\Fields\Concerns\IsReadOnly;
2626
use LaravelJsonApi\Eloquent\Fields\Concerns\OnRelated;
27+
use LaravelJsonApi\Validation\Fields\FieldRuleMap;
2728
use LaravelJsonApi\Validation\Fields\IsValidated;
2829
use LogicException;
2930

@@ -222,43 +223,17 @@ public function serialize(object $model)
222223
*/
223224
public function rulesForCreation(?Request $request): ?array
224225
{
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);
240228
}
241229

242230
/**
243231
* @inheritDoc
244232
*/
245233
public function rulesForUpdate(?Request $request, object $model): ?array
246234
{
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);
262237
}
263238

264239
/**

tests/lib/Integration/Fields/MapTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,12 @@ public function testItIsValidatedOnCreate(): void
316316

317317
$this->assertInstanceOf(IsValidated::class, $map);
318318
$this->assertEquals([
319-
'.' => 'array:bar,foo',
319+
'.' => ['array:bar,foo'],
320320
'foo' => ['string', 'foo1'],
321321
'bar' => [new JsonNumber(), 'bar1'],
322322
], $map->rulesForCreation($request));
323323
$this->assertEquals([
324-
'.' => 'array:bar,foo',
324+
'.' => ['array:bar,foo'],
325325
'foo' => ['string', 'foo2'],
326326
'bar' => [new JsonNumber(), 'bar2'],
327327
], $map->rulesForUpdate($request, $model));

0 commit comments

Comments
 (0)