1 | <?php |
||
29 | class Length implements \Caridea\Validate\Rule |
||
30 | { |
||
31 | /** |
||
32 | * @var string The operator type |
||
33 | */ |
||
34 | private $operator; |
||
35 | /** |
||
36 | * @var int|int[] The length comparison |
||
37 | */ |
||
38 | private $length; |
||
39 | /** |
||
40 | * @var string The encoding to pass to `mb_strlen` |
||
41 | */ |
||
42 | private $encoding; |
||
43 | |||
44 | /** |
||
45 | * Creates a new LengthRule. |
||
46 | * |
||
47 | * @param string $operator The operator type |
||
48 | * @param int|int[] $length The length comparison |
||
49 | * @param string $encoding The encoding to pass to `mb_strlen` |
||
50 | */ |
||
51 | 4 | protected function __construct(string $operator, $length, string $encoding = 'UTF-8') |
|
57 | |||
58 | /** |
||
59 | * Validates the provided value. |
||
60 | * |
||
61 | * @param mixed $value A value to validate against the rule |
||
62 | * @param array|object $data The dataset which contains this field |
||
63 | * @return array An array of error codes or null if validation succeeded |
||
64 | */ |
||
65 | 4 | public function apply($value, $data = []) |
|
92 | |||
93 | /** |
||
94 | * Gets a rule that requires strings to be no longer than the limit. |
||
95 | * |
||
96 | * @param int $length The maximum length |
||
97 | * @param string $encoding The string encoding |
||
98 | * @return \Caridea\Validate\Rule\Length the created rule |
||
99 | */ |
||
100 | 1 | public static function max(int $length, string $encoding = 'UTF-8'): Length |
|
104 | |||
105 | /** |
||
106 | * Gets a rule that requires strings to be no shorter than the limit. |
||
107 | * |
||
108 | * @param int $length The minimum length |
||
109 | * @param string $encoding The string encoding |
||
110 | * @return \Caridea\Validate\Rule\Length the created rule |
||
111 | */ |
||
112 | 1 | public static function min(int $length, string $encoding = 'UTF-8'): Length |
|
116 | |||
117 | /** |
||
118 | * Gets a rule that requires strings to be exactly the length of the limit. |
||
119 | * |
||
120 | * @param int $length The required length |
||
121 | * @param string $encoding The string encoding |
||
122 | * @return \Caridea\Validate\Rule\Length the created rule |
||
123 | */ |
||
124 | 1 | public static function equal(int $length, string $encoding = 'UTF-8'): Length |
|
128 | |||
129 | /** |
||
130 | * Gets a rule that requires strings to have a minimum and maximum length. |
||
131 | * |
||
132 | * @param int $min The minimum length, inclusive |
||
133 | * @param int $max The maximum length, inclusive |
||
134 | * @param string $encoding The string encoding |
||
135 | * @return \Caridea\Validate\Rule\Length the created rule |
||
136 | */ |
||
137 | 1 | public static function between(int $min, int $max, string $encoding = 'UTF-8'): Length |
|
143 | } |
||
144 |