1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Caridea |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
8
|
|
|
* the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations under |
16
|
|
|
* the License. |
17
|
|
|
* |
18
|
|
|
* @copyright 2015-2016 LibreWorks contributors |
19
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache 2.0 License |
20
|
|
|
*/ |
21
|
|
|
namespace Caridea\Validate\Rule; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Compares string length to accepted boundaries. |
25
|
|
|
* |
26
|
|
|
* @copyright 2015-2016 LibreWorks contributors |
27
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache 2.0 License |
28
|
|
|
*/ |
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') |
52
|
|
|
{ |
53
|
4 |
|
$this->operator = $operator; |
54
|
4 |
|
$this->length = $length; |
55
|
4 |
|
$this->encoding = $encoding; |
56
|
4 |
|
} |
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 = []) |
66
|
|
|
{ |
67
|
4 |
|
if (!is_string($value)) { |
68
|
4 |
|
return ['FORMAT_ERROR']; |
69
|
|
|
} |
70
|
4 |
|
$length = mb_strlen($value, $this->encoding); |
71
|
4 |
|
switch ($this->operator) { |
72
|
4 |
|
case "lt": |
73
|
1 |
|
return $length > $this->length ? ['TOO_LONG'] : null; |
74
|
3 |
|
case "gt": |
75
|
1 |
|
return $length < $this->length ? ['TOO_SHORT'] : null; |
76
|
2 |
|
case "eq": |
77
|
1 |
|
if ($length > $this->length) { |
78
|
1 |
|
return ['TOO_LONG']; |
79
|
1 |
|
} elseif ($length < $this->length) { |
80
|
1 |
|
return ['TOO_SHORT']; |
81
|
|
|
} |
82
|
1 |
|
return null; |
83
|
1 |
|
case "bt": |
84
|
1 |
|
if ($length > $this->length[1]) { |
85
|
1 |
|
return ['TOO_LONG']; |
86
|
1 |
|
} elseif ($length < $this->length[0]) { |
87
|
1 |
|
return ['TOO_SHORT']; |
88
|
|
|
} |
89
|
1 |
|
return null; |
90
|
|
|
} |
91
|
|
|
} |
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 |
101
|
|
|
{ |
102
|
1 |
|
return new Length('lt', $length, $encoding); |
103
|
|
|
} |
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 |
113
|
|
|
{ |
114
|
1 |
|
return new Length('gt', $length, $encoding); |
115
|
|
|
} |
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 |
125
|
|
|
{ |
126
|
1 |
|
return new Length('eq', $length, $encoding); |
127
|
|
|
} |
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 |
138
|
|
|
{ |
139
|
1 |
|
$length = [$min, $max]; |
140
|
1 |
|
sort($length); |
141
|
1 |
|
return new Length('bt', $length, $encoding); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|