1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Validator\DataSetInterface; |
8
|
|
|
use Yiisoft\Validator\ErrorMessage; |
9
|
|
|
use Yiisoft\Validator\HasValidationErrorMessage; |
10
|
|
|
use Yiisoft\Validator\Result; |
11
|
|
|
use Yiisoft\Validator\Rule; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* StringValidator validates that the attribute value is of certain length. |
15
|
|
|
* |
16
|
|
|
* Note, this validator should only be used with string-typed attributes. |
17
|
|
|
*/ |
18
|
|
|
class HasLength extends Rule |
19
|
|
|
{ |
20
|
|
|
use HasValidationErrorMessage; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int|null maximum length. null means no maximum length limit. |
24
|
|
|
* |
25
|
|
|
* @see tooLongMessage for the customized message for a too long string. |
26
|
|
|
*/ |
27
|
|
|
private ?int $max = null; |
28
|
|
|
/** |
29
|
|
|
* @var int|null minimum length. null means no minimum length limit. |
30
|
|
|
* |
31
|
|
|
* @see tooShortMessage for the customized message for a too short string. |
32
|
|
|
*/ |
33
|
|
|
private ?int $min = null; |
34
|
|
|
/** |
35
|
|
|
* @var string user-defined error message used when the value is not a string. |
36
|
|
|
*/ |
37
|
|
|
private string $message = 'This value must be a string.'; |
38
|
|
|
/** |
39
|
|
|
* @var string user-defined error message used when the length of the value is smaller than {@see $min}. |
40
|
|
|
*/ |
41
|
|
|
private string $tooShortMessage = 'This value should contain at least {min, number} {min, plural, one{character} other{characters}}.'; |
42
|
|
|
/** |
43
|
|
|
* @var string user-defined error message used when the length of the value is greater than {@see $max}. |
44
|
|
|
*/ |
45
|
|
|
private string $tooLongMessage = 'This value should contain at most {max, number} {max, plural, one{character} other{characters}}.'; |
46
|
|
|
/** |
47
|
|
|
* @var string the encoding of the string value to be validated (e.g. 'UTF-8'). |
48
|
|
|
* If this property is not set, application wide encoding will be used. |
49
|
|
|
*/ |
50
|
|
|
protected string $encoding = 'UTF-8'; |
51
|
|
|
|
52
|
6 |
|
protected function validateValue($value, DataSetInterface $dataSet = null): Result |
53
|
|
|
{ |
54
|
6 |
|
$result = new Result(); |
55
|
|
|
|
56
|
6 |
|
if (!is_string($value)) { |
57
|
2 |
|
$result->addError(new ErrorMessage($this->message)); |
58
|
2 |
|
return $result; |
59
|
|
|
} |
60
|
|
|
|
61
|
6 |
|
$length = mb_strlen($value, $this->encoding); |
62
|
|
|
|
63
|
6 |
|
if ($this->min !== null && $length < $this->min) { |
64
|
3 |
|
$result->addError(new ErrorMessage($this->tooShortMessage, ['min' => $this->min])); |
65
|
|
|
} |
66
|
6 |
|
if ($this->max !== null && $length > $this->max) { |
67
|
4 |
|
$result->addError(new ErrorMessage($this->tooLongMessage, ['max' => $this->max])); |
68
|
|
|
} |
69
|
|
|
|
70
|
6 |
|
return $result; |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
public function min(int $value): self |
74
|
|
|
{ |
75
|
3 |
|
$new = clone $this; |
76
|
3 |
|
$new->min = $value; |
77
|
3 |
|
return $new; |
78
|
|
|
} |
79
|
|
|
|
80
|
9 |
|
public function max(int $value): self |
81
|
|
|
{ |
82
|
9 |
|
$new = clone $this; |
83
|
9 |
|
$new->max = $value; |
84
|
9 |
|
return $new; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function encoding(string $encoding): self |
88
|
|
|
{ |
89
|
|
|
$new = clone $this; |
90
|
|
|
$new->encoding = $encoding; |
91
|
|
|
return $new; |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
public function tooShortMessage(string $message): self |
95
|
|
|
{ |
96
|
1 |
|
$new = clone $this; |
97
|
1 |
|
$new->tooShortMessage = $message; |
98
|
1 |
|
return $new; |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
public function tooLongMessage(string $message): self |
102
|
|
|
{ |
103
|
1 |
|
$new = clone $this; |
104
|
1 |
|
$new->tooLongMessage = $message; |
105
|
1 |
|
return $new; |
106
|
|
|
} |
107
|
|
|
|
108
|
7 |
|
public function getRawOptions(): array |
109
|
|
|
{ |
110
|
7 |
|
return array_merge( |
111
|
7 |
|
parent::getRawOptions(), |
112
|
|
|
[ |
113
|
7 |
|
'message' => new ErrorMessage($this->message), |
114
|
7 |
|
'min' => $this->min, |
115
|
7 |
|
'tooShortMessage' => new ErrorMessage($this->tooShortMessage, ['min' => $this->min]), |
116
|
7 |
|
'max' => $this->max, |
117
|
7 |
|
'tooLongMessage' => new ErrorMessage($this->tooLongMessage, ['max' => $this->max]), |
118
|
7 |
|
'encoding' => $this->encoding, |
119
|
|
|
], |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|