|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule\Trait; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use Yiisoft\Validator\SkipOnAll; |
|
10
|
|
|
use Yiisoft\Validator\SkipOnNull; |
|
11
|
|
|
use Yiisoft\Validator\ValidationContext; |
|
12
|
|
|
|
|
13
|
|
|
use function is_callable; |
|
14
|
|
|
|
|
15
|
|
|
trait BeforeValidationTrait |
|
16
|
|
|
{ |
|
17
|
|
|
public function getSkipOnEmpty(): bool |
|
18
|
|
|
{ |
|
19
|
|
|
return $this->skipOnEmpty; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
62 |
|
public function shouldSkipOnEmpty($value): bool |
|
23
|
|
|
{ |
|
24
|
62 |
|
return ($this->skipOnEmptyCallback)($value); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
89 |
|
protected function initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback): void |
|
28
|
|
|
{ |
|
29
|
89 |
|
$this->skipOnEmpty = $skipOnEmpty; |
|
|
|
|
|
|
30
|
89 |
|
$this->skipOnEmptyCallback = $skipOnEmptyCallback; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
89 |
|
if ($this->skipOnEmptyCallback) { |
|
33
|
|
|
if (!is_callable($this->skipOnEmptyCallback)) { |
|
34
|
|
|
throw new InvalidArgumentException('$skipOnEmptyCallback must be a callable.'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$this->skipOnEmpty = true; |
|
38
|
|
|
|
|
39
|
|
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
89 |
|
$this->skipOnEmptyCallback = $this->skipOnEmpty === false ? new SkipOnAll() : new SkipOnNull(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
11 |
|
public function skipOnEmpty(bool $value): self |
|
46
|
|
|
{ |
|
47
|
11 |
|
$new = clone $this; |
|
48
|
11 |
|
$new->skipOnEmpty = $value; |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
11 |
|
return $new; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
11 |
|
public function skipOnEmptyCallback(callable $value): self |
|
54
|
|
|
{ |
|
55
|
11 |
|
$new = clone $this; |
|
56
|
11 |
|
$new->skipOnEmptyCallback = $value; |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
11 |
|
return $new; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
60 |
|
public function shouldSkipOnError(): bool |
|
62
|
|
|
{ |
|
63
|
60 |
|
return $this->skipOnError; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @psalm-return Closure(mixed, ValidationContext):bool|null |
|
68
|
|
|
* |
|
69
|
|
|
* @return Closure|null |
|
70
|
|
|
*/ |
|
71
|
59 |
|
public function getWhen(): ?Closure |
|
72
|
|
|
{ |
|
73
|
59 |
|
return $this->when; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|