1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 9/16/14 |
5
|
|
|
* Time: 9:19 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\Attribute\String; |
12
|
|
|
|
13
|
|
|
use NilPortugues\Validator\Attribute\GenericAttribute; |
14
|
|
|
use NilPortugues\Validator\Validator; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class StringAttribute |
18
|
|
|
* @package NilPortugues\Validator\Attribute\StringAttribute |
19
|
|
|
*/ |
20
|
|
|
class StringAttribute extends GenericAttribute |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param string $propertyName |
24
|
|
|
* @param Validator $validator |
25
|
|
|
* @param array $errorMessages |
26
|
|
|
* @param array $functionMap |
27
|
|
|
*/ |
28
|
|
|
public function __construct($propertyName, Validator $validator, array &$errorMessages, array &$functionMap) |
29
|
|
|
{ |
30
|
|
|
parent::__construct($propertyName, $validator, $errorMessages, $functionMap); |
31
|
|
|
|
32
|
|
|
$this->addCondition(__METHOD__); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Validates alphanumeric characters from a-Z and 0-9 |
37
|
|
|
* |
38
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
39
|
|
|
*/ |
40
|
|
|
public function isAlphanumeric() |
41
|
|
|
{ |
42
|
|
|
$this->addCondition(__METHOD__); |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Validates alphanumeric characters from a-Z, white spaces and empty values. |
49
|
|
|
* |
50
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
51
|
|
|
*/ |
52
|
|
|
public function isAlpha() |
53
|
|
|
{ |
54
|
|
|
$this->addCondition(__METHOD__); |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Validates is a string is between ranges. |
61
|
|
|
* |
62
|
|
|
* @param int $min |
63
|
|
|
* @param int $max |
64
|
|
|
* @param bool $inclusive |
65
|
|
|
* |
66
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
67
|
|
|
*/ |
68
|
|
|
public function isBetween($min, $max, $inclusive = false) |
69
|
|
|
{ |
70
|
|
|
$this->addCondition(__METHOD__, [$min, $max, $inclusive], ['min' => $min, 'max' => $max]); |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Validates if a string is in a specific charset |
77
|
|
|
* |
78
|
|
|
* @param string[] $charsetNames |
79
|
|
|
* |
80
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
81
|
|
|
*/ |
82
|
|
|
public function isCharset(array $charsetNames) |
83
|
|
|
{ |
84
|
|
|
$this->addCondition(__METHOD__, [$charsetNames]); |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Validates strings that contain only consonants. |
91
|
|
|
* |
92
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
93
|
|
|
*/ |
94
|
|
|
public function isAllConsonants() |
95
|
|
|
{ |
96
|
|
|
$this->addCondition(__METHOD__); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Validates if the input is equal some value. |
103
|
|
|
* |
104
|
|
|
* @param mixed $comparedValue |
105
|
|
|
* @param bool $identical |
106
|
|
|
* |
107
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
108
|
|
|
*/ |
109
|
|
|
public function equals($comparedValue, $identical = false) |
110
|
|
|
{ |
111
|
|
|
$this->addCondition(__METHOD__, [$comparedValue, $identical]); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param $value |
118
|
|
|
* @param bool $identical |
119
|
|
|
* |
120
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
121
|
|
|
*/ |
122
|
|
|
public function contains($value, $identical = false) |
123
|
|
|
{ |
124
|
|
|
$this->addCondition(__METHOD__, [$value, $identical]); |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Only accepts control characters. |
131
|
|
|
* |
132
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
133
|
|
|
*/ |
134
|
|
|
public function isControlCharacters() |
135
|
|
|
{ |
136
|
|
|
$this->addCondition(__METHOD__); |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Validates a value that doesn't allow a-Z, but accepts empty values and whitespace. |
143
|
|
|
* |
144
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
145
|
|
|
*/ |
146
|
|
|
public function isDigit() |
147
|
|
|
{ |
148
|
|
|
$this->addCondition(__METHOD__); |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Validates if a given value is at the end of the input. |
155
|
|
|
* |
156
|
|
|
* @param string $word |
157
|
|
|
* @param bool $identical |
158
|
|
|
* |
159
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
160
|
|
|
*/ |
161
|
|
|
public function endsWith($word, $identical = false) |
162
|
|
|
{ |
163
|
|
|
$this->addCondition(__METHOD__, [$word, $identical]); |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Validates if the input is contained in a specific haystack. |
170
|
|
|
* |
171
|
|
|
* @param string $haystack |
172
|
|
|
* @param bool $identical |
173
|
|
|
* |
174
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
175
|
|
|
*/ |
176
|
|
|
public function in($haystack, $identical = false) |
177
|
|
|
{ |
178
|
|
|
$this->addCondition(__METHOD__, [$haystack, $identical]); |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Validates all characters that are graphically represented. |
185
|
|
|
* |
186
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
187
|
|
|
*/ |
188
|
|
|
public function hasGraphicalCharsOnly() |
189
|
|
|
{ |
190
|
|
|
$this->addCondition(__METHOD__); |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param integer $length |
197
|
|
|
* |
198
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
199
|
|
|
*/ |
200
|
|
|
public function hasLength($length) |
201
|
|
|
{ |
202
|
|
|
$this->addCondition(__METHOD__, [$length], ['size' => $length]); |
203
|
|
|
|
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Validates if string characters are lowercase in the input. |
209
|
|
|
* |
210
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
211
|
|
|
*/ |
212
|
|
|
public function isLowercase() |
213
|
|
|
{ |
214
|
|
|
$this->addCondition(__METHOD__); |
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Validates if the given input is not empty or in other words is input mandatory and required. |
221
|
|
|
* This function also takes whitespace into account, use noWhitespace() if no spaces or line breaks |
222
|
|
|
* and other whitespace anywhere in the input is desired |
223
|
|
|
* |
224
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
225
|
|
|
*/ |
226
|
|
|
public function notEmpty() |
227
|
|
|
{ |
228
|
|
|
$this->addCondition(__METHOD__); |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Validates if a string contains no whitespace (spaces, tabs and line breaks). |
235
|
|
|
* |
236
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
237
|
|
|
*/ |
238
|
|
|
public function noWhitespace() |
239
|
|
|
{ |
240
|
|
|
$this->addCondition(__METHOD__); |
241
|
|
|
|
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Validates all characters that are graphically printable. |
247
|
|
|
* |
248
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
249
|
|
|
*/ |
250
|
|
|
public function hasPrintableCharsOnly() |
251
|
|
|
{ |
252
|
|
|
$this->addCondition(__METHOD__); |
253
|
|
|
|
254
|
|
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Accepts only punctuation characters. |
259
|
|
|
* |
260
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
261
|
|
|
*/ |
262
|
|
|
public function isPunctuation() |
263
|
|
|
{ |
264
|
|
|
$this->addCondition(__METHOD__); |
265
|
|
|
|
266
|
|
|
return $this; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Evaluates a regex on the input and validates if matches. |
271
|
|
|
* |
272
|
|
|
* @param string $regex |
273
|
|
|
* |
274
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
275
|
|
|
*/ |
276
|
|
|
public function matchesRegex($regex) |
277
|
|
|
{ |
278
|
|
|
$this->addCondition(__METHOD__, [$regex]); |
279
|
|
|
|
280
|
|
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Validates slug-like strings. |
285
|
|
|
* |
286
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
287
|
|
|
*/ |
288
|
|
|
public function isSlug() |
289
|
|
|
{ |
290
|
|
|
$this->addCondition(__METHOD__); |
291
|
|
|
|
292
|
|
|
return $this; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param |
297
|
|
|
* |
298
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
299
|
|
|
*/ |
300
|
|
|
public function isSpace() |
301
|
|
|
{ |
302
|
|
|
$this->addCondition(__METHOD__); |
303
|
|
|
|
304
|
|
|
return $this; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* This validator validates only if the value is at the beginning of a string. |
309
|
|
|
* |
310
|
|
|
* @param mixed $word |
311
|
|
|
* @param bool $identical |
312
|
|
|
* |
313
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
314
|
|
|
*/ |
315
|
|
|
public function startsWith($word, $identical = false) |
316
|
|
|
{ |
317
|
|
|
$this->addCondition(__METHOD__, [$word, $identical]); |
318
|
|
|
|
319
|
|
|
return $this; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Validates if string characters are uppercase in the input. |
324
|
|
|
* |
325
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
326
|
|
|
*/ |
327
|
|
|
public function isUppercase() |
328
|
|
|
{ |
329
|
|
|
$this->addCondition(__METHOD__); |
330
|
|
|
|
331
|
|
|
return $this; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Validates version numbers using Semantic Versioning. Eg: 1.0.0 |
336
|
|
|
* |
337
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
338
|
|
|
*/ |
339
|
|
|
public function isVersion() |
340
|
|
|
{ |
341
|
|
|
$this->addCondition(__METHOD__); |
342
|
|
|
|
343
|
|
|
return $this; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Validates strings that contains only vowels. |
348
|
|
|
* |
349
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
350
|
|
|
*/ |
351
|
|
|
public function isVowel() |
352
|
|
|
{ |
353
|
|
|
$this->addCondition(__METHOD__); |
354
|
|
|
|
355
|
|
|
return $this; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Accepts an hexadecimal number, however, that it doesn't accept strings starting with 0x. |
360
|
|
|
* |
361
|
|
|
* @return \NilPortugues\Validator\Attribute\String\StringAttribute |
362
|
|
|
*/ |
363
|
|
|
public function isHexDigit() |
364
|
|
|
{ |
365
|
|
|
$this->addCondition(__METHOD__); |
366
|
|
|
|
367
|
|
|
return $this; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* @param int $amount |
372
|
|
|
* |
373
|
|
|
* @return $this |
374
|
|
|
*/ |
375
|
|
|
public function hasLowercase($amount = null) |
376
|
|
|
{ |
377
|
|
|
$this->addCondition(__METHOD__, [$amount]); |
378
|
|
|
|
379
|
|
|
return $this; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* @param int $amount |
384
|
|
|
* |
385
|
|
|
* @return $this |
386
|
|
|
*/ |
387
|
|
|
public function hasUppercase($amount = null) |
388
|
|
|
{ |
389
|
|
|
$this->addCondition(__METHOD__, [$amount]); |
390
|
|
|
|
391
|
|
|
return $this; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* @param int $amount |
396
|
|
|
* |
397
|
|
|
* @return $this |
398
|
|
|
*/ |
399
|
|
|
public function hasNumeric($amount = null) |
400
|
|
|
{ |
401
|
|
|
$this->addCondition(__METHOD__, [$amount]); |
402
|
|
|
|
403
|
|
|
return $this; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @param integer $amount |
408
|
|
|
* |
409
|
|
|
* @return $this |
410
|
|
|
*/ |
411
|
|
|
public function hasSpecialCharacters($amount = null) |
412
|
|
|
{ |
413
|
|
|
$this->addCondition(__METHOD__, [$amount]); |
414
|
|
|
|
415
|
|
|
return $this; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* @return $this |
420
|
|
|
*/ |
421
|
|
|
public function isEmail() |
422
|
|
|
{ |
423
|
|
|
$this->addCondition(__METHOD__); |
424
|
|
|
|
425
|
|
|
return $this; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* @return $this |
430
|
|
|
*/ |
431
|
|
|
public function isUrl() |
432
|
|
|
{ |
433
|
|
|
$this->addCondition(__METHOD__); |
434
|
|
|
|
435
|
|
|
return $this; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* @param bool $strict |
440
|
|
|
* |
441
|
|
|
* @return $this |
442
|
|
|
*/ |
443
|
|
|
public function isUUID($strict = true) |
444
|
|
|
{ |
445
|
|
|
$this->addCondition(__METHOD__, [$strict]); |
446
|
|
|
|
447
|
|
|
return $this; |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
|