@@ -60,9 +60,9 @@ discard block |
||
60 | 60 | { |
61 | 61 | $toAccessor = $this->nestedAccessorFactory->create($result, $this->pathDelimiter); |
62 | 62 | |
63 | - foreach($schema as $keyTo => $keyFrom) { |
|
63 | + foreach ($schema as $keyTo => $keyFrom) { |
|
64 | 64 | $value = $this->getValue($source, $keyFrom); |
65 | - if($keyTo === '') { |
|
65 | + if ($keyTo === '') { |
|
66 | 66 | return $value; |
67 | 67 | } |
68 | 68 | $toAccessor->set($keyTo, $value); |
@@ -77,19 +77,19 @@ discard block |
||
77 | 77 | */ |
78 | 78 | public function getValue($source, $key) |
79 | 79 | { |
80 | - if($key === '' || $key === null) { |
|
80 | + if ($key === '' || $key === null) { |
|
81 | 81 | return $source; |
82 | 82 | } |
83 | 83 | |
84 | - if($source === null || (!is_array($source) && !is_object($source))) { |
|
84 | + if ($source === null || (!is_array($source) && !is_object($source))) { |
|
85 | 85 | return $this->getValueFromUnsupportedSource($source, $key); |
86 | 86 | } |
87 | 87 | |
88 | - if(is_string($key)) { |
|
88 | + if (is_string($key)) { |
|
89 | 89 | return $this->getValueByKey($source, $key); |
90 | 90 | } |
91 | 91 | |
92 | - if(is_array($key)) { |
|
92 | + if (is_array($key)) { |
|
93 | 93 | return $this->getValueByFilters($source, $key); |
94 | 94 | } |
95 | 95 | |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | $key, |
138 | 138 | $this->needToThrow(SchematorException::CANNOT_GET_VALUE) |
139 | 139 | ); |
140 | - } catch(NestedAccessorException $e) { |
|
140 | + } catch (NestedAccessorException $e) { |
|
141 | 141 | throw SchematorException::createAsCannotGetValue($source, $key, $e); |
142 | 142 | } |
143 | 143 | } |
@@ -152,13 +152,13 @@ discard block |
||
152 | 152 | protected function getValueByFilters($source, array $filters) |
153 | 153 | { |
154 | 154 | $result = $source; |
155 | - foreach($filters as $filterConfig) { |
|
156 | - if(is_string($filterConfig)) { |
|
155 | + foreach ($filters as $filterConfig) { |
|
156 | + if (is_string($filterConfig)) { |
|
157 | 157 | $result = $this->getValue($result, $filterConfig); |
158 | - } elseif(is_array($filterConfig)) { |
|
158 | + } elseif (is_array($filterConfig)) { |
|
159 | 159 | $result = $this->runFilter($filterConfig, $result, $source); |
160 | 160 | } else { |
161 | - if($this->needToThrow(SchematorException::UNSUPPORTED_FILTER_CONFIG_TYPE)) { |
|
161 | + if ($this->needToThrow(SchematorException::UNSUPPORTED_FILTER_CONFIG_TYPE)) { |
|
162 | 162 | throw SchematorException::createAsUnsupportedFilterConfigType($filterConfig); |
163 | 163 | } |
164 | 164 | $result = null; |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | */ |
178 | 178 | protected function getValueFromUnsupportedSource($source, $key) |
179 | 179 | { |
180 | - if($this->needToThrow(SchematorException::UNSUPPORTED_SOURCE_TYPE)) { |
|
180 | + if ($this->needToThrow(SchematorException::UNSUPPORTED_SOURCE_TYPE)) { |
|
181 | 181 | throw SchematorException::createAsUnsupportedSourceType($source, $key); |
182 | 182 | } |
183 | 183 | return null; |
@@ -192,7 +192,7 @@ discard block |
||
192 | 192 | */ |
193 | 193 | protected function getValueByUnsupportedKey($source, $key) |
194 | 194 | { |
195 | - if($this->needToThrow(SchematorException::UNSUPPORTED_KEY_TYPE)) { |
|
195 | + if ($this->needToThrow(SchematorException::UNSUPPORTED_KEY_TYPE)) { |
|
196 | 196 | throw SchematorException::createAsUnsupportedKeyType($source, $key); |
197 | 197 | } |
198 | 198 | return null; |
@@ -210,7 +210,7 @@ discard block |
||
210 | 210 | { |
211 | 211 | $filterName = strval(array_shift($filterConfig)); |
212 | 212 | |
213 | - if( |
|
213 | + if ( |
|
214 | 214 | !isset($this->filterMap[$filterName]) |
215 | 215 | && $this->needToThrow(SchematorException::FILTER_NOT_FOUND) |
216 | 216 | ) { |
@@ -222,13 +222,13 @@ discard block |
||
222 | 222 | new FilterContext($this, $source, $rootSource, $filterConfig), |
223 | 223 | ...$filterConfig |
224 | 224 | ); |
225 | - } catch(SchematorException $e) { |
|
226 | - if($this->needToThrow($e->getCode())) { |
|
225 | + } catch (SchematorException $e) { |
|
226 | + if ($this->needToThrow($e->getCode())) { |
|
227 | 227 | throw $e; |
228 | 228 | } |
229 | 229 | return null; |
230 | - } catch(Throwable $e) { |
|
231 | - if($this->needToThrow(SchematorException::FILTER_ERROR)) { |
|
230 | + } catch (Throwable $e) { |
|
231 | + if ($this->needToThrow(SchematorException::FILTER_ERROR)) { |
|
232 | 232 | throw SchematorException::createAsFilterError($filterName, $filterConfig, $source, $e); |
233 | 233 | } |
234 | 234 | return null; |
@@ -49,10 +49,10 @@ discard block |
||
49 | 49 | public static function date(FilterContextInterface $context, string $format, ?int $timezone = null) |
50 | 50 | { |
51 | 51 | $source = $context->getSource(); |
52 | - if($source === null) { |
|
52 | + if ($source === null) { |
|
53 | 53 | throw SchematorException::createAsFilterError('date', $context->getConfig(), $source); |
54 | 54 | } |
55 | - if($timezone === null) { |
|
55 | + if ($timezone === null) { |
|
56 | 56 | return date($format, intval($source)); |
57 | 57 | } |
58 | 58 | return gmdate($format, $source+3600*$timezone); |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | public static function implode(FilterContextInterface $context, string $delimiter = ', '): ?string |
69 | 69 | { |
70 | 70 | $source = $context->getSource(); |
71 | - if($source === null || !is_array($source)) { |
|
71 | + if ($source === null || !is_array($source)) { |
|
72 | 72 | throw SchematorException::createAsFilterError('implode', $context->getConfig(), $source); |
73 | 73 | } |
74 | 74 | return implode($delimiter, $source); |
@@ -84,10 +84,10 @@ discard block |
||
84 | 84 | public static function explode(FilterContextInterface $context, string $delimiter = ', ') |
85 | 85 | { |
86 | 86 | $source = $context->getSource(); |
87 | - if($source === null || !is_scalar($source)) { |
|
87 | + if ($source === null || !is_scalar($source)) { |
|
88 | 88 | throw SchematorException::createAsFilterError('explode', $context->getConfig(), $source); |
89 | 89 | } |
90 | - return explode($delimiter, (string)$source); |
|
90 | + return explode($delimiter, (string) $source); |
|
91 | 91 | } |
92 | 92 | |
93 | 93 | /** |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | public static function sum(FilterContextInterface $context) |
100 | 100 | { |
101 | 101 | $source = $context->getSource(); |
102 | - if($source === null || !is_array($source)) { |
|
102 | + if ($source === null || !is_array($source)) { |
|
103 | 103 | throw SchematorException::createAsFilterError('sum', $context->getConfig(), $source); |
104 | 104 | } |
105 | 105 | return array_sum($source); |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | public static function average(FilterContextInterface $context) |
115 | 115 | { |
116 | 116 | $source = $context->getSource(); |
117 | - if($source === null || !is_array($source)) { |
|
117 | + if ($source === null || !is_array($source)) { |
|
118 | 118 | throw SchematorException::createAsFilterError('average', $context->getConfig(), $source); |
119 | 119 | } |
120 | 120 | return array_sum($source)/count($source); |
@@ -130,25 +130,25 @@ discard block |
||
130 | 130 | public static function filter(FilterContextInterface $context, $filterConfig): ?array |
131 | 131 | { |
132 | 132 | $source = $context->getSource(); |
133 | - if($source === null || !is_array($source)) { |
|
133 | + if ($source === null || !is_array($source)) { |
|
134 | 134 | throw SchematorException::createAsFilterError('filter', $context->getConfig(), $source); |
135 | 135 | } |
136 | 136 | |
137 | - if(is_callable($filterConfig)) { |
|
137 | + if (is_callable($filterConfig)) { |
|
138 | 138 | return array_values(array_filter($source, $filterConfig)); |
139 | 139 | } |
140 | 140 | |
141 | 141 | $result = []; |
142 | 142 | |
143 | - foreach($source as $item) { |
|
144 | - foreach($filterConfig as $args) { |
|
145 | - if(!is_array($args)) { |
|
143 | + foreach ($source as $item) { |
|
144 | + foreach ($filterConfig as $args) { |
|
145 | + if (!is_array($args)) { |
|
146 | 146 | throw SchematorException::createAsFilterError('filter', $context->getConfig(), $source); |
147 | 147 | } |
148 | 148 | |
149 | 149 | $rule = array_shift($args); |
150 | 150 | |
151 | - if(RuleHelper::check($item, $rule, $args)) { |
|
151 | + if (RuleHelper::check($item, $rule, $args)) { |
|
152 | 152 | $result[] = $item; |
153 | 153 | break; |
154 | 154 | } |
@@ -168,10 +168,10 @@ discard block |
||
168 | 168 | public static function sort(FilterContextInterface $context, ?callable $sortCallback = null): ?array |
169 | 169 | { |
170 | 170 | $source = $context->getSource(); |
171 | - if($source === null || !is_array($source)) { |
|
171 | + if ($source === null || !is_array($source)) { |
|
172 | 172 | throw SchematorException::createAsFilterError('sort', $context->getConfig(), $source); |
173 | 173 | } |
174 | - if($sortCallback !== null) { |
|
174 | + if ($sortCallback !== null) { |
|
175 | 175 | usort($source, $sortCallback); |
176 | 176 | } else { |
177 | 177 | sort($source); |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | public static function rsort(FilterContextInterface $context): ?array |
188 | 188 | { |
189 | 189 | $source = $context->getSource(); |
190 | - if($source === null || !is_array($source)) { |
|
190 | + if ($source === null || !is_array($source)) { |
|
191 | 191 | throw SchematorException::createAsFilterError('rsort', $context->getConfig(), $source); |
192 | 192 | } |
193 | 193 | |
@@ -204,7 +204,7 @@ discard block |
||
204 | 204 | public static function path(FilterContextInterface $context) |
205 | 205 | { |
206 | 206 | $source = $context->getSource(); |
207 | - if($source === null) { |
|
207 | + if ($source === null) { |
|
208 | 208 | throw SchematorException::createAsFilterError('path', $context->getConfig(), $source); |
209 | 209 | } |
210 | 210 | return $context->getSchemator()->getValue($context->getRootSource(), $source); |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | public static function flatten(FilterContextInterface $context): ?array |
220 | 220 | { |
221 | 221 | $source = $context->getSource(); |
222 | - if($source === null || !is_array($source)) { |
|
222 | + if ($source === null || !is_array($source)) { |
|
223 | 223 | throw SchematorException::createAsFilterError('flatten', $context->getConfig(), $source); |
224 | 224 | } |
225 | 225 | return ArrHelper::flatten($source); |
@@ -235,37 +235,37 @@ discard block |
||
235 | 235 | public static function replace(FilterContextInterface $context, array $rules) |
236 | 236 | { |
237 | 237 | $source = $context->getSource(); |
238 | - if($source === null) { |
|
238 | + if ($source === null) { |
|
239 | 239 | throw SchematorException::createAsFilterError('replace', $context->getConfig(), $source); |
240 | 240 | } |
241 | 241 | |
242 | 242 | $isArray = is_array($source); |
243 | 243 | |
244 | - if(!$isArray) { |
|
244 | + if (!$isArray) { |
|
245 | 245 | $source = [$source]; |
246 | 246 | } |
247 | 247 | |
248 | 248 | $result = []; |
249 | 249 | |
250 | - foreach($source as $item) { |
|
250 | + foreach ($source as $item) { |
|
251 | 251 | $isReplaced = false; |
252 | 252 | $elseValue = $item; |
253 | 253 | |
254 | - foreach($rules as $args) { |
|
255 | - if(!is_array($args)) { |
|
254 | + foreach ($rules as $args) { |
|
255 | + if (!is_array($args)) { |
|
256 | 256 | throw SchematorException::createAsFilterError('replace', $context->getConfig(), $source); |
257 | 257 | } |
258 | 258 | |
259 | 259 | $value = array_shift($args); |
260 | 260 | $rule = array_shift($args); |
261 | 261 | |
262 | - if($rule === 'else') { |
|
262 | + if ($rule === 'else') { |
|
263 | 263 | $elseValue = $value; |
264 | 264 | } |
265 | 265 | |
266 | 266 | $replace = null; |
267 | 267 | |
268 | - if(RuleHelper::check($item, $rule, $args)) { |
|
268 | + if (RuleHelper::check($item, $rule, $args)) { |
|
269 | 269 | $replace = $value; |
270 | 270 | $isReplaced = true; |
271 | 271 | |
@@ -274,12 +274,12 @@ discard block |
||
274 | 274 | } |
275 | 275 | } |
276 | 276 | |
277 | - if(!$isReplaced) { |
|
277 | + if (!$isReplaced) { |
|
278 | 278 | $result[] = $elseValue; |
279 | 279 | } |
280 | 280 | } |
281 | 281 | |
282 | - if(!$isArray) { |
|
282 | + if (!$isArray) { |
|
283 | 283 | $result = $result[0]; |
284 | 284 | } |
285 | 285 |