|
1
|
|
|
<?php namespace Arcanedev\Sanitizer; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\Sanitizer\Contracts\Filterable; |
|
4
|
|
|
use Arcanedev\Sanitizer\Contracts\Sanitizer as SanitizerContract; |
|
5
|
|
|
use Closure; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Sanitizer |
|
9
|
|
|
* |
|
10
|
|
|
* @package Arcanedev\Sanitizer |
|
11
|
|
|
* @author ARCANEDEV <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Sanitizer implements SanitizerContract |
|
14
|
|
|
{ |
|
15
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
16
|
|
|
| Properties |
|
17
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
18
|
|
|
*/ |
|
19
|
|
|
/** |
|
20
|
|
|
* Available filters. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $filters = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Rules to sanitize. |
|
28
|
|
|
* |
|
29
|
|
|
* @var Entities\Rules |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $rules; |
|
32
|
|
|
|
|
33
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
34
|
|
|
| Constructor |
|
35
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
36
|
|
|
*/ |
|
37
|
|
|
/** |
|
38
|
|
|
* Create a new sanitizer instance. |
|
39
|
|
|
* |
|
40
|
|
|
* @param array $filters |
|
41
|
|
|
*/ |
|
42
|
315 |
|
public function __construct(array $filters = []) |
|
43
|
|
|
{ |
|
44
|
315 |
|
$this->rules = new Entities\Rules; |
|
45
|
315 |
|
$this->setFilters($filters); |
|
46
|
315 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
49
|
|
|
| Getters & Setters |
|
50
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
51
|
|
|
*/ |
|
52
|
|
|
/** |
|
53
|
|
|
* Set filters. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $filters |
|
56
|
|
|
* |
|
57
|
|
|
* @return self |
|
58
|
|
|
*/ |
|
59
|
315 |
|
public function setFilters(array $filters) |
|
60
|
|
|
{ |
|
61
|
315 |
|
if (empty($this->filters)) { |
|
62
|
315 |
|
$this->filters = $this->getDefaultFilters(); |
|
63
|
105 |
|
} |
|
64
|
|
|
|
|
65
|
315 |
|
$this->filters = array_merge( |
|
66
|
315 |
|
$this->filters, $filters |
|
67
|
105 |
|
); |
|
68
|
|
|
|
|
69
|
315 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set rules. |
|
74
|
|
|
* |
|
75
|
|
|
* @param array $rules |
|
76
|
|
|
* |
|
77
|
|
|
* @return self |
|
78
|
|
|
*/ |
|
79
|
225 |
|
public function setRules(array $rules) |
|
80
|
|
|
{ |
|
81
|
225 |
|
$this->rules->set($rules); |
|
82
|
|
|
|
|
83
|
216 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
87
|
|
|
| Main Functions |
|
88
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
89
|
|
|
*/ |
|
90
|
|
|
/** |
|
91
|
|
|
* Sanitize the given data. |
|
92
|
|
|
* |
|
93
|
|
|
* @param array $data |
|
94
|
|
|
* @param array $rules |
|
95
|
|
|
* @param array $filters |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
225 |
|
public function sanitize(array $data, array $rules = [], array $filters = []) |
|
100
|
|
|
{ |
|
101
|
225 |
|
$this->setRules($rules); |
|
102
|
216 |
|
$this->setFilters($filters); |
|
103
|
|
|
|
|
104
|
216 |
|
foreach ($data as $name => $value) { |
|
105
|
216 |
|
$data[$name] = $this->sanitizeAttribute($name, $value); |
|
106
|
66 |
|
} |
|
107
|
|
|
|
|
108
|
198 |
|
return $data; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Sanitize the given attribute |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $attribute |
|
115
|
|
|
* @param mixed $value |
|
116
|
|
|
* |
|
117
|
|
|
* @return mixed |
|
118
|
|
|
*/ |
|
119
|
216 |
|
protected function sanitizeAttribute($attribute, $value) |
|
120
|
|
|
{ |
|
121
|
216 |
|
foreach ($this->rules->get($attribute) as $rule) { |
|
122
|
216 |
|
$value = $this->applyFilter($rule['name'], $value, $rule['options']); |
|
123
|
66 |
|
} |
|
124
|
|
|
|
|
125
|
198 |
|
return $value; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Apply the given filter by its name. |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $name |
|
132
|
|
|
* @param mixed $value |
|
133
|
|
|
* @param array $options |
|
134
|
|
|
* |
|
135
|
|
|
* @return mixed |
|
136
|
|
|
*/ |
|
137
|
216 |
|
protected function applyFilter($name, $value, $options = []) |
|
138
|
|
|
{ |
|
139
|
216 |
|
$this->hasFilter($name); |
|
140
|
|
|
|
|
141
|
207 |
|
if (empty($value)) return $value; |
|
142
|
|
|
|
|
143
|
207 |
|
$filter = $this->filters[$name]; |
|
144
|
|
|
|
|
145
|
207 |
|
if ($filter instanceof Closure) { |
|
146
|
27 |
|
return call_user_func_array($filter, compact('value', 'options')); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** @var Filterable $filterable */ |
|
150
|
189 |
|
$filterable = new $filter; |
|
151
|
|
|
|
|
152
|
189 |
|
return $filterable->filter($value, $options); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
156
|
|
|
| Check Functions |
|
157
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
158
|
|
|
*/ |
|
159
|
|
|
/** |
|
160
|
|
|
* Check has filter, if does not exist, throw an Exception. |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $name |
|
163
|
|
|
* |
|
164
|
|
|
* @throws Exceptions\FilterNotFoundException |
|
165
|
|
|
*/ |
|
166
|
216 |
|
private function hasFilter($name) |
|
167
|
|
|
{ |
|
168
|
216 |
|
if ( ! isset($this->filters[$name])) { |
|
169
|
9 |
|
throw new Exceptions\FilterNotFoundException( |
|
170
|
9 |
|
"No filter found by the name of $name" |
|
171
|
3 |
|
); |
|
172
|
|
|
} |
|
173
|
207 |
|
} |
|
174
|
|
|
|
|
175
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
176
|
|
|
| Other Functions |
|
177
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
178
|
|
|
*/ |
|
179
|
|
|
/** |
|
180
|
|
|
* Get default filters. |
|
181
|
|
|
* |
|
182
|
|
|
* @return array |
|
183
|
|
|
*/ |
|
184
|
315 |
|
private function getDefaultFilters() |
|
185
|
|
|
{ |
|
186
|
|
|
return [ |
|
187
|
315 |
|
'capitalize' => Filters\CapitalizeFilter::class, |
|
188
|
105 |
|
'email' => Filters\EmailFilter::class, |
|
189
|
105 |
|
'escape' => Filters\EscapeFilter::class, |
|
190
|
105 |
|
'format_date' => Filters\FormatDateFilter::class, |
|
191
|
105 |
|
'lowercase' => Filters\LowercaseFilter::class, |
|
192
|
105 |
|
'slug' => Filters\SlugFilter::class, |
|
193
|
105 |
|
'trim' => Filters\TrimFilter::class, |
|
194
|
105 |
|
'uppercase' => Filters\UppercaseFilter::class, |
|
195
|
105 |
|
'url' => Filters\UrlFilter::class, |
|
196
|
105 |
|
]; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|