|
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 ReflectionObject; |
|
11
|
|
|
use Yiisoft\Validator\AfterInitAttributeEventInterface; |
|
12
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait; |
|
13
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; |
|
14
|
|
|
use Yiisoft\Validator\Rule\Trait\WhenTrait; |
|
15
|
|
|
use Yiisoft\Validator\RuleWithOptionsInterface; |
|
16
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
|
17
|
|
|
use Yiisoft\Validator\SkipOnErrorInterface; |
|
18
|
|
|
use Yiisoft\Validator\WhenInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @psalm-import-type WhenType from WhenInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
|
24
|
|
|
final class Callback implements |
|
25
|
|
|
RuleWithOptionsInterface, |
|
26
|
|
|
SkipOnErrorInterface, |
|
27
|
|
|
WhenInterface, |
|
28
|
|
|
SkipOnEmptyInterface, |
|
29
|
|
|
AfterInitAttributeEventInterface |
|
30
|
|
|
{ |
|
31
|
|
|
use SkipOnEmptyTrait; |
|
32
|
|
|
use SkipOnErrorTrait; |
|
33
|
16 |
|
use WhenTrait; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct( |
|
36
|
|
|
/** |
|
37
|
|
|
* @var callable|null |
|
38
|
|
|
*/ |
|
39
|
|
|
private $callback = null, |
|
40
|
|
|
private string|null $method = null, |
|
41
|
|
|
|
|
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var bool|callable|null |
|
44
|
|
|
*/ |
|
45
|
|
|
private $skipOnEmpty = null, |
|
46
|
|
|
private bool $skipOnError = false, |
|
47
|
|
|
/** |
|
48
|
|
|
* @var WhenType |
|
49
|
|
|
*/ |
|
50
|
16 |
|
private Closure|null $when = null, |
|
51
|
1 |
|
) { |
|
52
|
|
|
if ($this->callback === null && $this->method === null) { |
|
53
|
|
|
throw new InvalidArgumentException('Either "$callback" or "$method" must be specified.'); |
|
54
|
15 |
|
} |
|
55
|
1 |
|
|
|
56
|
|
|
if ($this->callback !== null && $this->method !== null) { |
|
57
|
|
|
throw new InvalidArgumentException('"$callback" and "$method" are mutually exclusive.'); |
|
58
|
|
|
} |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
public function getName(): string |
|
62
|
|
|
{ |
|
63
|
|
|
return 'callback'; |
|
64
|
17 |
|
} |
|
65
|
|
|
|
|
66
|
17 |
|
public function getCallback(): callable|null |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->callback; |
|
|
|
|
|
|
69
|
2 |
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
public function getMethod(): string|null |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->method; |
|
74
|
4 |
|
} |
|
75
|
|
|
|
|
76
|
4 |
|
public function afterInitAttribute(object $object, int $target): void |
|
77
|
1 |
|
{ |
|
78
|
|
|
if ($this->method === null) { |
|
79
|
|
|
return; |
|
80
|
3 |
|
} |
|
81
|
|
|
|
|
82
|
3 |
|
$method = $this->method; |
|
83
|
3 |
|
|
|
84
|
1 |
|
$reflection = new ReflectionObject($object); |
|
85
|
|
|
if (!$reflection->hasMethod($method)) { |
|
86
|
|
|
throw new InvalidArgumentException( |
|
87
|
|
|
sprintf( |
|
88
|
|
|
'Method "%s" does not exist in class "%s".', |
|
89
|
|
|
$method, |
|
90
|
|
|
$object::class, |
|
91
|
|
|
) |
|
92
|
2 |
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
3 |
|
/** @psalm-suppress MixedMethodCall */ |
|
96
|
|
|
$this->callback = Closure::bind(fn (mixed ...$args): mixed => $object->{$method}(...$args), $object, $object); |
|
97
|
|
|
} |
|
98
|
3 |
|
|
|
99
|
3 |
|
public function getOptions(): array |
|
100
|
3 |
|
{ |
|
101
|
|
|
return [ |
|
102
|
|
|
'method' => $this->method, |
|
103
|
|
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
|
104
|
16 |
|
'skipOnError' => $this->skipOnError, |
|
105
|
|
|
]; |
|
106
|
16 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getHandlerClassName(): string |
|
109
|
|
|
{ |
|
110
|
|
|
return CallbackHandler::class; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|