-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathCallback.php
141 lines (125 loc) · 4.36 KB
/
Callback.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
declare(strict_types=1);
namespace Yiisoft\Validator\Rule;
use Attribute;
use Closure;
use InvalidArgumentException;
use ReflectionObject;
use Yiisoft\Validator\AfterInitAttributeEventInterface;
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
use Yiisoft\Validator\Rule\Trait\WhenTrait;
use Yiisoft\Validator\DumpedRuleInterface;
use Yiisoft\Validator\SkipOnEmptyInterface;
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\WhenInterface;
/**
* Defines validation options to validating the value using a callback.
*
* @see CallbackHandler
*
* @psalm-import-type SkipOnEmptyValue from SkipOnEmptyInterface
* @psalm-import-type WhenType from WhenInterface
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class Callback implements
DumpedRuleInterface,
SkipOnErrorInterface,
WhenInterface,
SkipOnEmptyInterface,
AfterInitAttributeEventInterface
{
use SkipOnEmptyTrait;
use SkipOnErrorTrait;
use WhenTrait;
/**
* @param callable|null $callback Callable with the `function ($value, $rule, $context): Result` signature that
* performs the validation. Mutually exclusive with {@see $method}.
* @param string|null $method Name of a validated object method with the `function ($value, $rule, $context): Result`
* signature that performs the validation. Mutually exclusive with {@see $callback}.
* @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty.
* See {@see SkipOnEmptyInterface}.
* @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error.
* See {@see SkipOnErrorInterface}.
* @param Closure|null $when A callable to define a condition for applying the rule.
* See {@see WhenInterface}.
*
* @psalm-param SkipOnEmptyValue $skipOnEmpty
* @psalm-param WhenType $when
*
* @throws InvalidArgumentException When neither {@see $callback} nor {@see $method} is specified or
* both are specified at the same time.
*/
public function __construct(
private mixed $callback = null,
private string|null $method = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
if ($this->callback === null && $this->method === null) {
throw new InvalidArgumentException('Either "$callback" or "$method" must be specified.');
}
if ($this->callback !== null && $this->method !== null) {
throw new InvalidArgumentException('"$callback" and "$method" are mutually exclusive.');
}
$this->skipOnEmpty = $skipOnEmpty;
}
public function getName(): string
{
return self::class;
}
/**
* Get the callable that performs validation.
*
* @return callable|null The callable that performs validation.
*
* @see $callback
*/
public function getCallback(): callable|null
{
return $this->callback;
}
/**
* Get a name of a validated object method that performs the validation.
*
* @return string|null Name of a method that performs the validation.
*
* @see $method
*/
public function getMethod(): string|null
{
return $this->method;
}
public function afterInitAttribute(object $object): void
{
if ($this->method === null) {
return;
}
$method = $this->method;
$reflection = new ReflectionObject($object);
if (!$reflection->hasMethod($method)) {
throw new InvalidArgumentException(
sprintf(
'Method "%s" does not exist in class "%s".',
$method,
$object::class,
)
);
}
/** @psalm-suppress MixedMethodCall */
$this->callback = Closure::bind(fn (mixed ...$args): mixed => $object->{$method}(...$args), $object, $object);
}
public function getOptions(): array
{
return [
'method' => $this->method,
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
'skipOnError' => $this->skipOnError,
];
}
public function getHandler(): string
{
return CallbackHandler::class;
}
}