|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
4
|
|
|
* Date: 9/16/14 |
|
5
|
|
|
* Time: 10:20 PM |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace NilPortugues\Validator\Validation\String; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class StringValidation |
|
15
|
|
|
* @package NilPortugues\Validator\Validation\StringAttribute |
|
16
|
|
|
*/ |
|
17
|
|
|
class StringValidation |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @param $value |
|
21
|
|
|
* |
|
22
|
|
|
* @return bool |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function isString($value) |
|
25
|
|
|
{ |
|
26
|
|
|
return \is_string($value); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $value |
|
31
|
|
|
* |
|
32
|
|
|
* @return bool |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function isAlphanumeric($value) |
|
35
|
|
|
{ |
|
36
|
|
|
return \preg_match('/^[a-z0-9]+$/i', $value) > 0; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $value |
|
41
|
|
|
* |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function isAlpha($value) |
|
45
|
|
|
{ |
|
46
|
|
|
return \preg_match('/^[a-z]+$/i', $value) > 0; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $value |
|
51
|
|
|
* @param integer $min |
|
52
|
|
|
* @param integer $max |
|
53
|
|
|
* @param bool $inclusive |
|
54
|
|
|
* |
|
55
|
|
|
* @throws \InvalidArgumentException |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function isBetween($value, $min, $max, $inclusive = false) |
|
59
|
|
|
{ |
|
60
|
|
|
\settype($min, 'int'); |
|
61
|
|
|
\settype($max, 'int'); |
|
62
|
|
|
\settype($inclusive, 'bool'); |
|
63
|
|
|
|
|
64
|
|
|
$length = \mb_strlen($value, \mb_detect_encoding($value)); |
|
65
|
|
|
|
|
66
|
|
|
if ($min > $max) { |
|
67
|
|
|
throw new \InvalidArgumentException(\sprintf('%s cannot be less than %s for validation', $min, $max)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (false === $inclusive) { |
|
71
|
|
|
return $min < $length && $length < $max; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $min <= $length && $length <= $max; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $value |
|
79
|
|
|
* @param string $charset |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function isCharset($value, $charset) |
|
84
|
|
|
{ |
|
85
|
|
|
$available = \mb_list_encodings(); |
|
86
|
|
|
|
|
87
|
|
|
$charset = \is_array($charset) ? $charset : array($charset); |
|
88
|
|
|
|
|
89
|
|
|
$charsetList = \array_filter( |
|
90
|
|
|
$charset, |
|
91
|
|
|
function ($charsetName) use ($available) { |
|
92
|
|
|
return \in_array($charsetName, $available, true); |
|
93
|
|
|
} |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
$detectedEncoding = \mb_detect_encoding($value, $charset, true); |
|
97
|
|
|
|
|
98
|
|
|
return \in_array($detectedEncoding, $charsetList, true); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $value |
|
103
|
|
|
* |
|
104
|
|
|
* @return bool |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function isAllConsonants($value) |
|
107
|
|
|
{ |
|
108
|
|
|
return \preg_match('/^(\s|[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z])+$/', $value) > 0; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string $value |
|
113
|
|
|
* @param $contains |
|
114
|
|
|
* @param bool $identical |
|
115
|
|
|
* |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
|
|
public static function contains($value, $contains, $identical = false) |
|
119
|
|
|
{ |
|
120
|
|
|
if (false === $identical) { |
|
121
|
|
|
return false !== \mb_stripos($value, $contains, 0, \mb_detect_encoding($value)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return false !== \mb_strpos($value, $contains, 0, \mb_detect_encoding($value)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param string $value |
|
129
|
|
|
* |
|
130
|
|
|
* @return bool |
|
131
|
|
|
*/ |
|
132
|
|
|
public static function isControlCharacters($value) |
|
133
|
|
|
{ |
|
134
|
|
|
return \ctype_cntrl($value); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param $value |
|
139
|
|
|
* |
|
140
|
|
|
* @return bool |
|
141
|
|
|
*/ |
|
142
|
|
|
public static function isDigit($value) |
|
143
|
|
|
{ |
|
144
|
|
|
return \ctype_digit($value); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param string $value |
|
149
|
|
|
* @param $contains |
|
150
|
|
|
* @param bool $identical |
|
151
|
|
|
* |
|
152
|
|
|
* @return bool |
|
153
|
|
|
*/ |
|
154
|
|
|
public static function endsWith($value, $contains, $identical = false) |
|
155
|
|
|
{ |
|
156
|
|
|
$enc = \mb_detect_encoding($value); |
|
157
|
|
|
|
|
158
|
|
|
if (false === $identical) { |
|
159
|
|
|
return \mb_strripos($value, $contains, -1, $enc) === (\mb_strlen($value, $enc) - \mb_strlen($contains, $enc)); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return \mb_strrpos($value, $contains, 0, $enc) === (\mb_strlen($value, $enc) - \mb_strlen($contains, $enc)); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Validates if the input is equal some value. |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $value |
|
169
|
|
|
* @param $comparedValue |
|
170
|
|
|
* @param bool $identical |
|
171
|
|
|
* |
|
172
|
|
|
* @return bool |
|
173
|
|
|
*/ |
|
174
|
|
|
public static function equals($value, $comparedValue, $identical = false) |
|
175
|
|
|
{ |
|
176
|
|
|
if (false === $identical) { |
|
177
|
|
|
return $value == $comparedValue; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $value === $comparedValue; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param string $value |
|
185
|
|
|
* @param string $haystack |
|
186
|
|
|
* @param bool $identical |
|
187
|
|
|
* |
|
188
|
|
|
* @return bool |
|
189
|
|
|
*/ |
|
190
|
|
|
public static function in($value, $haystack, $identical = false) |
|
191
|
|
|
{ |
|
192
|
|
|
$haystack = (string) $haystack; |
|
193
|
|
|
$enc = \mb_detect_encoding($value); |
|
194
|
|
|
|
|
195
|
|
|
if (false === $identical) { |
|
196
|
|
|
return (false !== \mb_stripos($haystack, $value, 0, $enc)); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return (false !== \mb_strpos($haystack, $value, 0, $enc)); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param string $value |
|
204
|
|
|
* |
|
205
|
|
|
* @return bool |
|
206
|
|
|
*/ |
|
207
|
|
|
public static function hasGraphicalCharsOnly($value) |
|
208
|
|
|
{ |
|
209
|
|
|
return \ctype_graph($value); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param string $value |
|
214
|
|
|
* @param integer $length |
|
215
|
|
|
* |
|
216
|
|
|
* @return bool |
|
217
|
|
|
*/ |
|
218
|
|
|
public static function hasLength($value, $length) |
|
219
|
|
|
{ |
|
220
|
|
|
\settype($length, 'int'); |
|
221
|
|
|
|
|
222
|
|
|
return \mb_strlen($value, \mb_detect_encoding($value)) === $length; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @param string $value |
|
227
|
|
|
* |
|
228
|
|
|
* @return bool |
|
229
|
|
|
*/ |
|
230
|
|
|
public static function isLowercase($value) |
|
231
|
|
|
{ |
|
232
|
|
|
return $value === \mb_strtolower($value, \mb_detect_encoding($value)); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @param string $value |
|
237
|
|
|
* |
|
238
|
|
|
* @return bool |
|
239
|
|
|
*/ |
|
240
|
|
|
public static function notEmpty($value) |
|
241
|
|
|
{ |
|
242
|
|
|
$value = \trim($value); |
|
243
|
|
|
|
|
244
|
|
|
return !empty($value); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @param string $value |
|
249
|
|
|
* |
|
250
|
|
|
* @return bool |
|
251
|
|
|
*/ |
|
252
|
|
|
public static function noWhitespace($value) |
|
253
|
|
|
{ |
|
254
|
|
|
return 0 === \preg_match('/\s/', $value); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* @param string $value |
|
259
|
|
|
* |
|
260
|
|
|
* @return bool |
|
261
|
|
|
*/ |
|
262
|
|
|
public static function hasPrintableCharsOnly($value) |
|
263
|
|
|
{ |
|
264
|
|
|
return \ctype_print($value); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* @param string $value |
|
269
|
|
|
* |
|
270
|
|
|
* @return bool |
|
271
|
|
|
*/ |
|
272
|
|
|
public static function isPunctuation($value) |
|
273
|
|
|
{ |
|
274
|
|
|
return \ctype_punct($value); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* @param string $value |
|
279
|
|
|
* @param string $regex |
|
280
|
|
|
* |
|
281
|
|
|
* @return bool |
|
282
|
|
|
*/ |
|
283
|
|
|
public static function matchesRegex($value, $regex) |
|
284
|
|
|
{ |
|
285
|
|
|
return \preg_match($regex, $value) > 0; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @param string $value |
|
290
|
|
|
* |
|
291
|
|
|
* @return bool |
|
292
|
|
|
*/ |
|
293
|
|
|
public static function isSlug($value) |
|
294
|
|
|
{ |
|
295
|
|
|
if ((false !== \strstr($value, '--')) |
|
296
|
|
|
|| (!preg_match('@^[0-9a-z\-]+$@', $value)) |
|
297
|
|
|
|| (\preg_match('@^-|-$@', $value)) |
|
298
|
|
|
) { |
|
299
|
|
|
return false; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
return true; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* @param string $value |
|
307
|
|
|
* |
|
308
|
|
|
* @return bool |
|
309
|
|
|
*/ |
|
310
|
|
|
public static function isSpace($value) |
|
311
|
|
|
{ |
|
312
|
|
|
return \ctype_space($value); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* @param string $value |
|
317
|
|
|
* @param $contains |
|
318
|
|
|
* @param bool $identical |
|
319
|
|
|
* |
|
320
|
|
|
* @return bool |
|
321
|
|
|
*/ |
|
322
|
|
|
public static function startsWith($value, $contains, $identical = false) |
|
323
|
|
|
{ |
|
324
|
|
|
$enc = \mb_detect_encoding($value); |
|
325
|
|
|
|
|
326
|
|
|
if (false === $identical) { |
|
327
|
|
|
return 0 === \mb_stripos($value, $contains, 0, $enc); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
return 0 === \mb_strpos($value, $contains, 0, $enc); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* @param string $value |
|
335
|
|
|
* |
|
336
|
|
|
* @return bool |
|
337
|
|
|
*/ |
|
338
|
|
|
public static function isUppercase($value) |
|
339
|
|
|
{ |
|
340
|
|
|
return $value === \mb_strtoupper($value, \mb_detect_encoding($value)); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* @param string $value |
|
345
|
|
|
* |
|
346
|
|
|
* @return bool |
|
347
|
|
|
*/ |
|
348
|
|
|
public static function isVersion($value) |
|
349
|
|
|
{ |
|
350
|
|
|
return \preg_match('/^[0-9]+\.[0-9]+(\.[0-9]*)?([+-][^+-][0-9A-Za-z-.]*)?$/', $value) > 0; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* @param string $value |
|
355
|
|
|
* |
|
356
|
|
|
* @return bool |
|
357
|
|
|
*/ |
|
358
|
|
|
public static function isVowel($value) |
|
359
|
|
|
{ |
|
360
|
|
|
return \preg_match('/^(\s|[aeiouAEIOU])*$/', $value) > 0; |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* @param string $value |
|
365
|
|
|
* |
|
366
|
|
|
* @return bool |
|
367
|
|
|
*/ |
|
368
|
|
|
public static function isHexDigit($value) |
|
369
|
|
|
{ |
|
370
|
|
|
return \ctype_xdigit($value); |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
/** |
|
374
|
|
|
* @param string $value |
|
375
|
|
|
* @param int $amount |
|
376
|
|
|
* |
|
377
|
|
|
* @return bool |
|
378
|
|
|
*/ |
|
379
|
|
|
public static function hasLowercase($value, $amount = null) |
|
380
|
|
|
{ |
|
381
|
|
|
return self::hasStringSubset($value, $amount, '/[a-z]/'); |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
/** |
|
385
|
|
|
* @param string $value |
|
386
|
|
|
* @param integer|null $amount |
|
387
|
|
|
* @param string $regex |
|
388
|
|
|
* |
|
389
|
|
|
* @return bool |
|
390
|
|
|
*/ |
|
391
|
|
|
private static function hasStringSubset($value, $amount, $regex) |
|
392
|
|
|
{ |
|
393
|
|
|
\settype($value, 'string'); |
|
394
|
|
|
|
|
395
|
|
|
$minMatches = 1; |
|
396
|
|
|
if (!empty($amount)) { |
|
397
|
|
|
$minMatches = $amount; |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
$value = \preg_replace('/\s+/', '', $value); |
|
401
|
|
|
$length = \strlen($value); |
|
402
|
|
|
|
|
403
|
|
|
$counter = 0; |
|
404
|
|
|
for ($i = 0; $i < $length; $i++) { |
|
405
|
|
|
if (\preg_match($regex, $value[$i]) > 0) { |
|
406
|
|
|
$counter++; |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
if ($counter === $minMatches) { |
|
410
|
|
|
return true; |
|
411
|
|
|
} |
|
412
|
|
|
} |
|
413
|
|
|
|
|
414
|
|
|
return false; |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
/** |
|
418
|
|
|
* @param string $value |
|
419
|
|
|
* @param int $amount |
|
420
|
|
|
* |
|
421
|
|
|
* @return bool |
|
422
|
|
|
*/ |
|
423
|
|
|
public static function hasUppercase($value, $amount = null) |
|
424
|
|
|
{ |
|
425
|
|
|
return self::hasStringSubset($value, $amount, '/[A-Z]/'); |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
/** |
|
429
|
|
|
* @param string $value |
|
430
|
|
|
* @param int $amount |
|
431
|
|
|
* |
|
432
|
|
|
* @return bool |
|
433
|
|
|
*/ |
|
434
|
|
|
public static function hasNumeric($value, $amount = null) |
|
435
|
|
|
{ |
|
436
|
|
|
return self::hasStringSubset($value, $amount, '/[0-9]/'); |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
/** |
|
440
|
|
|
* @param string $value |
|
441
|
|
|
* @param int $amount |
|
442
|
|
|
* |
|
443
|
|
|
* @return bool |
|
444
|
|
|
*/ |
|
445
|
|
|
public static function hasSpecialCharacters($value, $amount = null) |
|
446
|
|
|
{ |
|
447
|
|
|
return self::hasStringSubset($value, $amount, '/[^a-zA-Z\d\s]/'); |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
/** |
|
451
|
|
|
* @param string $value |
|
452
|
|
|
* |
|
453
|
|
|
* @return bool |
|
454
|
|
|
*/ |
|
455
|
|
|
public static function isEmail($value) |
|
456
|
|
|
{ |
|
457
|
|
|
\settype($value, 'string'); |
|
458
|
|
|
|
|
459
|
|
|
return \preg_match('/^[A-Z0-9._%\-+]+@(?:[A-Z0-9\-]+\.)+(?:[A-Z0-9\-]+)$/i', $value) > 0; |
|
460
|
|
|
} |
|
461
|
|
|
|
|
462
|
|
|
/** |
|
463
|
|
|
* @param string $value |
|
464
|
|
|
* |
|
465
|
|
|
* @return boolean |
|
466
|
|
|
*/ |
|
467
|
|
|
public static function isUrl($value) |
|
468
|
|
|
{ |
|
469
|
|
|
if (\strlen($value) > 0 && $value[0] == $value[1] && $value[0] == "/") { |
|
470
|
|
|
$value = 'http:'.$value; |
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
|
|
return false !== \filter_var($value, FILTER_VALIDATE_URL, ['options' => ['flags' => FILTER_FLAG_PATH_REQUIRED]]); |
|
474
|
|
|
} |
|
475
|
|
|
|
|
476
|
|
|
/** |
|
477
|
|
|
* @param string $value |
|
478
|
|
|
* @param bool $strict |
|
479
|
|
|
* |
|
480
|
|
|
* @return bool |
|
481
|
|
|
*/ |
|
482
|
|
|
public static function isUUID($value, $strict = true) |
|
483
|
|
|
{ |
|
484
|
|
|
\settype($value, 'string'); |
|
485
|
|
|
|
|
486
|
|
|
$pattern = '/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/i'; |
|
487
|
|
|
if (true !== $strict) { |
|
488
|
|
|
$value = \trim($value, '[]{}'); |
|
489
|
|
|
$pattern = '/^[a-f0-9]{4}(?:-?[a-f0-9]{4}){7}$/i'; |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
|
|
return \preg_match($pattern, $value) > 0; |
|
493
|
|
|
} |
|
494
|
|
|
} |
|
495
|
|
|
|