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