Skip to content

Commit 4a74a5a

Browse files
committed
[Refactor] Update new to-one and to-many rule objects
1 parent 77dae9f commit 4a74a5a

File tree

6 files changed

+51
-157
lines changed

6 files changed

+51
-157
lines changed

docs/basics/validators.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,22 @@ protected function rules($record = null): array
245245
}
246246
```
247247

248-
The `Rule::hasOne()` and `Rule::hasMany()` methods accept a list of resource types for polymorphic relationships.
249-
They also return rule objects that have methods to determine whether empty relationships are acceptable.
250-
Use the `required()` method to indicate that an empty value is not acceptable, or use the `allowEmpty($bool)`
251-
method to toggle whether empty values are allowed. If neither method is called, the rule *will* accept
252-
an empty value.
248+
The `HasOne` and `HasMany` rules accept a list of resource types for polymorphic relationships. If no
249+
type is provided to the constructor, then the plural form of the attribute name will be used. For
250+
example:
251+
252+
```php
253+
protected function rules($record = null): array
254+
{
255+
return [
256+
'author' => [
257+
'required',
258+
new HasOne(), // expects 'authors' resources
259+
],
260+
'tags' => new HasMany(), // expects 'tags' resources
261+
];
262+
}
263+
```
253264

254265
### Updating Resources
255266

src/Rules/HasMany.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ protected function accept(?array $data): bool
1616
return false;
1717
}
1818

19-
if (empty($data) && $this->acceptsNone()) {
19+
if (empty($data)) {
2020
return true;
21-
} else if (empty($data)) {
22-
return false;
2321
}
2422

2523
if (Arr::isAssoc($data)) {

src/Rules/HasOne.php

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace CloudCreativity\LaravelJsonApi\Rules;
44

5-
use CloudCreativity\LaravelJsonApi\Utils\Str;
65
use Illuminate\Contracts\Validation\Rule;
6+
use Illuminate\Support\Str;
77

88
class HasOne implements Rule
99
{
@@ -13,11 +13,6 @@ class HasOne implements Rule
1313
*/
1414
private $types;
1515

16-
/**
17-
* @var bool
18-
*/
19-
private $allowEmpty;
20-
2116
/**
2217
* HasOne constructor.
2318
*
@@ -27,30 +22,6 @@ class HasOne implements Rule
2722
public function __construct(string ...$types)
2823
{
2924
$this->types = $types;
30-
$this->allowEmpty();
31-
}
32-
33-
/**
34-
* Set whether an empty `has-one` relationship is allowed.
35-
*
36-
* @param bool $bool
37-
* @return $this
38-
*/
39-
public function allowEmpty(bool $bool = true): self
40-
{
41-
$this->allowEmpty = $bool;
42-
43-
return $this;
44-
}
45-
46-
/**
47-
* Set a related resource to always be required.
48-
*
49-
* @return $this
50-
*/
51-
public function required(): self
52-
{
53-
return $this->allowEmpty(false);
5425
}
5526

5627
/**
@@ -62,6 +33,10 @@ public function passes($attribute, $value)
6233
return false;
6334
}
6435

36+
if (empty($this->types)) {
37+
$this->types = [Str::plural($attribute)];
38+
}
39+
6540
return $this->accept($value);
6641
}
6742

@@ -70,7 +45,7 @@ public function passes($attribute, $value)
7045
*/
7146
public function message()
7247
{
73-
$key = 'jsonapi::validation.' . Str::underscore(class_basename($this));;
48+
$key = 'jsonapi::validation.' . Str::snake(class_basename($this));;
7449

7550
return trans($key, [
7651
'types' => collect($this->types)->implode(', '),
@@ -85,7 +60,7 @@ public function message()
8560
*/
8661
protected function accept(?array $data): bool
8762
{
88-
if (is_null($data) && $this->acceptsNone()) {
63+
if (is_null($data)) {
8964
return true;
9065
}
9166

@@ -103,12 +78,4 @@ protected function acceptType($data): bool
10378
);
10479
}
10580

106-
/**
107-
* @return bool
108-
*/
109-
protected function acceptsNone(): bool
110-
{
111-
return $this->allowEmpty;
112-
}
113-
11481
}

src/Validation/Rule.php

Lines changed: 0 additions & 58 deletions
This file was deleted.

tests/lib/Unit/Validation/Rules/HasManyTest.php

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
namespace CloudCreativity\LaravelJsonApi\Tests\Unit\Validation\Rules;
1919

20+
use CloudCreativity\LaravelJsonApi\Rules\HasMany;
2021
use CloudCreativity\LaravelJsonApi\Tests\Unit\TestCase;
21-
use CloudCreativity\LaravelJsonApi\Validation\Rule;
2222

2323
class HasManyTest extends TestCase
2424
{
@@ -102,11 +102,24 @@ public function invalidProvider(): array
102102
public function testValid($types, $value): void
103103
{
104104
$types = (array) $types;
105-
$rule = Rule::hasMany(...$types);
105+
$rule = new HasMany(...$types);
106106

107107
$this->assertTrue($rule->passes('authors', $value));
108108
}
109109

110+
public function testValidWithAttributeName(): void
111+
{
112+
$rule = new HasMany();
113+
114+
$this->assertTrue($rule->passes('tags', [
115+
['type' => 'tags', 'id' => '1'],
116+
]));
117+
118+
$this->assertFalse($rule->passes('tags', [
119+
['type' => 'users', 'id' => '1'],
120+
]));
121+
}
122+
110123
/**
111124
* @param $types
112125
* @param $value
@@ -115,31 +128,9 @@ public function testValid($types, $value): void
115128
public function testInvalid($types, $value): void
116129
{
117130
$types = (array) $types;
118-
$rule = Rule::hasMany(...$types);
131+
$rule = new HasMany(...$types);
119132

120133
$this->assertFalse($rule->passes('authors', $value));
121134
}
122135

123-
public function testAllowEmpty(): void
124-
{
125-
$this->assertFalse(
126-
Rule::hasMany('users')->required()->passes('authors', []),
127-
'required'
128-
);
129-
130-
$this->assertTrue(
131-
Rule::hasMany('users')->allowEmpty(true)->passes('authors', []),
132-
'allows empty'
133-
);
134-
135-
$this->assertFalse(
136-
Rule::hasMany('users')->allowEmpty(false)->passes('authors', []),
137-
'does not allow empty'
138-
);
139-
140-
$this->assertFalse(
141-
Rule::hasMany('users')->allowEmpty(true)->passes('authors', null),
142-
'rejects empty has-one when empty allowed'
143-
);
144-
}
145136
}

tests/lib/Unit/Validation/Rules/HasOneTest.php

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace CloudCreativity\LaravelJsonApi\Tests\Unit\Validation\Rules;
44

5+
use CloudCreativity\LaravelJsonApi\Rules\HasOne;
56
use CloudCreativity\LaravelJsonApi\Tests\Unit\TestCase;
6-
use CloudCreativity\LaravelJsonApi\Validation\Rule;
77

88
class HasOneTest extends TestCase
99
{
@@ -72,11 +72,19 @@ public function invalidProvider(): array
7272
public function testValid($types, $value): void
7373
{
7474
$types = (array) $types;
75-
$rule = Rule::hasOne(...$types);
75+
$rule = new HasOne(...$types);
7676

7777
$this->assertTrue($rule->passes('author', $value));
7878
}
7979

80+
public function testValidWithAttributeName(): void
81+
{
82+
$rule = new HasOne();
83+
84+
$this->assertTrue($rule->passes('author', ['type' => 'authors', 'id' => '1']));
85+
$this->assertFalse($rule->passes('author', ['type' => 'users', 'id' => '1']));
86+
}
87+
8088
/**
8189
* @param $types
8290
* @param $value
@@ -85,31 +93,8 @@ public function testValid($types, $value): void
8593
public function testInvalid($types, $value): void
8694
{
8795
$types = (array) $types;
88-
$rule = Rule::hasOne(...$types);
96+
$rule = new HasOne(...$types);
8997

9098
$this->assertFalse($rule->passes('author', $value));
9199
}
92-
93-
public function testAllowEmpty(): void
94-
{
95-
$this->assertFalse(
96-
Rule::hasOne('users')->required()->passes('author', null),
97-
'required'
98-
);
99-
100-
$this->assertTrue(
101-
Rule::hasOne('users')->allowEmpty(true)->passes('author', null),
102-
'allows empty'
103-
);
104-
105-
$this->assertFalse(
106-
Rule::hasOne('users')->allowEmpty(false)->passes('author', null),
107-
'does not allow empty'
108-
);
109-
110-
$this->assertFalse(
111-
Rule::hasOne('users')->allowEmpty(true)->passes('author', []),
112-
'rejects empty has-many when empty allowed'
113-
);
114-
}
115100
}

0 commit comments

Comments
 (0)