1 | <?php |
||
8 | class Sanitizer |
||
9 | { |
||
10 | /** |
||
11 | * The value placeholder string. This allows the user to change the |
||
12 | * position of the value to sanitize in the arguments array. |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | const PLACEHOLDER_VALUE = '{{ VALUE }}'; |
||
17 | |||
18 | /** |
||
19 | * The global keyword. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | const GLOBAL_KEY = '*'; |
||
24 | |||
25 | /** @var array */ |
||
26 | protected $rules = []; |
||
27 | |||
28 | /** @var \Alfheim\Sanitizer\Registrar\RegistrarInterface */ |
||
29 | protected $registrar; |
||
30 | |||
31 | /** |
||
32 | * Statically create a new Sanitizer instance with a set of rules. |
||
33 | * |
||
34 | * @param string|array $ruleset The sanitation rules. |
||
35 | * |
||
36 | * @return \Alfheim\Sanitizer\Sanitizer |
||
37 | * |
||
38 | * @static |
||
39 | */ |
||
40 | 96 | public static function make($ruleset) |
|
44 | |||
45 | /** |
||
46 | * Add rules to the sanitizer. |
||
47 | * |
||
48 | * @param string|array $ruleset The sanitation rules. |
||
49 | * |
||
50 | * @return \Alfheim\Sanitizer\Sanitizer $this |
||
51 | */ |
||
52 | 132 | public function rules($ruleset) |
|
64 | |||
65 | /** |
||
66 | * Sanitize some data. |
||
67 | * |
||
68 | * @param mixed $data |
||
69 | * |
||
70 | * @return mixed |
||
71 | */ |
||
72 | 128 | public function sanitize($data) |
|
73 | { |
||
74 | 128 | if ($this->hasGlobals()) { |
|
75 | 60 | if (! is_array($data)) { |
|
76 | 48 | return $this->sanitizeValueFor(static::GLOBAL_KEY, $data); |
|
77 | } |
||
78 | |||
79 | 12 | foreach ($data as $key => $value) { |
|
80 | 12 | $data[$key] = $this->sanitizeValueFor(static::GLOBAL_KEY, $value); |
|
81 | 9 | } |
|
82 | 9 | } |
|
83 | |||
84 | 80 | foreach ($data as $key => $value) { |
|
85 | 80 | if (! $this->shouldSanitize($key)) { |
|
86 | 28 | continue; |
|
87 | } |
||
88 | |||
89 | 72 | $data[$key] = $this->sanitizeValueFor($key, $value); |
|
90 | 57 | } |
|
91 | |||
92 | 76 | return $data; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * Sanitize some data by reference. |
||
97 | * |
||
98 | * @param mixed &$data |
||
99 | */ |
||
100 | 8 | public function sanitizeByRef(&$data) |
|
104 | |||
105 | /** |
||
106 | * Set the registrar instance for the sanitizer. |
||
107 | * |
||
108 | * @param \Alfheim\Sanitizer\Registrar\RegistrarInterface $registrar |
||
109 | * |
||
110 | * @return \Alfheim\Sanitizer\Sanitizer $this |
||
111 | */ |
||
112 | 28 | public function setRegistrar(RegistrarInterface $registrar) |
|
118 | |||
119 | /** |
||
120 | * Check if a registrar has been set. |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | 120 | protected function hasRegistrar() |
|
128 | |||
129 | /** |
||
130 | * Check if global rules have been registered. |
||
131 | * |
||
132 | * @return bool |
||
133 | */ |
||
134 | 128 | protected function hasGlobals() |
|
138 | |||
139 | /** |
||
140 | * Check if a given key should be sanitized. |
||
141 | * |
||
142 | * @param string $key |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | 132 | protected function shouldSanitize($key) |
|
150 | |||
151 | /** |
||
152 | * Sanitize a single value for a given key. |
||
153 | * |
||
154 | * @param string $key |
||
155 | * @param mixed $value |
||
156 | * |
||
157 | * @return mixed |
||
158 | */ |
||
159 | 128 | protected function sanitizeValueFor($key, $value) |
|
170 | |||
171 | /** |
||
172 | * Resolve the callable for a given rule. |
||
173 | * |
||
174 | * @param mixed $value |
||
175 | * @param string $key |
||
176 | * |
||
177 | * @return callable |
||
178 | * |
||
179 | * @throws \InvalidArgumentException |
||
180 | */ |
||
181 | 128 | protected function getCallable($value, $key) |
|
211 | |||
212 | /** |
||
213 | * Build the arguments for a callback. |
||
214 | * |
||
215 | * @param mixed $value |
||
216 | * @param array|null $args |
||
217 | * |
||
218 | * @return array |
||
219 | */ |
||
220 | 124 | protected function buildArguments($value, array $args = null) |
|
221 | { |
||
222 | 124 | if (! $args) { |
|
223 | 116 | return (array) $value; |
|
224 | } |
||
225 | |||
226 | 28 | $valuePosition = array_search(static::PLACEHOLDER_VALUE, $args, true); |
|
227 | |||
228 | 28 | if ($valuePosition === false) { |
|
229 | 4 | return array_merge((array) $value, $args); |
|
230 | } else { |
||
231 | 24 | $args[$valuePosition] = $value; |
|
232 | } |
||
233 | |||
234 | 24 | return $args; |
|
235 | } |
||
236 | |||
237 | /** |
||
238 | * Add a rule to the sanitizer factory. |
||
239 | * |
||
240 | * @param string $key |
||
241 | * @param string|array|\Closure $rules |
||
242 | * |
||
243 | * @return void |
||
244 | * |
||
245 | * @throws \InvalidArgumentException |
||
246 | */ |
||
247 | 132 | protected function addRule($key, $rules) |
|
257 | |||
258 | /** |
||
259 | * Build a valid set of rules. |
||
260 | * |
||
261 | * @param string|array|\Closure $rules |
||
262 | * |
||
263 | * @return array |
||
264 | */ |
||
265 | 132 | protected function buildRules($rules) |
|
288 | |||
289 | /** |
||
290 | * Get the filter method name which will be called on an object. |
||
291 | * |
||
292 | * @param string $value |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | 4 | protected function getFilterMethodName($value) |
|
302 | } |
||
303 |