Skip to content

Commit 6552777

Browse files
authored
Add anyOf and in_array_keys (#56)
* docs: updated link to point at 12.x * update: added in array keys rule * update: added any of rule * refactor: add missing return type * refactor: switch to shared defined rule type * refactor: leave unwrapping to laravel
1 parent 961f08a commit 6552777

File tree

6 files changed

+375
-20
lines changed

6 files changed

+375
-20
lines changed

UPGRADE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## v6
44

5+
### Upgrading from v6.0 to v6.1
6+
7+
- Minimum Laravel version increased from `12.0` to `12.16`.
8+
59
### Upgrading from v5.6 to v6.0
610

711
- Minimum Laravel version increased from `11.43` to `12.0`.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"minimum-stability": "stable",
77
"require": {
88
"php": "^8.2||^8.3||^8.4",
9-
"laravel/framework": "^12.0"
9+
"laravel/framework": "^12.16"
1010
},
1111
"require-dev": {
1212
"ext-json": "*",

src/Rule.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
use Illuminate\Contracts\Validation\Rule as RuleContract;
1313
use Illuminate\Contracts\Validation\ValidationRule;
1414
use Illuminate\Support\Collection;
15+
use Illuminate\Support\Fluent;
1516
use Illuminate\Validation\ConditionalRules;
1617
use Illuminate\Validation\Rule as LaravelRule;
18+
use Illuminate\Validation\Rules\AnyOf;
1719
use Illuminate\Validation\Rules\ArrayRule;
1820
use Illuminate\Validation\Rules\Can;
1921
use Illuminate\Validation\Rules\Date;
@@ -33,6 +35,11 @@
3335
use Stringable;
3436
use UnitEnum;
3537

38+
/**
39+
* @phpstan-type RuleType RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string
40+
* @phpstan-type FieldRules RuleSet|array<int, RuleType>|RuleType
41+
* @phpstan-type RuleSetDefinition RuleSet|array<string, FieldRules>
42+
*/
3643
class Rule
3744
{
3845
/**
@@ -147,6 +154,18 @@ public static function alphaNum(?bool $limitToAscii = null): string
147154
return 'alpha_num';
148155
}
149156

157+
/**
158+
* The `anyOf` validation rule allows you to specify that the field under validation must satisfy any of the given
159+
* validation rulesets.
160+
*
161+
* @link https://laravel.com/docs/12.x/validation#rule-anyof
162+
* @param array<array-key, RuleSetDefinition> $ruleSets
163+
*/
164+
public static function anyOf(array $ruleSets): AnyOf
165+
{
166+
return LaravelRule::anyOf($ruleSets);
167+
}
168+
150169
/**
151170
* The field under validation must be a PHP *array*.
152171
*
@@ -662,6 +681,16 @@ public static function inArray(string $anotherField): string
662681
return 'in_array:'.$anotherField;
663682
}
664683

684+
/**
685+
* The field under validation must be an array having at least one of the given *values* as a key within the array.
686+
*
687+
* @link https://laravel.com/docs/12.x/validation#rule-in-array-keys
688+
*/
689+
public static function inArrayKeys(string ...$value): string
690+
{
691+
return 'in_array_keys:'.implode(',', $value);
692+
}
693+
665694
/**
666695
* The field under validation must be an integer.
667696
*
@@ -1403,9 +1432,9 @@ public static function uuid(): string
14031432
/**
14041433
* Create a new conditional rule set.
14051434
*
1406-
* @param bool|callable(\Illuminate\Support\Fluent<array-key, mixed>): bool $condition
1407-
* @param array<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>|string|RuleSet $rules
1408-
* @param array<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>|string|RuleSet $defaultRules
1435+
* @param bool|callable(Fluent<array-key, mixed>): bool $condition
1436+
* @param array<array-key, RuleType>|string|RuleSet $rules
1437+
* @param array<array-key, RuleType>|string|RuleSet $defaultRules
14091438
*/
14101439
public static function when(
14111440
mixed $condition,

src/RuleHelperServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class RuleHelperServiceProvider extends ServiceProvider
1010
{
11-
public function register()
11+
public function register(): void
1212
{
1313
$this->app->singleton(Contracts\DefinedRuleSets::class, DefinedRuleSets::class);
1414
}

src/RuleSet.php

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,29 @@
99
use Brick\Math\BigNumber;
1010
use DateTimeInterface;
1111
use Illuminate\Contracts\Support\Arrayable;
12-
use Illuminate\Contracts\Validation\InvokableRule;
13-
use Illuminate\Contracts\Validation\Rule as RuleContract;
14-
use Illuminate\Contracts\Validation\ValidationRule;
15-
use Illuminate\Validation\ConditionalRules;
1612
use Illuminate\Validation\Rules\Password;
1713
use Illuminate\Validation\Rules\RequiredIf;
1814
use IteratorAggregate;
19-
use Stringable;
2015
use UnitEnum;
2116

2217
/**
23-
* @implements Arrayable<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>
24-
* @implements IteratorAggregate<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>
18+
* @phpstan-import-type RuleType from Rule
19+
* @phpstan-import-type RuleSetDefinition from Rule
20+
* @implements Arrayable<array-key, RuleType>
21+
* @implements IteratorAggregate<array-key, RuleType>
2522
*/
2623
class RuleSet implements Arrayable, IteratorAggregate
2724
{
2825
/**
29-
* @param array<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string> $rules
26+
* @param array<array-key, RuleType> $rules
3027
*/
3128
final public function __construct(protected array $rules = [])
3229
{
3330
//
3431
}
3532

3633
/**
37-
* @return ArrayIterator<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>
34+
* @return ArrayIterator<array-key, RuleType>
3835
*/
3936
public function getIterator(): ArrayIterator
4037
{
@@ -44,7 +41,7 @@ public function getIterator(): ArrayIterator
4441
/**
4542
* Get the rule set as an array.
4643
*
47-
* @return array<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>
44+
* @return array<array-key, RuleType>
4845
*/
4946
public function toArray(): array
5047
{
@@ -54,7 +51,7 @@ public function toArray(): array
5451
/**
5552
* Create a new rule set.
5653
*
57-
* @param array<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string> $rules
54+
* @param array<array-key, RuleType> $rules
5855
*/
5956
public static function create(array $rules = []): self
6057
{
@@ -80,7 +77,7 @@ public static function useDefined(string|BackedEnum|UnitEnum $name): RuleSet
8077
/**
8178
* Append one or more rules to the end of the rule set.
8279
*
83-
* @param RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string $rule
80+
* @param RuleType $rule
8481
*/
8582
public function concat(...$rule): self
8683
{
@@ -98,7 +95,7 @@ public function concatDefined(string $name): self
9895
/**
9996
* Append a rule to the end of the rule set.
10097
*
101-
* @param RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string $rule
98+
* @param RuleType $rule
10299
*/
103100
public function rule(mixed $rule): self
104101
{
@@ -205,6 +202,18 @@ public function alphaNum(?bool $limitToAscii = null): self
205202
return $this->rule(Rule::alphaNum($limitToAscii));
206203
}
207204

205+
/**
206+
* The `anyOf` validation rule allows you to specify that the field under validation must satisfy any of the given
207+
* validation rulesets.
208+
*
209+
* @link https://laravel.com/docs/12.x/validation#rule-anyof
210+
* @param array<array-key, RuleSetDefinition> $ruleSets
211+
*/
212+
public function anyOf(array $ruleSets): self
213+
{
214+
return $this->rule(Rule::anyOf($ruleSets));
215+
}
216+
208217
/**
209218
* The field under validation must be a PHP *array*.
210219
*
@@ -759,6 +768,16 @@ public function inArray(string $anotherField): self
759768
return $this->rule(Rule::inArray($anotherField));
760769
}
761770

771+
/**
772+
* The field under validation must be an array having at least one of the given *values* as a key within the array.
773+
*
774+
* @link https://laravel.com/docs/12.x/validation#rule-in-array-keys
775+
*/
776+
public function inArrayKeys(string ...$value): self
777+
{
778+
return $this->rule(Rule::inArrayKeys(...$value));
779+
}
780+
762781
/**
763782
* The field under validation must be an integer.
764783
*
@@ -1505,8 +1524,8 @@ public function uuid(): self
15051524
* Create a new conditional rule set.
15061525
*
15071526
* @param bool|callable(\Illuminate\Support\Fluent<array-key, mixed>): bool $condition
1508-
* @param array<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>|string|RuleSet $rules
1509-
* @param array<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>|string|RuleSet $defaultRules
1527+
* @param array<array-key, RuleType>|string|RuleSet $rules
1528+
* @param array<array-key, RuleType>|string|RuleSet $defaultRules
15101529
*/
15111530
public function when(mixed $condition, array|string|RuleSet $rules, array|string|RuleSet $defaultRules = []): self
15121531
{

0 commit comments

Comments
 (0)