forked from Wixel/GUMP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gump.class.php
2407 lines (2120 loc) · 66.2 KB
/
gump.class.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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* GUMP - A fast, extensible PHP input validation class.
*
* @author Sean Nieuwoudt (http://twitter.com/SeanNieuwoudt)
* @author Filis Futsarov (http://twitter.com/FilisCode)
* @copyright Copyright (c) 2017 wixelhq.com
*
* @version 1.5
*/
class GUMP
{
// Singleton instance of GUMP
protected static $instance = null;
// Validation rules for execution
protected $validation_rules = array();
// Filter rules for execution
protected $filter_rules = array();
// Instance attribute containing errors from last run
protected $errors = array();
// Contain readable field names that have been set manually
protected static $fields = array();
// Custom validation methods
protected static $validation_methods = array();
// Custom validation methods error messages and custom ones
protected static $validation_methods_errors = array();
// Customer filter methods
protected static $filter_methods = array();
// ** ------------------------- Instance Helper ---------------------------- ** //
/**
* Function to create and return previously created instance
*
* @return GUMP
*/
public static function get_instance(){
if(self::$instance === null)
{
self::$instance = new static();
}
return self::$instance;
}
// ** ------------------------- Validation Data ------------------------------- ** //
public static $basic_tags = '<br><p><a><strong><b><i><em><img><blockquote><code><dd><dl><hr><h1><h2><h3><h4><h5><h6><label><ul><li><span><sub><sup>';
public static $en_noise_words = "about,after,all,also,an,and,another,any,are,as,at,be,because,been,before,
being,between,both,but,by,came,can,come,could,did,do,each,for,from,get,
got,has,had,he,have,her,here,him,himself,his,how,if,in,into,is,it,its,it's,like,
make,many,me,might,more,most,much,must,my,never,now,of,on,only,or,other,
our,out,over,said,same,see,should,since,some,still,such,take,than,that,
the,their,them,then,there,these,they,this,those,through,to,too,under,up,
very,was,way,we,well,were,what,where,which,while,who,with,would,you,your,a,
b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,$,1,2,3,4,5,6,7,8,9,0,_";
// field characters below will be replaced with a space.
protected $fieldCharsToRemove = array('_', '-');
protected $lang;
// ** ------------------------- Validation Helpers ---------------------------- ** //
public function __construct($lang = 'en')
{
if ($lang) {
$lang_file = __DIR__.DIRECTORY_SEPARATOR.'lang'.DIRECTORY_SEPARATOR.$lang.'.php';
if (file_exists($lang_file)) {
$this->lang = $lang;
} else {
throw new \Exception('Language with key "'.$lang.'" does not exist');
}
}
}
/**
* Shorthand method for inline validation.
*
* @param array $data The data to be validated
* @param array $validators The GUMP validators
*
* @return mixed True(boolean) or the array of error messages
*/
public static function is_valid(array $data, array $validators)
{
$gump = self::get_instance();
$gump->validation_rules($validators);
if ($gump->run($data) === false) {
return $gump->get_readable_errors(false);
} else {
return true;
}
}
/**
* Shorthand method for running only the data filters.
*
* @param array $data
* @param array $filters
*
* @return mixed
*/
public static function filter_input(array $data, array $filters)
{
$gump = self::get_instance();
return $gump->filter($data, $filters);
}
/**
* Magic method to generate the validation error messages.
*
* @return string
*/
public function __toString()
{
return $this->get_readable_errors(true);
}
/**
* Perform XSS clean to prevent cross site scripting.
*
* @static
*
* @param array $data
*
* @return array
*/
public static function xss_clean(array $data)
{
foreach ($data as $k => $v) {
$data[$k] = filter_var($v, FILTER_SANITIZE_STRING);
}
return $data;
}
/**
* Adds a custom validation rule using a callback function.
*
* @param string $rule
* @param callable $callback
* @param string $error_message
*
* @return bool
*
* @throws Exception
*/
public static function add_validator($rule, $callback, $error_message = null)
{
$method = 'validate_'.$rule;
if (method_exists(__CLASS__, $method) || isset(self::$validation_methods[$rule])) {
throw new Exception("Validator rule '$rule' already exists.");
}
self::$validation_methods[$rule] = $callback;
if ($error_message) {
self::$validation_methods_errors[$rule] = $error_message;
}
return true;
}
/**
* Adds a custom filter using a callback function.
*
* @param string $rule
* @param callable $callback
*
* @return bool
*
* @throws Exception
*/
public static function add_filter($rule, $callback)
{
$method = 'filter_'.$rule;
if (method_exists(__CLASS__, $method) || isset(self::$filter_methods[$rule])) {
throw new Exception("Filter rule '$rule' already exists.");
}
self::$filter_methods[$rule] = $callback;
return true;
}
/**
* Helper method to extract an element from an array safely
*
* @param mixed $key
* @param array $array
* @param mixed $default
* @return mixed
*/
public static function field($key, array $array, $default = null)
{
if(!is_array($array)) {
return null;
}
if(isset($array[$key])) {
return $array[$key];
} else {
return $default;
}
}
/**
* Getter/Setter for the validation rules.
*
* @param array $rules
*
* @return array
*/
public function validation_rules(array $rules = array())
{
if (empty($rules)) {
return $this->validation_rules;
}
$this->validation_rules = $rules;
}
/**
* Getter/Setter for the filter rules.
*
* @param array $rules
*
* @return array
*/
public function filter_rules(array $rules = array())
{
if (empty($rules)) {
return $this->filter_rules;
}
$this->filter_rules = $rules;
}
/**
* Run the filtering and validation after each other.
*
* @param array $data
* @param bool $check_fields
*
* @return array
*
* @throws Exception
*/
public function run(array $data, $check_fields = false)
{
$data = $this->filter($data, $this->filter_rules());
$validated = $this->validate(
$data, $this->validation_rules()
);
if ($check_fields === true) {
$this->check_fields($data);
}
if ($validated !== true) {
return false;
}
return $data;
}
/**
* Ensure that the field counts match the validation rule counts.
*
* @param array $data
*/
private function check_fields(array $data)
{
$ruleset = $this->validation_rules();
$mismatch = array_diff_key($data, $ruleset);
$fields = array_keys($mismatch);
foreach ($fields as $field) {
$this->errors[] = array(
'field' => $field,
'value' => $data[$field],
'rule' => 'mismatch',
'param' => null,
);
}
}
/**
* Sanitize the input data.
*
* @param array $input
* @param null $fields
* @param bool $utf8_encode
*
* @return array
*/
public function sanitize(array $input, array $fields = array(), $utf8_encode = true)
{
$magic_quotes = (bool) get_magic_quotes_gpc();
if (empty($fields)) {
$fields = array_keys($input);
}
$return = array();
foreach ($fields as $field) {
if (!isset($input[$field])) {
continue;
} else {
$value = $input[$field];
if (is_array($value)) {
$value = $this->sanitize($value);
}
if (is_string($value)) {
if ($magic_quotes === true) {
$value = stripslashes($value);
}
if (strpos($value, "\r") !== false) {
$value = trim($value);
}
if (function_exists('iconv') && function_exists('mb_detect_encoding') && $utf8_encode) {
$current_encoding = mb_detect_encoding($value);
if ($current_encoding != 'UTF-8' && $current_encoding != 'UTF-16') {
$value = iconv($current_encoding, 'UTF-8', $value);
}
}
$value = filter_var($value, FILTER_SANITIZE_STRING);
}
$return[$field] = $value;
}
}
return $return;
}
/**
* Return the error array from the last validation run.
*
* @return array
*/
public function errors()
{
return $this->errors;
}
/**
* Perform data validation against the provided ruleset.
*
* @param mixed $input
* @param array $ruleset
*
* @return mixed
*
* @throws Exception
*/
public function validate(array $input, array $ruleset)
{
$this->errors = array();
foreach ($ruleset as $field => $rules) {
$rules = explode('|', $rules);
$look_for = array('required_file', 'required');
if (count(array_intersect($look_for, $rules)) > 0 || (isset($input[$field]))) {
if (isset($input[$field])) {
if (is_array($input[$field]) && in_array('required_file', $ruleset)) {
$input_array = $input[$field];
} else {
$input_array = array($input[$field]);
}
} else {
$input_array = array('');
}
foreach ($input_array as $value) {
$input[$field] = $value;
foreach ($rules as $rule) {
$method = null;
$param = null;
// Check if we have rule parameters
if (strstr($rule, ',') !== false) {
$rule = explode(',', $rule);
$method = 'validate_'.$rule[0];
$param = $rule[1];
$rule = $rule[0];
// If there is a reference to a field
if (preg_match('/(?:(?:^|;)_([a-z_]+))/', $param, $matches)) {
// If provided parameter is a field
if (isset($input[$matches[1]])) {
$param = str_replace('_'.$matches[1], $input[$matches[1]], $param);
}
}
} else {
$method = 'validate_'.$rule;
}
//self::$validation_methods[$rule] = $callback;
if (is_callable(array($this, $method))) {
$result = $this->$method(
$field, $input, $param
);
if (is_array($result)) {
if (array_search($result['field'], array_column($this->errors, 'field')) === false) {
$this->errors[] = $result;
}
}
} elseif(isset(self::$validation_methods[$rule])) {
$result = call_user_func(self::$validation_methods[$rule], $field, $input, $param);
if($result === false) {
if (array_search($result['field'], array_column($this->errors, 'field')) === false) {
$this->errors[] = array(
'field' => $field,
'value' => $input[$field],
'rule' => $rule,
'param' => $param,
);
}
}
} else {
throw new Exception("Validator method '$method' does not exist.");
}
}
}
}
}
return (count($this->errors) > 0) ? $this->errors : true;
}
/**
* Set a readable name for a specified field names.
*
* @param string $field
* @param string $readable_name
*/
public static function set_field_name($field, $readable_name)
{
self::$fields[$field] = $readable_name;
}
/**
* Set readable name for specified fields in an array.
*
* Usage:
*
* GUMP::set_field_names(array(
* "name" => "My Lovely Name",
* "username" => "My Beloved Username",
* ));
*
* @param array $array
*/
public static function set_field_names(array $array)
{
foreach ($array as $field => $readable_name) {
self::set_field_name($field, $readable_name);
}
}
/**
* Set a custom error message for a validation rule.
*
* @param string $rule
* @param string $message
*/
public static function set_error_message($rule, $message)
{
$gump = self::get_instance();
self::$validation_methods_errors[$rule] = $message;
}
/**
* Set custom error messages for validation rules in an array.
*
* Usage:
*
* GUMP::set_error_messages(array(
* "validate_required" => "{field} is required",
* "validate_valid_email" => "{field} must be a valid email",
* ));
*
* @param array $array
*/
public static function set_error_messages(array $array)
{
foreach ($array as $rule => $message) {
self::set_error_message($rule, $message);
}
}
/**
* Get error messages.
*
* @return array
*/
protected function get_messages()
{
$lang_file = __DIR__.DIRECTORY_SEPARATOR.'lang'.DIRECTORY_SEPARATOR.$this->lang.'.php';
$messages = require $lang_file;
if ($validation_methods_errors = self::$validation_methods_errors) {
$messages = array_merge($messages, $validation_methods_errors);
}
return $messages;
}
/**
* Process the validation errors and return human readable error messages.
*
* @param bool $convert_to_string = false
* @param string $field_class
* @param string $error_class
*
* @return array
* @return string
*/
public function get_readable_errors($convert_to_string = false, $field_class = 'gump-field', $error_class = 'gump-error-message')
{
if (empty($this->errors)) {
return ($convert_to_string) ? null : array();
}
$resp = array();
// Error messages
$messages = $this->get_messages();
foreach ($this->errors as $e) {
$field = ucwords(str_replace($this->fieldCharsToRemove, chr(32), $e['field']));
$param = $e['param'];
// Let's fetch explicitly if the field names exist
if (array_key_exists($e['field'], self::$fields)) {
$field = self::$fields[$e['field']];
// If param is a field (i.e. equalsfield validator)
if (array_key_exists($param, self::$fields)) {
$param = self::$fields[$e['param']];
}
}
// Messages
if (isset($messages[$e['rule']])) {
if (is_array($param)) {
$param = implode(', ', $param);
}
$message = str_replace('{param}', $param, str_replace('{field}', '<span class="'.$field_class.'">'.$field.'</span>', $messages[$e['rule']]));
$resp[] = $message;
} else {
throw new \Exception ('Rule "'.$e['rule'].'" does not have an error message');
}
}
if (!$convert_to_string) {
return $resp;
} else {
$buffer = '';
foreach ($resp as $s) {
$buffer .= "<span class=\"$error_class\">$s</span>";
}
return $buffer;
}
}
/**
* Process the validation errors and return an array of errors with field names as keys.
*
* @param $convert_to_string
*
* @return array | null (if empty)
*/
public function get_errors_array($convert_to_string = null)
{
if (empty($this->errors)) {
return ($convert_to_string) ? null : array();
}
$resp = array();
// Error messages
$messages = $this->get_messages();
foreach ($this->errors as $e)
{
$field = ucwords(str_replace(array('_', '-'), chr(32), $e['field']));
$param = $e['param'];
// Let's fetch explicitly if the field names exist
if (array_key_exists($e['field'], self::$fields)) {
$field = self::$fields[$e['field']];
// If param is a field (i.e. equalsfield validator)
if (array_key_exists($param, self::$fields)) {
$param = self::$fields[$e['param']];
}
}
// Messages
if (isset($messages[$e['rule']])) {
// Show first validation error and don't allow to be overwritten
if (!isset($resp[$e['field']])) {
if (is_array($param)) {
$param = implode(', ', $param);
}
$message = str_replace('{param}', $param, str_replace('{field}', $field, $messages[$e['rule']]));
$resp[$e['field']] = $message;
}
} else {
throw new \Exception ('Rule "'.$e['rule'].'" does not have an error message');
}
}
return $resp;
}
/**
* Filter the input data according to the specified filter set.
*
* @param mixed $input
* @param array $filterset
*
* @throws Exception
*
* @return mixed
*
* @throws Exception
*/
public function filter(array $input, array $filterset)
{
foreach ($filterset as $field => $filters) {
if (!array_key_exists($field, $input)) {
continue;
}
$filters = explode('|', $filters);
foreach ($filters as $filter) {
$params = null;
if (strstr($filter, ',') !== false) {
$filter = explode(',', $filter);
$params = array_slice($filter, 1, count($filter) - 1);
$filter = $filter[0];
}
if (is_array($input[$field])) {
$input_array = &$input[$field];
} else {
$input_array = array(&$input[$field]);
}
foreach ($input_array as &$value) {
if (is_callable(array($this, 'filter_'.$filter))) {
$method = 'filter_'.$filter;
$value = $this->$method($value, $params);
} elseif (function_exists($filter)) {
$value = $filter($value);
} elseif (isset(self::$filter_methods[$filter])) {
$value = call_user_func(self::$filter_methods[$filter], $value, $params);
} else {
throw new Exception("Filter method '$filter' does not exist.");
}
}
}
}
return $input;
}
// ** ------------------------- Filters --------------------------------------- ** //
/**
* Replace noise words in a string (http://tax.cchgroup.com/help/Avoiding_noise_words_in_your_search.htm).
*
* Usage: '<index>' => 'noise_words'
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_noise_words($value, $params = null)
{
$value = preg_replace('/\s\s+/u', chr(32), $value);
$value = " $value ";
$words = explode(',', self::$en_noise_words);
foreach ($words as $word) {
$word = trim($word);
$word = " $word "; // Normalize
if (stripos($value, $word) !== false) {
$value = str_ireplace($word, chr(32), $value);
}
}
return trim($value);
}
/**
* Remove all known punctuation from a string.
*
* Usage: '<index>' => 'rmpunctuataion'
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_rmpunctuation($value, $params = null)
{
return preg_replace("/(?![.=$'€%-])\p{P}/u", '', $value);
}
/**
* Sanitize the string by removing any script tags.
*
* Usage: '<index>' => 'sanitize_string'
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_sanitize_string($value, $params = null)
{
return filter_var($value, FILTER_SANITIZE_STRING);
}
/**
* Sanitize the string by urlencoding characters.
*
* Usage: '<index>' => 'urlencode'
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_urlencode($value, $params = null)
{
return filter_var($value, FILTER_SANITIZE_ENCODED);
}
/**
* Sanitize the string by converting HTML characters to their HTML entities.
*
* Usage: '<index>' => 'htmlencode'
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_htmlencode($value, $params = null)
{
return filter_var($value, FILTER_SANITIZE_SPECIAL_CHARS);
}
/**
* Sanitize the string by removing illegal characters from emails.
*
* Usage: '<index>' => 'sanitize_email'
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_sanitize_email($value, $params = null)
{
return filter_var($value, FILTER_SANITIZE_EMAIL);
}
/**
* Sanitize the string by removing illegal characters from numbers.
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_sanitize_numbers($value, $params = null)
{
return filter_var($value, FILTER_SANITIZE_NUMBER_INT);
}
/**
* Sanitize the string by removing illegal characters from float numbers.
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_sanitize_floats($value, $params = null)
{
return filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
}
/**
* Filter out all HTML tags except the defined basic tags.
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_basic_tags($value, $params = null)
{
return strip_tags($value, self::$basic_tags);
}
/**
* Convert the provided numeric value to a whole number.
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_whole_number($value, $params = null)
{
return intval($value);
}
/**
* Convert MS Word special characters to web safe characters.
* [“, ”, ‘, ’, –, …] => [", ", ', ', -, ...]
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_ms_word_characters($value, $params = null)
{
$word_open_double = '“';
$word_close_double = '”';
$web_safe_double = '"';
$value = str_replace(array($word_open_double, $word_close_double), $web_safe_double, $value);
$word_open_single = '‘';
$word_close_single = '’';
$web_safe_single = "'";
$value = str_replace(array($word_open_single, $word_close_single), $web_safe_single, $value);
$word_em = '–';
$web_safe_em = '-';
$value = str_replace($word_em, $web_safe_em, $value);
$word_ellipsis = '…';
$web_ellipsis = '...';
$value = str_replace($word_ellipsis, $web_ellipsis, $value);
return $value;
}
/**
* Converts to lowercase.
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_lower_case($value, $params = null)
{
return strtolower($value);
}
/**
* Converts to uppercase.
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_upper_case($value, $params = null)
{
return strtoupper($value);
}
/**
* Converts value to url-web-slugs.
*
* Credit:
* https://stackoverflow.com/questions/40641973/php-to-convert-string-to-slug
* http://cubiq.org/the-perfect-php-clean-url-generator
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_slug($value, $params = null)
{
$delimiter = '-';
$slug = strtolower(trim(preg_replace('/[\s-]+/', $delimiter, preg_replace('/[^A-Za-z0-9-]+/', $delimiter, preg_replace('/[&]/', 'and', preg_replace('/[\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $str))))), $delimiter));
return $slug;
}
// ** ------------------------- Validators ------------------------------------ ** //
/**
* Verify that a value is contained within the pre-defined value set.
*
* Usage: '<index>' => 'contains,value value value'
*
* @param string $field
* @param array $input
* @param null $param
*
* @return mixed
*/
protected function validate_contains($field, $input, $param = null)
{
if (!isset($input[$field])) {
return;
}
$param = trim(strtolower($param));
$value = trim(strtolower($input[$field]));
if (preg_match_all('#\'(.+?)\'#', $param, $matches, PREG_PATTERN_ORDER)) {
$param = $matches[1];
} else {
$param = explode(chr(32), $param);
}
if (in_array($value, $param)) { // valid, return nothing
return;
}
return array(
'field' => $field,
'value' => $value,
'rule' => __FUNCTION__,
'param' => $param,
);
}
/**
* Verify that a value is contained within the pre-defined value set.
* OUTPUT: will NOT show the list of values.
*
* Usage: '<index>' => 'contains_list,value;value;value'
*
* @param string $field
* @param array $input
*
* @return mixed