-
Notifications
You must be signed in to change notification settings - Fork 341
/
gump.class.php
1981 lines (1746 loc) · 54.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/filisdev)
*
* @version 1.11
*/
use GUMP\ArrayHelpers;
use GUMP\EnvHelpers;
class GUMP
{
/**
* Singleton instance of GUMP.
*
* @var self|null
*/
protected static $instance = null;
/**
* Contains readable field names that have been manually set.
*
* @var array
*/
protected static $fields = [];
/**
* Custom validators.
*
* @var array
*/
protected static $validation_methods = [];
/**
* Custom validators error messages.
*
* @var array
*/
protected static $validation_methods_errors = [];
/**
* Customer filters.
*
* @var array
*/
protected static $filter_methods = [];
// ** ------------------------- 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;
}
// ** ------------------------- Configuration -------------------------------- ** //
/**
* Rules delimiter.
*
* @var string
*/
public static $rules_delimiter = '|';
/**
* Rules-parameters delimiter.
*
* @var string
*/
public static $rules_parameters_delimiter = ',';
/**
* Rules parameters array delimiter.
*
* @var string
*/
public static $rules_parameters_arrays_delimiter = ';';
/**
* Characters that will be replaced to spaces during field name conversion (street_name => Street Name).
*
* @var array
*/
public static $field_chars_to_spaces = ['_', '-'];
// ** ------------------------- 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,_";
public static $trues = ['1', 1, 'true', true, 'yes', 'on'];
public static $falses = ['0', 0, 'false', false, 'no', 'off'];
/**
* Language for error messages.
*
* @var string
*/
protected $lang;
/**
* Custom field-rule messages.
*
* @var array
*/
protected $fields_error_messages = [];
/**
* Set of validation rules for execution.
*
* @var array
*/
protected $validation_rules = [];
/**
* Set of filters rules for execution.
*
* @var array
*/
protected $filter_rules = [];
/**
* Errors.
*
* @var array
*/
protected $errors = [];
// ** ------------------------- Validation Helpers ---------------------------- ** //
/**
* GUMP constructor.
*
* @param string $lang
* @throws Exception when language is not supported
*/
public function __construct(string $lang = 'en')
{
$lang_file_location = __DIR__.DIRECTORY_SEPARATOR.'lang'.DIRECTORY_SEPARATOR.$lang.'.php';
if (!EnvHelpers::file_exists($lang_file_location)) {
throw new Exception(sprintf("'%s' language is not supported.", $lang));
}
$this->lang = $lang;
}
/**
* Shorthand method for inline validation.
*
* @param array $data The data to be validated
* @param array $validators The GUMP validators
* @param array $fields_error_messages
* @return mixed True(boolean) or the array of error messages
* @throws Exception If validation rule does not exist
*/
public static function is_valid(array $data, array $validators, array $fields_error_messages = [])
{
$gump = self::get_instance();
$gump->validation_rules($validators);
$gump->set_fields_error_messages($fields_error_messages);
if ($gump->run($data) === false) {
return $gump->get_readable_errors();
}
return true;
}
/**
* Shorthand method for running only the data filters.
*
* @param array $data
* @param array $filters
* @return mixed
* @throws Exception If filter does not exist
*/
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
* @throws Exception
*/
public function __toString()
{
return $this->get_readable_errors(true);
}
/**
* Perform XSS clean to prevent cross site scripting.
*
* @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;
}
/**
* An empty value for us is: null, empty string or empty array
*
* @param $value
* @return bool
*/
public static function is_empty($value)
{
return (is_null($value) || $value === '' || (is_array($value) && count($value) === 0));
}
/**
* Adds a custom validation rule using a callback function.
*
* @param string $rule
* @param callable $callback
* @param string $error_message
*
* @return void
* @throws Exception when validator with the same name already exists
*/
public static function add_validator(string $rule, callable $callback, string $error_message)
{
if (method_exists(__CLASS__, self::validator_to_method($rule)) || isset(self::$validation_methods[$rule])) {
throw new Exception(sprintf("'%s' validator is already defined.", $rule));
}
self::$validation_methods[$rule] = $callback;
self::$validation_methods_errors[$rule] = $error_message;
}
/**
* Adds a custom filter using a callback function.
*
* @param string $rule
* @param callable $callback
*
* @return void
* @throws Exception when filter with the same name already exists
*/
public static function add_filter(string $rule, callable $callback)
{
if (method_exists(__CLASS__, self::filter_to_method($rule)) || isset(self::$filter_methods[$rule])) {
throw new Exception(sprintf("'%s' filter is already defined.", $rule));
}
self::$filter_methods[$rule] = $callback;
}
/**
* 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 (isset($array[$key])) {
return $array[$key];
}
return $default;
}
/**
* Getter/Setter for the validation rules.
*
* @param array $rules
* @return array
*/
public function validation_rules(array $rules = [])
{
if (empty($rules)) {
return $this->validation_rules;
}
$this->validation_rules = $rules;
}
public function set_fields_error_messages(array $fields_error_messages)
{
return $this->fields_error_messages = $fields_error_messages;
}
/**
* Getter/Setter for the filter rules.
*
* @param array $rules
* @return array
*/
public function filter_rules(array $rules = [])
{
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(), $this->fields_error_messages
);
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[] = $this->generate_error_array($field, $data[$field], 'mismatch');
}
}
/**
* Sanitize the input data.
*
* @param array $input
* @param array $fields
* @param bool $utf8_encode
*
* @return array
*/
public function sanitize(array $input, array $fields = [], bool $utf8_encode = true)
{
if (empty($fields)) {
$fields = array_keys($input);
}
$return = [];
foreach ($fields as $field) {
if (!isset($input[$field])) {
continue;
} else {
$value = $input[$field];
if (is_array($value)) {
$value = $this->sanitize($value, [], $utf8_encode);
}
if (is_string($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 array $input Input data.
* @param array $ruleset Validation rules.
*
* @return bool|array Returns bool true when no errors. Returns array when errors.
* @throws Exception
*/
public function validate(array $input, array $ruleset)
{
$this->errors = [];
foreach ($ruleset as $field => $rawRules) {
$input[$field] = ArrayHelpers::data_get($input, $field);
$rules = $this->parse_rules($rawRules);
$is_required = $this->field_has_required_rules($rules);
if (!$is_required && self::is_empty($input[$field])) {
continue;
}
foreach ($rules as $rule) {
$parsed_rule = $this->parse_rule($rule);
$result = $this->foreach_call_validator($parsed_rule['rule'], $field, $input, $parsed_rule['param']);
if (is_array($result)) {
$this->errors[] = $result;
break; // exit on first error
}
}
}
return (count($this->errors) > 0) ? $this->errors : true;
}
/**
* Parses filters and validators rules group.
*
* @param string|array $rules
* @return array
*/
private function parse_rules($rules)
{
// v2
if (is_array($rules)) {
$rules_names = [];
foreach ($rules as $key => $value) {
$rules_names[] = is_numeric($key) ? $value : $key;
}
return array_map(function($value, $key) use($rules) {
if ($value === $key) {
return [ $key ];
}
return [$key, $value];
}, $rules, $rules_names);
}
return explode(self::$rules_delimiter, $rules);;
}
/**
* Parses filters and validators individual rules.
*
* @param string|array $rule
* @return array
*/
private function parse_rule($rule)
{
// v2
if (is_array($rule)) {
return [
'rule' => $rule[0],
'param' => $this->parse_rule_params($rule[1] ?? [])
];
}
$result = [
'rule' => $rule,
'param' => []
];
if (strstr($rule, self::$rules_parameters_delimiter) !== false) {
list($rule, $param) = explode(self::$rules_parameters_delimiter, $rule);
$result['rule'] = $rule;
$result['param'] = $this->parse_rule_params($param);
}
return $result;
}
/**
* Parse rule parameters.
*
* @param string|array $param
* @return array|string|null
*/
private function parse_rule_params($param)
{
if (is_array($param)) {
return $param;
}
if (strstr($param, self::$rules_parameters_arrays_delimiter) !== false) {
return explode(self::$rules_parameters_arrays_delimiter, $param);
}
return [ $param ];
}
/**
* Checks if array of rules contains a required type of validator.
*
* @param array $rules
* @return bool
*/
private function field_has_required_rules(array $rules)
{
$require_type_of_rules = ['required', 'required_file'];
// v2
if (is_array($rules) && is_array($rules[0])) {
$found = array_filter($rules, function($item) use($require_type_of_rules) {
return in_array($item[0], $require_type_of_rules);
});
return count($found) > 0;
}
$found = array_values(array_intersect($require_type_of_rules, $rules));
return count($found) > 0;
}
/**
* Helper to convert validator rule name to validator rule method name.
*
* @param string $rule
* @return string
*/
private static function validator_to_method(string $rule)
{
return sprintf('validate_%s', $rule);
}
/**
* Helper to convert filter rule name to filter rule method name.
*
* @param string $rule
* @return string
*/
private static function filter_to_method(string $rule)
{
return sprintf('filter_%s', $rule);
}
/**
* Calls call_validator.
*
* @param string $rule
* @param string $field
* @param mixed $input
* @param array $rule_params
* @return array|bool
* @throws Exception
*/
private function foreach_call_validator(string $rule, string $field, array $input, array $rule_params = [])
{
$values = !is_array($input[$field]) ? [ $input[$field] ] : $input[$field];
foreach ($values as $value) {
$result = $this->call_validator($rule, $field, $input, $rule_params, $value);
if (is_array($result)) {
return $result;
}
}
return true;
}
/**
* Calls a validator.
*
* @param string $rule
* @param string $field
* @param mixed $input
* @param array $rule_params
* @return array|bool
* @throws Exception
*/
private function call_validator(string $rule, string $field, array $input, array $rule_params = [], $value = null)
{
$method = self::validator_to_method($rule);
if (is_callable([$this, $method])) {
$result = $this->$method($field, $input, $rule_params, $value);
// is_array check for backward compatibility
return (is_array($result) || $result === false)
? $this->generate_error_array($field, $input[$field], $rule, $rule_params, $value)
: true;
} elseif (isset(self::$validation_methods[$rule])) {
$result = call_user_func(self::$validation_methods[$rule], $field, $input, $rule_params, $value);
return ($result === false)
? $this->generate_error_array($field, $input[$field], $rule, $rule_params)
: true;
}
throw new Exception(sprintf("'%s' validator does not exist.", $rule));
}
/**
* Calls a filter.
*
* @param string $rule
* @param mixed $value
* @param array $rule_params
* @return mixed
* @throws Exception
*/
private function call_filter(string $rule, $value, array $rule_params = [])
{
$method = self::filter_to_method($rule);
if (is_callable(array($this, $method))) {
return $this->$method($value, $rule_params);
} elseif (isset(self::$filter_methods[$rule])) {
return call_user_func(self::$filter_methods[$rule], $value, $rule_params);
} elseif (function_exists($rule)) {
return call_user_func($rule, $value, ...$rule_params);
}
throw new Exception(sprintf("'%s' filter does not exist.", $rule));
}
/**
* Generates error array.
*
* @param string $field
* @param mixed $value
* @param string $rule
* @param array $rule_params
* @return array
*/
private function generate_error_array(string $field, $value, string $rule, array $rule_params = [])
{
return [
'field' => $field,
'value' => $value,
'rule' => $rule,
'params' => $rule_params
];
}
/**
* Set a readable name for a specified field names.
*
* @param string $field
* @param string $readable_name
*/
public static function set_field_name(string $field, string $readable_name)
{
self::$fields[$field] = $readable_name;
}
/**
* Set readable name for specified fields in an array.
*
* @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(string $rule, string $message)
{
self::$validation_methods_errors[$rule] = $message;
}
/**
* Set custom error messages for validation rules in an array.
*
* @param array $array
*/
public static function set_error_messages(array $array)
{
foreach ($array as $rule => $message) {
self::set_error_message($rule, $message);
}
}
/**
* Get all error messages.
*
* @return array
*/
protected function get_messages()
{
$lang_file = __DIR__.DIRECTORY_SEPARATOR.'lang'.DIRECTORY_SEPARATOR.$this->lang.'.php';
$messages = include $lang_file;
return array_merge($messages, self::$validation_methods_errors);
}
/**
* Get error message.
*
* @param array $messages
* @param string $field
* @param string $rule
* @return mixed|null
* @throws Exception
*/
private function get_error_message(array $messages, string $field, string $rule)
{
$custom_error_message = $this->get_custom_error_message($field, $rule);
if ($custom_error_message !== null) {
return $custom_error_message;
}
if (isset($messages[$rule])) {
return $messages[$rule];
}
throw new Exception(sprintf("'%s' validator does not have an error message.", $rule));
}
/**
* Get custom error message for field and rule.
*
* @param string $field
* @param string $rule
* @return string|null
*/
private function get_custom_error_message(string $field, string $rule)
{
$rule_name = str_replace('validate_', '', $rule);
return $this->fields_error_messages[$field][$rule_name] ?? null;
}
/**
* Process error message string.
*
* @param $field
* @param array $params
* @param string $message
* @param callable|null $transformer
* @return string
*/
private function process_error_message($field, array $params, string $message, callable $transformer = null)
{
// if field name is explicitly set, use it
if (array_key_exists($field, self::$fields)) {
$field = self::$fields[$field];
} else {
$field = ucwords(str_replace(self::$field_chars_to_spaces, chr(32), $field));
}
// if param is a field (i.e. equalsfield validator)
if (isset($params[0]) && array_key_exists($params[0], self::$fields)) {
$params[0] = self::$fields[$params[0]];
}
$replace = [
'{field}' => $field,
'{param}' => implode(', ', $params)
];
foreach ($params as $key => $value) {
$replace[sprintf('{param[%s]}', $key)] = $value;
}
// for get_readable_errors() <span>
if ($transformer) {
$replace = $transformer($replace);
}
return strtr($message, $replace);
}
/**
* 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|string
* @throws Exception if validator doesn't have an error message to set
*/
public function get_readable_errors(bool $convert_to_string = false, string $field_class = 'gump-field', string $error_class = 'gump-error-message')
{
if (empty($this->errors)) {
return $convert_to_string ? '' : [];
}
$messages = $this->get_messages();
$result = [];
$transformer = static function($replace) use($field_class) {
$replace['{field}'] = sprintf('<span class="%s">%s</span>', $field_class, $replace['{field}']);
return $replace;
};
foreach ($this->errors as $error) {
$message = $this->get_error_message($messages, $error['field'], $error['rule']);
$result[] = $this->process_error_message($error['field'], $error['params'], $message, $transformer);
}
if ($convert_to_string) {
return array_reduce($result, static function($prev, $next) use($error_class) {
return sprintf('%s<span class="%s">%s</span>', $prev, $error_class, $next);
});
}
return $result;
}
/**
* Process the validation errors and return an array of errors with field names as keys.
*
* @return array
* @throws Exception
*/
public function get_errors_array()
{
$messages = $this->get_messages();
$result = [];
foreach ($this->errors as $error) {
$message = $this->get_error_message($messages, $error['field'], $error['rule']);
$result[$error['field']] = $this->process_error_message($error['field'], $error['params'], $message);
}
return $result;
}
/**
* Filter the input data according to the specified filter set.
*
* @param mixed $input
* @param array $filterset
* @return mixed
* @throws Exception
*/
public function filter(array $input, array $filterset)
{
foreach ($filterset as $field => $filters) {
if (!array_key_exists($field, $input)) {
continue;
}
$filters = $this->parse_rules($filters);
foreach ($filters as $filter) {
$parsed_rule = $this->parse_rule($filter);
if (is_array($input[$field])) {
$input_array = &$input[$field];
} else {
$input_array = array(&$input[$field]);
}
foreach ($input_array as &$value) {
$value = $this->call_filter($parsed_rule['rule'], $value, $parsed_rule['param']);
}
}
}
return $input;
}
// ** ------------------------- Filters --------------------------------------- ** //
/**
* Replace noise words in a string (http://tax.cchgroup.com/help/Avoiding_noise_words_in_your_search.htm).
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_noise_words($value, array $params = [])
{
$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.
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_rmpunctuation($value, array $params = [])
{
return preg_replace("/(?![.=$'€%-])\p{P}/u", '', $value);
}
/**
* Sanitize the string by urlencoding characters.
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_urlencode($value, array $params = [])
{
return filter_var($value, FILTER_SANITIZE_ENCODED);
}
/**
* Sanitize the string by converting HTML characters to their HTML entities.
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_htmlencode($value, array $params = [])
{
return filter_var($value, FILTER_SANITIZE_SPECIAL_CHARS);
}
/**
* Sanitize the string by removing illegal characters from emails.
*
* @param string $value
* @param array $params
*
* @return string
*/
protected function filter_sanitize_email($value, array $params = [])
{
return filter_var($value, FILTER_SANITIZE_EMAIL);
}
/**
* Sanitize the string by removing illegal characters from numbers.