forked from rafaelwendel/DataValidator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataValidator.php
821 lines (704 loc) · 26.8 KB
/
DataValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
<?php
/**
* Class for validation of data
*
* @author Rafael Wendel Pinheiro (http://www.rafaelwendel.com)
* @version 1.0
* @link https://github.com/rafaelwendel/DataValidator/
*/
class Data_Validator {
protected $_data = array();
protected $_errors = array();
protected $_pattern = array();
protected $_messages = array();
/**
* Construct method (Set the error messages default)
* @access public
* @return void
*/
public function __construct() {
$this->set_messages_default();
$this->define_pattern();
}
/**
* Set a data for validate
* @access public
* @param $name String The name of data
* @param $value Mixed The value of data
* @return Data_Validator The self instance
*/
public function set($name, $value, $custom_name = null){
$this->_data['name'] = $name;
$this->_data['value'] = $value;
$this->_data['custom_name']= (isset($custom_name)) ? $custom_name : $name;
return $this;
}
/**
* Set error messages default born in the class
* @access protected
* @return void
*/
protected function set_messages_default(){
$this->_messages = array(
'is_required' => 'O campo %s é obrigatório',
'min_length' => 'O campo %s deve conter ao mínimo %s caracter(es)',
'max_length' => 'O campo %s deve conter ao máximo %s caracter(es)',
'between_length' => 'O campo %s deve conter entre %s e %s caracter(es)',
'min_value' => 'O valor do campo %s deve ser maior que %s ',
'max_value' => 'O valor do campo %s deve ser menor que %s ',
'between_values' => 'O valor do campo %s deve estar entre %s e %s',
'is_email' => 'O email %s não é válido ',
'is_url' => 'A URL %s não é válida ',
'is_slug' => '%s não é um slug ',
'is_num' => 'O valor %s não é numérico ',
'is_integer' => 'O valor %s não é inteiro ',
'is_float' => 'O valor %s não é float ',
'is_string' => 'O valor %s não é String ',
'is_boolean' => 'O valor %s não é booleano ',
'is_obj' => 'A variável %s não é um objeto ',
'is_instance_of' => '%s não é uma instância de %s ',
'is_arr' => 'A variável %s não é um array ',
'is_directory' => '%s não é um diretório válido ',
'is_equals' => 'O valor do campo %s deve ser igual à %s ',
'is_not_equals' => 'O valor do campo %s não deve ser igual à %s ',
'is_cpf' => 'O valor %s não é um CPF válido ',
'is_cnpj' => 'O valor %s não é um CNPJ válido ',
'contains' => 'O campo %s só aceita um do(s) seguinte(s) valore(s): [%s] ',
'not_contains' => 'O campo %s não aceita o(s) seguinte(s) valore(s): [%s] ',
'is_lowercase' => 'O campo %s só aceita caracteres minúsculos ',
'is_uppercase' => 'O campo %s só aceita caracteres maiúsculos ',
'is_multiple' => 'O valor %s não é múltiplo de %s',
'is_positive' => 'O campo %s só aceita valores positivos',
'is_negative' => 'O campo %s só aceita valores negativos',
'is_date' => 'A data %s não é válida',
'is_alpha' => 'O campo %s só aceita caracteres alfabéticos',
'is_alpha_num' => 'O campo %s só aceita caracteres alfanuméricos',
'no_whitespaces' => 'O campo %s não aceita espaços em branco',
'is_phone' => 'O campo %s não é um telefone válido',
'is_zipCode' => 'O campo %s não é um CEP válido',
'is_plate' => 'O campo $s não é válido',
'is_ip' => 'O campo $s não é um ip válido'
);
}
/**
* The number of validators methods available in DataValidator
* @access public
* @return int Number of validators methods
*/
public function get_number_validators_methods(){
return count($this->_messages);
}
/**
* Define a custom error message for some method
* @access public
* @param String $name The name of the method
* @param String $value The custom message
* @return void
*/
public function set_message($name, $value){
if (array_key_exists($name, $this->_messages)){
$this->_messages[$name] = $value;
}
}
/**
* Get the error messages
* @access public
* @param String $param [optional] A specific method
* @return Mixed One array with all error messages or a message of specific method
*/
public function get_messages($param = false){
if ($param){
return $this->_messages[$param];
}
return $this->_messages;
}
/**
* Define the pattern of name of error messages
* @access public
* @param String $prefix [optional] The prefix of name
* @param String $sufix [optional] The sufix of name
* @return void
*/
public function define_pattern($prefix = '', $sufix = ''){
$this->_pattern['prefix'] = $prefix;
$this->_pattern['sufix'] = $sufix;
}
/**
* Set a error of the invalid data
* @access protected
* @param String $error The error message
* @return void
*/
protected function set_error($error){
$this->_errors[$this->_pattern['prefix'] . $this->_data['name'] . $this->_pattern['sufix']][] = $error;
}
/**
* Verify if the current data is not null
* @access public
* @return Data_Validator The self instance
*/
public function is_required(){
//var_dump($_data);
if (empty ($this->_data['value'])){
$this->set_error(sprintf($this->_messages['is_required'], $this->_data['custom_name']));
}
return $this;
}
/**
* Verify if the length of current value is less than the parameter
* @access public
* @param Int $length The value for compare
* @param Boolean $inclusive [optional] Include the lenght in the comparison
* @return Data_Validator The self instance
*/
public function min_length($length, $inclusive = false){
$verify = ($inclusive === true ? strlen($this->_data['value']) >= $length : strlen($this->_data['value']) > $length);
if (!$verify){
$this->set_error(sprintf($this->_messages['min_length'], $this->_data['custom_name'], $length));
}
return $this;
}
/**
* Verify if the length of current value is more than the parameter
* @access public
* @param Int $length The value for compare
* @param Boolean $inclusive [optional] Include the lenght in the comparison
* @return Data_Validator The self instance
*/
public function max_length($length, $inclusive = false){
$verify = ($inclusive === true ? strlen($this->_data['value']) <= $length : strlen($this->_data['value']) < $length);
if (!$verify){
$this->set_error(sprintf($this->_messages['max_length'], $this->_data['custom_name'], $length));
}
return $this;
}
/**
* Verify if the length current value is between than the parameters
* @access public
* @param Int $min The minimum value for compare
* @param Int $max The maximum value for compare
* @return Data_Validator The self instance
*/
public function between_length($min, $max){
if(strlen($this->_data['value']) < $min || strlen($this->_data['value']) > $max){
$this->set_error(sprintf($this->_messages['between_length'], $this->_data['custom_name'], $min, $max));
}
return $this;
}
/**
* Verify if the current value is less than the parameter
* @access public
* @param Int $value The value for compare
* @param Boolean $inclusive [optional] Include the value in the comparison
* @return Data_Validator The self instance
*/
public function min_value($value, $inclusive = false){
$verify = ($inclusive === true ? !is_numeric($this->_data['value']) || $this->_data['value'] >= $value : !is_numeric($this->_data['value']) || $this->_data['value'] > $value);
if (!$verify){
$this->set_error(sprintf($this->_messages['min_value'], $this->_data['custom_name'], $value));
}
return $this;
}
/**
* Verify if the current value is more than the parameter
* @access public
* @param Int $value The value for compare
* @param Boolean $inclusive [optional] Include the value in the comparison
* @return Data_Validator The self instance
*/
public function max_value($value, $inclusive = false){
$verify = ($inclusive === true ? !is_numeric($this->_data['value']) || $this->_data['value'] <= $value : !is_numeric($this->_data['value']) || $this->_data['value'] < $value);
if (!$verify){
$this->set_error(sprintf($this->_messages['max_value'], $this->_data['custom_name'], $value));
}
return $this;
}
/**
* Verify if the length of current value is more than the parameter
* @access public
* @param Int $min_value The minimum value for compare
* @param Int $max_value The maximum value for compare
* @return Data_Validator The self instance
*/
public function between_values($min_value, $max_value){
if(!is_numeric($this->_data['value']) || (($this->_data['value'] < $min_value || $this->_data['value'] > $max_value ))){
$this->set_error(sprintf($this->_messages['between_values'], $this->_data['custom_name'], $min_value, $max_value));
}
return $this;
}
/**
* Verify if the current data is a valid email
* @access public
* @return Data_Validator The self instance
*/
public function is_email(){
if (filter_var($this->_data['value'], FILTER_VALIDATE_EMAIL) === false) {
$this->set_error(sprintf($this->_messages['is_email'], $this->_data['value']));
}
return $this;
}
/**
* Verify if the current data is a valid URL
* @access public
* @return Data_Validator The self instance
*/
public function is_url(){
if (filter_var($this->_data['value'], FILTER_VALIDATE_URL) === false) {
$this->set_error(sprintf($this->_messages['is_url'], $this->_data['value']));
}
return $this;
}
/**
* Verify if the current data is a slug
* @access public
* @return Data_Validator The self instance
*/
public function is_slug(){
$verify = true;
if (strstr($input, '--')) {
$verify = false;
}
if (!preg_match('@^[0-9a-z\-]+$@', $input)) {
$verify = false;
}
if (preg_match('@^-|-$@', $input)){
$verify = false;
}
if(!$verify){
$this->set_error(sprintf($this->_messages['is_slug'], $this->_data['value']));
}
return $this;
}
/**
* Verify if the current data is a numeric value
* @access public
* @return Data_Validator The self instance
*/
public function is_num(){
if (!is_numeric($this->_data['value'])){
$this->set_error(sprintf($this->_messages['is_num'], $this->_data['value']));
}
return $this;
}
/**
* Verify if the current data is a integer value
* @access public
* @return Data_Validator The self instance
*/
public function is_integer(){
if (!is_numeric($this->_data['value']) && (int) $this->_data['value'] != $this->_data['value']){
$this->set_error(sprintf($this->_messages['is_integer'], $this->_data['value']));
}
return $this;
}
/**
* Verify if the current data is a float value
* @access public
* @return Data_Validator The self instance
*/
public function is_float(){
if (!is_float(filter_var($this->_data['value'], FILTER_VALIDATE_FLOAT))){
$this->set_error(sprintf($this->_messages['is_float'], $this->_data['value']));
}
return $this;
}
/**
* Verify if the current data is a string value
* @access public
* @return Data_Validator The self instance
*/
public function is_string(){
if(!is_string($this->_data['value'])){
$this->set_error(sprintf($this->_messages['is_string'], $this->_data['value']));
}
return $this;
}
/**
* Verify if the current data is a boolean value
* @access public
* @return Data_Validator The self instance
*/
public function is_boolean(){
if(!is_bool($this->_data['value'])){
$this->set_error(sprintf($this->_messages['is_boolean'], $this->_data['value']));
}
return $this;
}
/**
* Verify if the current data is a object
* @access public
* @return Data_Validator The self instance
*/
public function is_obj(){
if(!is_object($this->_data['value'])){
$this->set_error(sprintf($this->_messages['is_obj'], $this->_data['custom_name']));
}
return $this;
}
/**
* Verify if the current data is a instance of the determinate class
* @access public
* @param String $class The class for compare
* @return Data_Validator The self instance
*/
public function is_instance_of($class){
if(!($this->_data['value'] instanceof $class)){
$this->set_error(sprintf($this->_messages['is_instance_of'], $this->_data['custom_name'], $class));
}
return $this;
}
/**
* Verify if the current data is a array
* @access public
* @return Data_Validator The self instance
*/
public function is_arr(){
if(!is_array($this->_data['value'])){
$this->set_error(sprintf($this->_messages['is_arr'], $this->_data['custom_name']));
}
return $this;
}
/**
* Verify if the current parameter it is a directory
* @access public
* @return Data_Validator The self instance
*/
public function is_directory(){
$verify = is_string($this->_data['value']) && is_dir($this->_data['value']);
if(!$verify){
$this->set_error(sprintf($this->_messages['is_directory'], $this->_data['value']));
}
return $this;
}
/**
* Verify if the current data is equals than the parameter
* @access public
* @param String $value The value for compare
* @param Boolean $identical [optional] Identical?
* @return Data_Validator The self instance
*/
public function is_equals($value, $identical = false){
$verify = ($identical === true ? $this->_data['value'] === $value : strtolower($this->_data['value']) == strtolower($value));
if(!$verify){
$this->set_error(sprintf($this->_messages['is_equals'], $this->_data['custom_name'], $value));
}
return $this;
}
/**
* Verify if the current data is not equals than the parameter
* @access public
* @param String $value The value for compare
* @param Boolean $identical [optional] Identical?
* @return Data_Validator The self instance
*/
public function is_not_equals($value, $identical = false){
$verify = ($identical === true ? $this->_data['value'] !== $value : strtolower($this->_data['value']) != strtolower($value));
if(!$verify){
$this->set_error(sprintf($this->_messages['is_not_equals'], $this->_data['custom_name'], $value));
}
return $this;
}
/**
* Verify if the current data is a valid CPF
* @access public
* @return Data_Validator The self instance
*/
public function is_cpf(){
$verify = true;
$c = preg_replace('/\D/', '', $this->_data['value']);
if (strlen($c) != 11)
$verify = false;
if (preg_match("/^{$c[0]}{11}$/", $c))
$verify = false;
for ($s = 10, $n = 0, $i = 0; $s >= 2; $n += $c[$i++] * $s--);
if ($c[9] != ((($n %= 11) < 2) ? 0 : 11 - $n))
$verify = false;
for ($s = 11, $n = 0, $i = 0; $s >= 2; $n += $c[$i++] * $s--);
if ($c[10] != ((($n %= 11) < 2) ? 0 : 11 - $n))
$verify = false;
if(!$verify){
$this->set_error(sprintf($this->_messages['is_cpf'], $this->_data['value']));
}
return $this;
}
/**
* Verify if the current data is a valid CNPJ
* @access public
* @return Data_Validator The self instance
*/
public function is_cnpj(){
$verify = true;
$c = preg_replace('/\D/', '', $this->_data['value']);
$b = array(6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2);
if (strlen($c) != 14)
$verify = false;
for ($i = 0, $n = 0; $i < 12; $n += $c[$i] * $b[++$i]);
if ($c[12] != ((($n %= 11) < 2) ? 0 : 11 - $n))
$verify = false;
for ($i = 0, $n = 0; $i <= 12; $n += $c[$i] * $b[$i++]);
if ($c[13] != ((($n %= 11) < 2) ? 0 : 11 - $n))
$verify = false;
if(!$verify){
$this->set_error(sprintf($this->_messages['is_cnpj'], $this->_data['value']));
}
return $this;
}
/**
* Verify if the current data contains in the parameter
* @access public
* @param Mixed $values One array or String with valids values
* @param Mixed $separator [optional] If $values as a String, pass the separator of values (ex: , - | )
* @return Data_Validator The self instance
*/
public function contains($values, $separator = false){
if(!is_array($values)){
if(!$separator || is_null($values)){
$values = array();
}
else{
$values = explode($separator, $values);
}
}
if(!in_array($this->_data['value'], $values)){
$this->set_error(sprintf($this->_messages['contains'], $this->_data['custom_name'], implode(', ', $values)));
}
return $this;
}
/**
* Verify if the current data not contains in the parameter
* @access public
* @param Mixed $values One array or String with valids values
* @param Mixed $separator [optional] If $values as a String, pass the separator of values (ex: , - | )
* @return Data_Validator The self instance
*/
public function not_contains($values, $separator = false){
if(!is_array($values)){
if(!$separator || is_null($values)){
$values = array();
}
else{
$values = explode($separator, $values);
}
}
if(in_array($this->_data['value'], $values)){
$this->set_error(sprintf($this->_messages['not_contains'], $this->_data['custom_name'], implode(', ', $values)));
}
return $this;
}
/**
* Verify if the current data is loweracase
* @access public
* @return Data_Validator The self instance
*/
public function is_lowercase(){
if($this->_data['value'] !== mb_strtolower($this->_data['value'], mb_detect_encoding($this->_data['value']))){
$this->set_error(sprintf($this->_messages['is_lowercase'], $this->_data['custom_name']));
}
return $this;
}
/**
* Verify if the current data is uppercase
* @access public
* @return Data_Validator The self instance
*/
public function is_uppercase(){
if($this->_data['value'] !== mb_strtoupper($this->_data['value'], mb_detect_encoding($this->_data['value']))){
$this->set_error(sprintf($this->_messages['is_uppercase'], $this->_data['custom_name']));
}
return $this;
}
/**
* Verify if the current data is multiple of the parameter
* @access public
* @param Int $value The value for comparison
* @return Data_Validator The self instance
*/
public function is_multiple($value){
if($value == 0){
$verify = ($this->_data['value'] == 0);
}
else{
$verify = ($this->_data['value'] % $value == 0);
}
if(!$verify){
$this->set_error(sprintf($this->_messages['is_multiple'], $this->_data['value'], $value));
}
return $this;
}
/**
* Verify if the current data is a positive number
* @access public
* @param Boolean $inclusive [optional] Include 0 in comparison?
* @return Data_Validator The self instance
*/
public function is_positive($inclusive = false){
$verify = ($inclusive === true ? ($this->_data['value'] >= 0) : ($this->_data['value'] > 0));
if(!$verify){
$this->set_error(sprintf($this->_messages['is_positive'], $this->_data['custom_name']));
}
return $this;
}
/**
* Verify if the current data is a negative number
* @access public
* @param Boolean $inclusive [optional] Include 0 in comparison?
* @return Data_Validator The self instance
*/
public function is_negative($inclusive = false){
$verify = ($inclusive === true ? ($this->_data['value'] <= 0) : ($this->_data['value'] < 0));
if(!$verify){
$this->set_error(sprintf($this->_messages['is_negative'], $this->_data['custom_name']));
}
return $this;
}
/**
* Verify if the current data is a valid Date
* @access public
* @param String $format [optional] The Date format
* @return Data_Validator The self instance
*/
public function is_date($format = null){
$verify = true;
if($this->_data['value'] instanceof DateTime){
return $this;
}
elseif(!is_string($this->_data['value'])){
$verify = false;
}
elseif (is_null($format)){
$verify = (strtotime($this->_data['value']) !== false);
if($verify){
return $this;
}
}
if($verify){
$date_from_format = DateTime::createFromFormat($format, $this->_data['value']);
$verify = $date_from_format && $this->_data['value'] === date($format, $date_from_format->getTimestamp());
}
if(!$verify){
$this->set_error(sprintf($this->_messages['is_date'], $this->_data['value']));
}
return $this;
}
/**
* Verify if the current data contains just alpha caracters
* @access protected
* @param String $string_format The regex
* @param String $additional [optional] The extra caracters
* @return Boolean True if data is valid or false otherwise
*/
protected function generic_alpha_num($string_format, $additional = ''){
$this->_data['value'] = (string) $this->_data['value'];
$clean_input = str_replace(str_split($additional), '', $this->_data['value']);
return ($clean_input !== $this->_data['value'] && $clean_input === '') || preg_match($string_format, $clean_input);
}
/**
* Verify if the current data contains just alpha caracters
* @access public
* @param String $additional [optional] The extra caracters
* @return Data_Validator The self instance
*/
public function is_alpha($additional = ''){
$string_format = '/^(\s|[a-zA-Z])*$/';
if(!$this->generic_alpha_num($string_format, $additional)){
$this->set_error(sprintf($this->_messages['is_alpha'], $this->_data['custom_name']));
}
return $this;
}
/**
* Verify if the current data contains just alpha-numerics caracters
* @access public
* @param String $additional [optional] The extra caracters
* @return Data_Validator The self instance
*/
public function is_alpha_num($additional = ''){
$string_format = '/^(\s|[a-zA-Z0-9])*$/';
if(!$this->generic_alpha_num($string_format, $additional)){
$this->set_error(sprintf($this->_messages['is_alpha_num'], $this->_data['custom_name']));
}
return $this;
}
/**
* Verify if the current data no contains white spaces
* @access public
* @return Data_Validator The self instance
*/
public function no_whitespaces(){
$verify = is_null($this->_data['value']) || preg_match('#^\S+$#', $this->_data['value']);
if(!$verify){
$this->set_error(sprintf($this->_messages['no_whitespaces'], $this->_data['custom_name']));
}
return $this;
}
/**
* Verify if the current data is a valid Phone Number (8 or 9 digits)
* @access public
* @return Data_Validator The self instance
*/
public function is_phone(){
$verify = preg_match('/^(\(0?\d{2}\)\s?|0?\d{2}[\s.-]?)\d{4,5}[\s.-]?\d{4}$/', $this->_data['value']);
if(!$verify){
$this->set_error(sprintf($this->_messages['is_phone'], $this->_data['custom_name']));
}
return $this;
}
/**
* Verify if the current data is a valid License Plate (Brazil)
* @access public
* @return Data_Validator The self instance
*/
public function is_plate(){
$verify = preg_match('/^[A-Z]{3}\-[0-9]{4}$/', $this->_data['value']);
if(!$verify){
$this->set_error(sprintf($this->_messages['is_plate'], $this->_data['custom_name']));
}
return $this;
}
/**
* Verify if the current data is a valid IP
* @access public
* @return Data_Validator The self instance
*/
public function is_ip(){
if (filter_var($this->_data['value'], FILTER_VALIDATE_IP) === false) {
$this->set_error(sprintf($this->_messages['is_ip'], $this->_data['value']));
}
return $this;
}
/**
* Verify if the current data is a valid Zip Code (Brazil)
* @access public
* @return Data_Validator The self instance
*/
public function is_zipCode(){
$verify = preg_match('/^[0-9]{5}-[0-9]{3}$/', $this->_data['value']);
if(!$verify){
$this->set_error(sprintf($this->_messages['is_zipCode'], $this->_data['custom_name']));
}
return $this;
}
/**
* Validate the data
* @access public
* @return bool The validation of data
*/
public function validate(){
return (count($this->_errors) > 0 ? false : true);
}
/**
* The messages of invalid data
* @param String $param [optional] A specific error
* @return Mixed One array with messages or a message of specific error
*/
public function get_errors($param = false){
if ($param){
if(isset($this->_errors[$this->_pattern['prefix'] . $param . $this->_pattern['sufix']])){
return $this->_errors[$this->_pattern['prefix'] . $param . $this->_pattern['sufix']];
}
else{
return false;
}
}
return $this->_errors;
}
}