Skip to content

Commit b14f6fb

Browse files
authored
Fix #295: Merge skipOnEmpty and skipOnEmptyCallback (#300)
1 parent 79aac22 commit b14f6fb

37 files changed

Lines changed: 400 additions & 319 deletions

src/BeforeValidationInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
use Closure;
88

99
/**
10-
* BeforeValidationInterface is the interface implemented by rules that need to execute checks before the validation.
10+
* `BeforeValidationInterface` is the interface implemented by rules that need to execute checks before the validation.
1111
*/
1212
interface BeforeValidationInterface
1313
{
14-
public function shouldSkipOnEmpty(mixed $validatedValue): bool;
15-
1614
public function shouldSkipOnError(): bool;
1715

1816
/**

src/Rule/AtLeast.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@
99
use Yiisoft\Validator\BeforeValidationInterface;
1010
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
1111
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
12+
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
1213
use Yiisoft\Validator\SerializableRuleInterface;
14+
use Yiisoft\Validator\SkipOnEmptyInterface;
1315
use Yiisoft\Validator\ValidationContext;
1416

1517
/**
1618
* Checks if at least {@see AtLeast::$min} of many attributes are filled.
1719
*/
1820
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
19-
final class AtLeast implements SerializableRuleInterface, BeforeValidationInterface
21+
final class AtLeast implements SerializableRuleInterface, BeforeValidationInterface, SkipOnEmptyInterface
2022
{
2123
use BeforeValidationTrait;
2224
use RuleNameTrait;
25+
use SkipOnEmptyTrait;
2326

2427
public function __construct(
2528
/**
@@ -35,18 +38,17 @@ public function __construct(
3538
* Message to display in case of error.
3639
*/
3740
private string $message = 'The model is not valid. Must have at least "{min}" filled attributes.',
38-
private bool $skipOnEmpty = false,
41+
3942
/**
40-
* @var callable
43+
* @var bool|callable|null
4144
*/
42-
private $skipOnEmptyCallback = null,
45+
private $skipOnEmpty = null,
4346
private bool $skipOnError = false,
4447
/**
4548
* @var Closure(mixed, ValidationContext):bool|null
4649
*/
4750
private ?Closure $when = null
4851
) {
49-
$this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback);
5052
}
5153

5254
/**
@@ -79,7 +81,7 @@ public function getOptions(): array
7981
'message' => $this->message,
8082
'parameters' => ['min' => $this->min],
8183
],
82-
'skipOnEmpty' => $this->skipOnEmpty,
84+
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
8385
'skipOnError' => $this->skipOnError,
8486
];
8587
}

src/Rule/Boolean.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@
99
use Yiisoft\Validator\BeforeValidationInterface;
1010
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
1111
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
12+
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
1213
use Yiisoft\Validator\SerializableRuleInterface;
14+
use Yiisoft\Validator\SkipOnEmptyInterface;
1315
use Yiisoft\Validator\ValidationContext;
1416

1517
/**
1618
* Checks if the value is a boolean value or a value corresponding to it.
1719
*/
1820
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
19-
final class Boolean implements SerializableRuleInterface, BeforeValidationInterface
21+
final class Boolean implements SerializableRuleInterface, BeforeValidationInterface, SkipOnEmptyInterface
2022
{
2123
use BeforeValidationTrait;
2224
use RuleNameTrait;
25+
use SkipOnEmptyTrait;
2326

2427
public function __construct(
2528
/**
@@ -37,18 +40,17 @@ public function __construct(
3740
*/
3841
private bool $strict = false,
3942
private string $message = 'The value must be either "{true}" or "{false}".',
40-
private bool $skipOnEmpty = false,
43+
4144
/**
42-
* @var callable
45+
* @var bool|callable|null
4346
*/
44-
private $skipOnEmptyCallback = null,
47+
private $skipOnEmpty = null,
4548
private bool $skipOnError = false,
4649
/**
4750
* @var Closure(mixed, ValidationContext):bool|null
4851
*/
4952
private ?Closure $when = null,
5053
) {
51-
$this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback);
5254
}
5355

5456
/**
@@ -96,7 +98,7 @@ public function getOptions(): array
9698
'false' => $this->falseValue === false ? 'false' : $this->falseValue,
9799
],
98100
],
99-
'skipOnEmpty' => $this->skipOnEmpty,
101+
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
100102
'skipOnError' => $this->skipOnError,
101103
];
102104
}

src/Rule/Callback.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,33 @@
88
use Yiisoft\Validator\BeforeValidationInterface;
99
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
1010
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
11+
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
1112
use Yiisoft\Validator\SerializableRuleInterface;
13+
use Yiisoft\Validator\SkipOnEmptyInterface;
1214
use Yiisoft\Validator\ValidationContext;
1315

14-
final class Callback implements SerializableRuleInterface, BeforeValidationInterface
16+
final class Callback implements SerializableRuleInterface, BeforeValidationInterface, SkipOnEmptyInterface
1517
{
1618
use BeforeValidationTrait;
1719
use RuleNameTrait;
20+
use SkipOnEmptyTrait;
1821

1922
public function __construct(
2023
/**
2124
* @var callable
2225
*/
2326
private $callback,
24-
private bool $skipOnEmpty = false,
27+
2528
/**
26-
* @var callable
29+
* @var bool|callable|null
2730
*/
28-
private $skipOnEmptyCallback = null,
31+
private $skipOnEmpty = null,
2932
private bool $skipOnError = false,
3033
/**
3134
* @var Closure(mixed, ValidationContext):bool|null
3235
*/
3336
private ?Closure $when = null,
3437
) {
35-
$this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback);
3638
}
3739

3840
/**
@@ -46,7 +48,7 @@ public function getCallback(): callable
4648
public function getOptions(): array
4749
{
4850
return [
49-
'skipOnEmpty' => $this->skipOnEmpty,
51+
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
5052
'skipOnError' => $this->skipOnError,
5153
];
5254
}

src/Rule/Compare.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
use Yiisoft\Validator\BeforeValidationInterface;
1111
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
1212
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
13+
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
1314
use Yiisoft\Validator\SerializableRuleInterface;
15+
use Yiisoft\Validator\SkipOnEmptyInterface;
1416
use Yiisoft\Validator\ValidationContext;
1517

16-
abstract class Compare implements SerializableRuleInterface, BeforeValidationInterface
18+
abstract class Compare implements SerializableRuleInterface, BeforeValidationInterface, SkipOnEmptyInterface
1719
{
1820
use BeforeValidationTrait;
1921
use RuleNameTrait;
22+
use SkipOnEmptyTrait;
2023

2124
/**
2225
* Constant for specifying the comparison as string values.
@@ -81,19 +84,17 @@ public function __construct(
8184
* {@see TYPE_NUMBER}.
8285
*/
8386
private string $operator = '==',
84-
private bool $skipOnEmpty = false,
87+
8588
/**
86-
* @var callable
89+
* @var bool|callable|null
8790
*/
88-
private $skipOnEmptyCallback = null,
91+
private $skipOnEmpty = null,
8992
private bool $skipOnError = false,
9093
/**
9194
* @var Closure(mixed, ValidationContext):bool|null
9295
*/
9396
private ?Closure $when = null,
9497
) {
95-
$this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback);
96-
9798
if (!isset($this->validOperators[$operator])) {
9899
throw new InvalidArgumentException("Operator \"$operator\" is not supported.");
99100
}
@@ -147,7 +148,7 @@ public function getOptions(): array
147148
],
148149
'type' => $this->type,
149150
'operator' => $this->operator,
150-
'skipOnEmpty' => $this->skipOnEmpty,
151+
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
151152
'skipOnError' => $this->skipOnError,
152153
];
153154
}

src/Rule/Composite.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,38 @@
1010
use Yiisoft\Validator\BeforeValidationInterface;
1111
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
1212
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
13+
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
1314
use Yiisoft\Validator\RuleInterface;
1415
use Yiisoft\Validator\SerializableRuleInterface;
16+
use Yiisoft\Validator\SkipOnEmptyInterface;
1517
use Yiisoft\Validator\ValidationContext;
1618

1719
/**
1820
* Allows to combine and validate multiple rules.
1921
*/
2022
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
21-
class Composite implements SerializableRuleInterface, BeforeValidationInterface
23+
class Composite implements SerializableRuleInterface, BeforeValidationInterface, SkipOnEmptyInterface
2224
{
2325
use BeforeValidationTrait;
2426
use RuleNameTrait;
27+
use SkipOnEmptyTrait;
2528

2629
public function __construct(
2730
/**
2831
* @var iterable<RuleInterface>
2932
*/
3033
private iterable $rules = [],
31-
private bool $skipOnEmpty = false,
34+
3235
/**
33-
* @var callable
36+
* @var bool|callable|null
3437
*/
35-
private $skipOnEmptyCallback = null,
38+
private $skipOnEmpty = null,
3639
private bool $skipOnError = false,
3740
/**
3841
* @var Closure(mixed, ValidationContext):bool|null
3942
*/
4043
private ?Closure $when = null,
4144
) {
42-
$this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback);
4345
}
4446

4547
#[ArrayShape([
@@ -59,7 +61,7 @@ public function getOptions(): array
5961
}
6062

6163
return [
62-
'skipOnEmpty' => $this->skipOnEmpty,
64+
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
6365
'skipOnError' => $this->skipOnError,
6466
'rules' => $arrayOfRules,
6567
];

src/Rule/Count.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@
1111
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
1212
use Yiisoft\Validator\Rule\Trait\LimitTrait;
1313
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
14+
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
1415
use Yiisoft\Validator\SerializableRuleInterface;
16+
use Yiisoft\Validator\SkipOnEmptyInterface;
1517
use Yiisoft\Validator\ValidationContext;
1618

1719
/**
1820
* Validates that the value contains certain number of items. Can be applied to arrays or classes implementing
1921
* {@see Countable} interface.
2022
*/
2123
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
22-
final class Count implements SerializableRuleInterface, BeforeValidationInterface
24+
final class Count implements SerializableRuleInterface, BeforeValidationInterface, SkipOnEmptyInterface
2325
{
2426
use BeforeValidationTrait;
2527
use LimitTrait;
2628
use RuleNameTrait;
29+
use SkipOnEmptyTrait;
2730

2831
public function __construct(
2932
/**
@@ -67,18 +70,17 @@ public function __construct(
6770
*/
6871
string $notExactlyMessage = 'This value must contain exactly {exactly, number} {exactly, plural, one{item} ' .
6972
'other{items}}.',
70-
private bool $skipOnEmpty = false,
73+
7174
/**
72-
* @var callable
75+
* @var bool|callable|null
7376
*/
74-
private $skipOnEmptyCallback = null,
77+
private $skipOnEmpty = null,
7578
private bool $skipOnError = false,
7679
/**
7780
* @var Closure(mixed, ValidationContext):bool|null
7881
*/
7982
private ?Closure $when = null,
8083
) {
81-
$this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback);
8284
$this->initLimitProperties(
8385
$min,
8486
$max,
@@ -103,7 +105,7 @@ public function getOptions(): array
103105
'message' => [
104106
'message' => $this->getMessage(),
105107
],
106-
'skipOnEmpty' => $this->skipOnEmpty,
108+
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
107109
'skipOnError' => $this->skipOnError,
108110
]);
109111
}

src/Rule/Each.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@
1010
use Yiisoft\Validator\BeforeValidationInterface;
1111
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
1212
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
13+
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
1314
use Yiisoft\Validator\RuleInterface;
1415
use Yiisoft\Validator\SerializableRuleInterface;
16+
use Yiisoft\Validator\SkipOnEmptyInterface;
1517
use Yiisoft\Validator\ValidationContext;
1618

1719
/**
1820
* Validates an array by checking each of its elements against a set of rules.
1921
*/
2022
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
21-
final class Each implements SerializableRuleInterface, BeforeValidationInterface
23+
final class Each implements SerializableRuleInterface, BeforeValidationInterface, SkipOnEmptyInterface
2224
{
2325
use BeforeValidationTrait;
2426
use RuleNameTrait;
27+
use SkipOnEmptyTrait;
2528

2629
public function __construct(
2730
/**
@@ -30,18 +33,17 @@ public function __construct(
3033
private iterable $rules = [],
3134
private string $incorrectInputMessage = 'Value must be array or iterable.',
3235
private string $message = '{error} {value} given.',
33-
private bool $skipOnEmpty = false,
36+
3437
/**
35-
* @var callable
38+
* @var bool|callable|null
3639
*/
37-
private $skipOnEmptyCallback = null,
40+
private $skipOnEmpty = null,
3841
private bool $skipOnError = false,
3942
/**
4043
* @var Closure(mixed, ValidationContext):bool|null
4144
*/
4245
private ?Closure $when = null,
4346
) {
44-
$this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback);
4547
}
4648

4749
/**
@@ -93,7 +95,7 @@ public function getOptions(): array
9395
'message' => [
9496
'message' => $this->getMessage(),
9597
],
96-
'skipOnEmpty' => $this->skipOnEmpty,
98+
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
9799
'skipOnError' => $this->skipOnError,
98100
'rules' => $arrayOfRules,
99101
];

0 commit comments

Comments
 (0)