1 | <?php namespace Arcanedev\Sanitizer; |
||
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 = []) |
|
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) |
|
71 | |||
72 | /** |
||
73 | * Set rules. |
||
74 | * |
||
75 | * @param array $rules |
||
76 | * |
||
77 | * @return self |
||
78 | */ |
||
79 | 225 | public function setRules(array $rules) |
|
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 = []) |
|
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) |
|
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 = []) |
|
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) |
|
174 | |||
175 | /* ------------------------------------------------------------------------------------------------ |
||
176 | | Other Functions |
||
177 | | ------------------------------------------------------------------------------------------------ |
||
178 | */ |
||
179 | /** |
||
180 | * Get default filters. |
||
181 | * |
||
182 | * @return array |
||
183 | */ |
||
184 | 315 | private function getDefaultFilters() |
|
198 | } |
||
199 |