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
|
37 |
|
public function shouldSkipOnEmpty($value): bool |
23
|
|
|
{ |
24
|
37 |
|
return ($this->skipOnEmptyCallback)($value); |
25
|
|
|
} |
26
|
|
|
|
27
|
88 |
|
protected function initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback): void |
28
|
|
|
{ |
29
|
88 |
|
$this->skipOnEmpty = $skipOnEmpty; |
|
|
|
|
30
|
88 |
|
$this->skipOnEmptyCallback = $skipOnEmptyCallback; |
|
|
|
|
31
|
|
|
|
32
|
88 |
|
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
|
88 |
|
$this->skipOnEmptyCallback = $this->skipOnEmpty === false ? new SkipOnAll() : new SkipOnNull(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function skipOnEmpty(bool $value): self |
46
|
|
|
{ |
47
|
|
|
$new = clone $this; |
48
|
|
|
$new->skipOnEmpty = $value; |
|
|
|
|
49
|
|
|
|
50
|
|
|
return $new; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function skipOnEmptyCallback(callable $value): self |
54
|
|
|
{ |
55
|
|
|
$new = clone $this; |
56
|
|
|
$new->skipOnEmptyCallback = $value; |
|
|
|
|
57
|
|
|
|
58
|
|
|
return $new; |
59
|
|
|
} |
60
|
|
|
|
61
|
34 |
|
public function shouldSkipOnError(): bool |
62
|
|
|
{ |
63
|
34 |
|
return $this->skipOnError; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @psalm-return Closure(mixed, ValidationContext):bool|null |
68
|
|
|
* |
69
|
|
|
* @return Closure|null |
70
|
|
|
*/ |
71
|
33 |
|
public function getWhen(): ?Closure |
72
|
|
|
{ |
73
|
33 |
|
return $this->when; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|