|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Attribute; |
|
8
|
|
|
use Closure; |
|
9
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait; |
|
10
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; |
|
11
|
|
|
use Yiisoft\Validator\Rule\Trait\WhenTrait; |
|
12
|
|
|
use Yiisoft\Validator\RuleWithOptionsInterface; |
|
13
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
|
14
|
|
|
use Yiisoft\Validator\SkipOnErrorInterface; |
|
15
|
|
|
use Yiisoft\Validator\WhenInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Defines validation options to check that the value is a string. |
|
19
|
|
|
|
|
20
|
|
|
* When you also need to check the value has a certain length, or it's a valid e-mail address, etc. use according rules |
|
21
|
|
|
* instead (without combining them). Full list of rules working with strings is available at |
|
22
|
|
|
* {@link https://github.com/yiisoft/validator/blob/master/docs/guide/en/built-in-rules.md#string-rules}. |
|
23
|
|
|
* |
|
24
|
|
|
* @see StringValueHandler |
|
25
|
|
|
* |
|
26
|
|
|
* @psalm-import-type WhenType from WhenInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
|
29
|
|
|
final class StringValue implements |
|
30
|
|
|
RuleWithOptionsInterface, |
|
31
|
|
|
SkipOnErrorInterface, |
|
32
|
|
|
WhenInterface, |
|
33
|
|
|
SkipOnEmptyInterface |
|
34
|
|
|
{ |
|
35
|
|
|
use SkipOnEmptyTrait; |
|
36
|
|
|
use SkipOnErrorTrait; |
|
37
|
|
|
use WhenTrait; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $message Error message used when the value is not a string. |
|
41
|
|
|
* |
|
42
|
|
|
* You may use the following placeholders in the message: |
|
43
|
|
|
* |
|
44
|
|
|
* - `{attribute}`: the translated label of the attribute being validated. |
|
45
|
|
|
* - `{type}`: the type of the value being validated. |
|
46
|
|
|
* @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty. |
|
47
|
|
|
* See {@see SkipOnEmptyInterface}. |
|
48
|
|
|
* @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. |
|
49
|
|
|
* See {@see SkipOnErrorInterface}. |
|
50
|
|
|
* @param Closure|null $when A callable to define a condition for applying the rule. |
|
51
|
|
|
* See {@see WhenInterface}. |
|
52
|
|
|
* |
|
53
|
|
|
* @psalm-param WhenType $when |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct( |
|
56
|
|
|
private string $message = 'The value must be a string.', |
|
57
|
|
|
private mixed $skipOnEmpty = null, |
|
58
|
|
|
private bool $skipOnError = false, |
|
59
|
|
|
private Closure|null $when = null, |
|
60
|
|
|
) { |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getName(): string |
|
64
|
|
|
{ |
|
65
|
|
|
return 'string'; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get error message used when the value is not a string. |
|
70
|
|
|
* |
|
71
|
|
|
* @return string Error message. |
|
72
|
|
|
* |
|
73
|
|
|
* @see $message |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getMessage(): string |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->message; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getOptions(): array |
|
81
|
|
|
{ |
|
82
|
|
|
return [ |
|
83
|
|
|
'message' => [ |
|
84
|
|
|
'template' => $this->message, |
|
85
|
|
|
'parameters' => [], |
|
86
|
|
|
], |
|
87
|
|
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
|
88
|
|
|
'skipOnError' => $this->skipOnError, |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getHandler(): string |
|
93
|
|
|
{ |
|
94
|
|
|
return StringValueHandler::class; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|