@@ -40,7 +40,7 @@ |
||
| 40 | 40 | */ |
| 41 | 41 | public function withFilters(iterable $filters): SchematorBuilderInterface |
| 42 | 42 | { |
| 43 | - foreach($filters as $filterName => $filter) { |
|
| 43 | + foreach ($filters as $filterName => $filter) { |
|
| 44 | 44 | $this->schemator->addFilter($filterName, $filter); |
| 45 | 45 | } |
| 46 | 46 | return $this; |
@@ -29,11 +29,11 @@ discard block |
||
| 29 | 29 | */ |
| 30 | 30 | public function __construct(&$source, string $pathDelimiter = '.') |
| 31 | 31 | { |
| 32 | - if($source === null) { |
|
| 32 | + if ($source === null) { |
|
| 33 | 33 | $source = []; |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | - if(is_scalar($source)) { |
|
| 36 | + if (is_scalar($source)) { |
|
| 37 | 37 | throw NestedAccessorException::createAsSourceIsScalar($source); |
| 38 | 38 | } |
| 39 | 39 | |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | public function get(?string $path = null, bool $strict = true) |
| 53 | 53 | { |
| 54 | 54 | // when path is not specified |
| 55 | - if($path === null || $path === '') { |
|
| 55 | + if ($path === null || $path === '') { |
|
| 56 | 56 | // let's return the full source |
| 57 | 57 | return $this->source; |
| 58 | 58 | } |
@@ -70,7 +70,7 @@ discard block |
||
| 70 | 70 | ); |
| 71 | 71 | |
| 72 | 72 | // when strict mode is on and we got errors |
| 73 | - if($strict && $errorsCount) { |
|
| 73 | + if ($strict && $errorsCount) { |
|
| 74 | 74 | throw NestedAccessorException::createAsCannotGetValue($path, $errorsCount); |
| 75 | 75 | } |
| 76 | 76 | |
@@ -101,9 +101,9 @@ discard block |
||
| 101 | 101 | protected function _get($source, array $path, &$result, int &$errorsCount): void |
| 102 | 102 | { |
| 103 | 103 | // if path stack is empty — we reached target value of given path in source argument |
| 104 | - if(!count($path)) { |
|
| 104 | + if (!count($path)) { |
|
| 105 | 105 | // so if result is multiple |
| 106 | - if(is_array($result)) { |
|
| 106 | + if (is_array($result)) { |
|
| 107 | 107 | // we append source to result |
| 108 | 108 | $result[] = $source; |
| 109 | 109 | } else { |
@@ -115,11 +115,11 @@ discard block |
||
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | // let's iterate every path part from stack |
| 118 | - while(count($path)) { |
|
| 118 | + while (count($path)) { |
|
| 119 | 119 | $key = array_pop($path); |
| 120 | 120 | |
| 121 | - if(is_array($source)) { |
|
| 122 | - if(!array_key_exists($key, $source)) { |
|
| 121 | + if (is_array($source)) { |
|
| 122 | + if (!array_key_exists($key, $source)) { |
|
| 123 | 123 | // path part key is missing in source array |
| 124 | 124 | $errorsCount++; |
| 125 | 125 | // we cannot go deeper |
@@ -127,8 +127,8 @@ discard block |
||
| 127 | 127 | } |
| 128 | 128 | // go to the next nested level |
| 129 | 129 | $source = $source[$key]; |
| 130 | - } elseif(is_object($source)) { |
|
| 131 | - if(!property_exists($source, $key)) { |
|
| 130 | + } elseif (is_object($source)) { |
|
| 131 | + if (!property_exists($source, $key)) { |
|
| 132 | 132 | // path part key is missing in source object |
| 133 | 133 | $errorsCount++; |
| 134 | 134 | // we cannot go deeper |
@@ -145,13 +145,13 @@ discard block |
||
| 145 | 145 | |
| 146 | 146 | // when it's not the last iteration of the stack |
| 147 | 147 | // and the source is non-associative array (list) |
| 148 | - if(count($path) && is_array($source) && !ArrHelper::isAssoc($source)) { |
|
| 148 | + if (count($path) && is_array($source) && !ArrHelper::isAssoc($source)) { |
|
| 149 | 149 | // the result will be multiple |
| 150 | - if(!is_array($result)) { |
|
| 150 | + if (!is_array($result)) { |
|
| 151 | 151 | $result = []; |
| 152 | 152 | } |
| 153 | 153 | // and we need to use recursive call for each item of this array |
| 154 | - foreach($source as $item) { |
|
| 154 | + foreach ($source as $item) { |
|
| 155 | 155 | $this->_get($item, $path, $result, $errorsCount); |
| 156 | 156 | } |
| 157 | 157 | // we don't need to do something in this recursive branch |
@@ -177,15 +177,15 @@ discard block |
||
| 177 | 177 | { |
| 178 | 178 | $temp = &$source; |
| 179 | 179 | // let's iterate every path part to go deeper into nesting |
| 180 | - foreach($path as $key) { |
|
| 181 | - if(isset($temp) && is_scalar($temp)) { |
|
| 180 | + foreach ($path as $key) { |
|
| 181 | + if (isset($temp) && is_scalar($temp)) { |
|
| 182 | 182 | // value in the middle of the path must me an array |
| 183 | 183 | $temp = []; |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | // go to the next nested level |
| 187 | - if(is_object($temp)) { |
|
| 188 | - if($strict && !property_exists($temp, $key)) { |
|
| 187 | + if (is_object($temp)) { |
|
| 188 | + if ($strict && !property_exists($temp, $key)) { |
|
| 189 | 189 | throw NestedAccessorException::createAsCannotSetValue($key); |
| 190 | 190 | } |
| 191 | 191 | $temp = &$temp->{$key}; |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | */ |
| 33 | 33 | public function generate(iterable $source, array $schema): Generator |
| 34 | 34 | { |
| 35 | - foreach($source as $item) { |
|
| 35 | + foreach ($source as $item) { |
|
| 36 | 36 | yield $this->schemator->convert($item, $schema); |
| 37 | 37 | } |
| 38 | 38 | } |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | $gen = $this->generate($source, $schema); |
| 46 | 46 | $result = []; |
| 47 | 47 | |
| 48 | - foreach($gen as $item) { |
|
| 48 | + foreach ($gen as $item) { |
|
| 49 | 49 | $result[] = $item; |
| 50 | 50 | } |
| 51 | 51 | |
@@ -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 | |