1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Attribute; |
8
|
|
|
use Closure; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use TypeError; |
11
|
|
|
use Yiisoft\Validator\AttributeEventInterface; |
12
|
|
|
use Yiisoft\Validator\BeforeValidationInterface; |
13
|
|
|
use Yiisoft\Validator\DataSet\ObjectDataSet; |
14
|
|
|
use Yiisoft\Validator\DataSetInterface; |
15
|
|
|
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait; |
16
|
|
|
use Yiisoft\Validator\Rule\Trait\RuleNameTrait; |
17
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait; |
18
|
|
|
use Yiisoft\Validator\SerializableRuleInterface; |
19
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
20
|
|
|
use Yiisoft\Validator\ValidationContext; |
21
|
|
|
|
22
|
|
|
use function get_class; |
23
|
|
|
|
24
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
25
|
|
|
final class Callback implements |
26
|
|
|
SerializableRuleInterface, |
27
|
|
|
BeforeValidationInterface, |
28
|
|
|
SkipOnEmptyInterface, |
29
|
|
|
AttributeEventInterface |
30
|
|
|
{ |
31
|
|
|
use BeforeValidationTrait; |
32
|
|
|
use RuleNameTrait; |
33
|
|
|
use SkipOnEmptyTrait; |
34
|
|
|
|
35
|
9 |
|
public function __construct( |
36
|
|
|
/** |
37
|
|
|
* @var callable|null |
38
|
|
|
*/ |
39
|
|
|
private $callback = null, |
40
|
|
|
private ?string $method = null, |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var bool|callable|null |
44
|
|
|
*/ |
45
|
|
|
private $skipOnEmpty = null, |
46
|
|
|
private bool $skipOnError = false, |
47
|
|
|
/** |
48
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
49
|
|
|
*/ |
50
|
|
|
private ?Closure $when = null, |
51
|
|
|
) { |
52
|
9 |
|
if ($this->callback === null && $this->method === null) { |
53
|
|
|
throw new InvalidArgumentException('Either "$callback" or "$method" must be specified.'); |
54
|
|
|
} |
55
|
|
|
|
56
|
9 |
|
if ($this->callback !== null && $this->method !== null) { |
57
|
|
|
throw new InvalidArgumentException('"$callback" and "$method" are mutually exclusive.'); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return callable|null |
63
|
|
|
*/ |
64
|
13 |
|
public function getCallback(): ?callable |
65
|
|
|
{ |
66
|
13 |
|
return $this->callback; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getMethod(): ?string |
70
|
|
|
{ |
71
|
|
|
return $this->method; |
72
|
|
|
} |
73
|
|
|
|
74
|
4 |
|
public function afterInitAttribute(DataSetInterface $dataSet): void |
75
|
|
|
{ |
76
|
4 |
|
if (!$dataSet instanceof ObjectDataSet) { |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
try { |
81
|
4 |
|
$this->callback = Closure::fromCallable([get_class($dataSet->getObject()), $this->method]); |
82
|
3 |
|
} catch (TypeError) { |
83
|
3 |
|
throw new InvalidArgumentException('Method must exist and have public and static modifers.'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
public function getOptions(): array |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
2 |
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
91
|
2 |
|
'skipOnError' => $this->skipOnError, |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
8 |
|
public function getHandlerClassName(): string |
96
|
|
|
{ |
97
|
8 |
|
return CallbackHandler::class; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|