@@ -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 | |
@@ -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::createAsBadFilterSource($context); |
| 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::createAsBadFilterSource($context); |
| 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::createAsBadFilterSource($context); |
| 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::createAsBadFilterSource($context); |
| 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::createAsBadFilterSource($context); |
| 119 | 119 | } |
| 120 | 120 | return array_sum($source)/count($source); |
@@ -130,29 +130,29 @@ 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::createAsBadFilterSource($context); |
| 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 | - if(!is_array($filterConfig)) { |
|
| 141 | + if (!is_array($filterConfig)) { |
|
| 142 | 142 | throw SchematorException::createAsBadFilterConfig($context); |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | $result = []; |
| 146 | 146 | |
| 147 | - foreach($source as $item) { |
|
| 148 | - foreach($filterConfig as $args) { |
|
| 149 | - if(!is_array($args)) { |
|
| 147 | + foreach ($source as $item) { |
|
| 148 | + foreach ($filterConfig as $args) { |
|
| 149 | + if (!is_array($args)) { |
|
| 150 | 150 | throw SchematorException::createAsBadFilterConfig($context); |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | $rule = array_shift($args); |
| 154 | 154 | |
| 155 | - if(RuleHelper::check($item, $rule, $args)) { |
|
| 155 | + if (RuleHelper::check($item, $rule, $args)) { |
|
| 156 | 156 | $result[] = $item; |
| 157 | 157 | break; |
| 158 | 158 | } |
@@ -172,10 +172,10 @@ discard block |
||
| 172 | 172 | public static function sort(FilterContextInterface $context, ?callable $sortCallback = null): ?array |
| 173 | 173 | { |
| 174 | 174 | $source = $context->getSource(); |
| 175 | - if($source === null || !is_array($source)) { |
|
| 175 | + if ($source === null || !is_array($source)) { |
|
| 176 | 176 | throw SchematorException::createAsBadFilterSource($context); |
| 177 | 177 | } |
| 178 | - if($sortCallback !== null) { |
|
| 178 | + if ($sortCallback !== null) { |
|
| 179 | 179 | usort($source, $sortCallback); |
| 180 | 180 | } else { |
| 181 | 181 | sort($source); |
@@ -192,7 +192,7 @@ discard block |
||
| 192 | 192 | public static function rsort(FilterContextInterface $context): ?array |
| 193 | 193 | { |
| 194 | 194 | $source = $context->getSource(); |
| 195 | - if($source === null || !is_array($source)) { |
|
| 195 | + if ($source === null || !is_array($source)) { |
|
| 196 | 196 | throw SchematorException::createAsBadFilterSource($context); |
| 197 | 197 | } |
| 198 | 198 | |
@@ -209,7 +209,7 @@ discard block |
||
| 209 | 209 | public static function path(FilterContextInterface $context) |
| 210 | 210 | { |
| 211 | 211 | $source = $context->getSource(); |
| 212 | - if($source === null) { |
|
| 212 | + if ($source === null) { |
|
| 213 | 213 | throw SchematorException::createAsBadFilterSource($context); |
| 214 | 214 | } |
| 215 | 215 | return $context->getSchemator()->getValue($context->getRootSource(), $source); |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | public static function flatten(FilterContextInterface $context): ?array |
| 225 | 225 | { |
| 226 | 226 | $source = $context->getSource(); |
| 227 | - if($source === null || !is_array($source)) { |
|
| 227 | + if ($source === null || !is_array($source)) { |
|
| 228 | 228 | throw SchematorException::createAsBadFilterSource($context); |
| 229 | 229 | } |
| 230 | 230 | return ArrHelper::flatten($source); |
@@ -240,37 +240,37 @@ discard block |
||
| 240 | 240 | public static function replace(FilterContextInterface $context, array $rules) |
| 241 | 241 | { |
| 242 | 242 | $source = $context->getSource(); |
| 243 | - if($source === null) { |
|
| 243 | + if ($source === null) { |
|
| 244 | 244 | throw SchematorException::createAsBadFilterSource($context); |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | $isArray = is_array($source); |
| 248 | 248 | |
| 249 | - if(!$isArray) { |
|
| 249 | + if (!$isArray) { |
|
| 250 | 250 | $source = [$source]; |
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | $result = []; |
| 254 | 254 | |
| 255 | - foreach($source as $item) { |
|
| 255 | + foreach ($source as $item) { |
|
| 256 | 256 | $isReplaced = false; |
| 257 | 257 | $elseValue = $item; |
| 258 | 258 | |
| 259 | - foreach($rules as $args) { |
|
| 260 | - if(!is_array($args)) { |
|
| 259 | + foreach ($rules as $args) { |
|
| 260 | + if (!is_array($args)) { |
|
| 261 | 261 | throw SchematorException::createAsBadFilterConfig($context); |
| 262 | 262 | } |
| 263 | 263 | |
| 264 | 264 | $value = array_shift($args); |
| 265 | 265 | $rule = array_shift($args); |
| 266 | 266 | |
| 267 | - if($rule === 'else') { |
|
| 267 | + if ($rule === 'else') { |
|
| 268 | 268 | $elseValue = $value; |
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | $replace = null; |
| 272 | 272 | |
| 273 | - if(RuleHelper::check($item, $rule, $args)) { |
|
| 273 | + if (RuleHelper::check($item, $rule, $args)) { |
|
| 274 | 274 | $replace = $value; |
| 275 | 275 | $isReplaced = true; |
| 276 | 276 | |
@@ -279,12 +279,12 @@ discard block |
||
| 279 | 279 | } |
| 280 | 280 | } |
| 281 | 281 | |
| 282 | - if(!$isReplaced) { |
|
| 282 | + if (!$isReplaced) { |
|
| 283 | 283 | $result[] = $elseValue; |
| 284 | 284 | } |
| 285 | 285 | } |
| 286 | 286 | |
| 287 | - if(!$isArray) { |
|
| 287 | + if (!$isArray) { |
|
| 288 | 288 | $result = $result[0]; |
| 289 | 289 | } |
| 290 | 290 | |
@@ -61,9 +61,9 @@ discard block |
||
| 61 | 61 | { |
| 62 | 62 | $toAccessor = $this->nestedAccessorFactory->create($result, $this->pathDelimiter); |
| 63 | 63 | |
| 64 | - foreach($schema as $keyTo => $keyFrom) { |
|
| 64 | + foreach ($schema as $keyTo => $keyFrom) { |
|
| 65 | 65 | $value = $this->getValue($source, $keyFrom); |
| 66 | - if($keyTo === '') { |
|
| 66 | + if ($keyTo === '') { |
|
| 67 | 67 | return $value; |
| 68 | 68 | } |
| 69 | 69 | $toAccessor->set($keyTo, $value); |
@@ -78,19 +78,19 @@ discard block |
||
| 78 | 78 | */ |
| 79 | 79 | public function getValue($source, $key) |
| 80 | 80 | { |
| 81 | - if($key === '' || $key === null) { |
|
| 81 | + if ($key === '' || $key === null) { |
|
| 82 | 82 | return $source; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - if($source === null || (!is_array($source) && !is_object($source))) { |
|
| 85 | + if ($source === null || (!is_array($source) && !is_object($source))) { |
|
| 86 | 86 | return $this->getValueFromUnsupportedSource($source, $key); |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | - if(is_string($key)) { |
|
| 89 | + if (is_string($key)) { |
|
| 90 | 90 | return $this->getValueByKey($source, $key); |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - if(is_array($key)) { |
|
| 93 | + if (is_array($key)) { |
|
| 94 | 94 | return $this->getValueByFilters($source, $key); |
| 95 | 95 | } |
| 96 | 96 | |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | $key, |
| 139 | 139 | $this->needToThrow(SchematorException::CANNOT_GET_VALUE) |
| 140 | 140 | ); |
| 141 | - } catch(NestedAccessorException $e) { |
|
| 141 | + } catch (NestedAccessorException $e) { |
|
| 142 | 142 | throw SchematorException::createAsCannotGetValue($source, $key, $e); |
| 143 | 143 | } |
| 144 | 144 | } |
@@ -153,13 +153,13 @@ discard block |
||
| 153 | 153 | protected function getValueByFilters($source, array $filters) |
| 154 | 154 | { |
| 155 | 155 | $result = $source; |
| 156 | - foreach($filters as $filterConfig) { |
|
| 157 | - if(is_string($filterConfig)) { |
|
| 156 | + foreach ($filters as $filterConfig) { |
|
| 157 | + if (is_string($filterConfig)) { |
|
| 158 | 158 | $result = $this->getValue($result, $filterConfig); |
| 159 | - } elseif(is_array($filterConfig)) { |
|
| 159 | + } elseif (is_array($filterConfig)) { |
|
| 160 | 160 | $result = $this->runFilter($filterConfig, $result, $source); |
| 161 | 161 | } else { |
| 162 | - if($this->needToThrow(SchematorException::UNSUPPORTED_FILTER_CONFIG_TYPE)) { |
|
| 162 | + if ($this->needToThrow(SchematorException::UNSUPPORTED_FILTER_CONFIG_TYPE)) { |
|
| 163 | 163 | throw SchematorException::createAsUnsupportedFilterConfigType($filterConfig); |
| 164 | 164 | } |
| 165 | 165 | $result = null; |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | */ |
| 179 | 179 | protected function getValueFromUnsupportedSource($source, $key) |
| 180 | 180 | { |
| 181 | - if($this->needToThrow(SchematorException::UNSUPPORTED_SOURCE_TYPE)) { |
|
| 181 | + if ($this->needToThrow(SchematorException::UNSUPPORTED_SOURCE_TYPE)) { |
|
| 182 | 182 | throw SchematorException::createAsUnsupportedSourceType($source, $key); |
| 183 | 183 | } |
| 184 | 184 | return null; |
@@ -193,7 +193,7 @@ discard block |
||
| 193 | 193 | */ |
| 194 | 194 | protected function getValueByUnsupportedKey($source, $key) |
| 195 | 195 | { |
| 196 | - if($this->needToThrow(SchematorException::UNSUPPORTED_KEY_TYPE)) { |
|
| 196 | + if ($this->needToThrow(SchematorException::UNSUPPORTED_KEY_TYPE)) { |
|
| 197 | 197 | throw SchematorException::createAsUnsupportedKeyType($source, $key); |
| 198 | 198 | } |
| 199 | 199 | return null; |
@@ -211,7 +211,7 @@ discard block |
||
| 211 | 211 | { |
| 212 | 212 | $filterName = strval(array_shift($filterConfig)); |
| 213 | 213 | |
| 214 | - if( |
|
| 214 | + if ( |
|
| 215 | 215 | !isset($this->filterMap[$filterName]) |
| 216 | 216 | && $this->needToThrow(SchematorException::FILTER_NOT_FOUND) |
| 217 | 217 | ) { |
@@ -224,18 +224,18 @@ discard block |
||
| 224 | 224 | $filterContext, |
| 225 | 225 | ...$filterConfig |
| 226 | 226 | ); |
| 227 | - } catch(SchematorException $e) { |
|
| 228 | - if($this->needToThrow($e->getCode())) { |
|
| 227 | + } catch (SchematorException $e) { |
|
| 228 | + if ($this->needToThrow($e->getCode())) { |
|
| 229 | 229 | throw $e; |
| 230 | 230 | } |
| 231 | 231 | return null; |
| 232 | - } catch(TypeError $e) { |
|
| 233 | - if($this->needToThrow(SchematorException::BAD_FILTER_CONFIG)) { |
|
| 232 | + } catch (TypeError $e) { |
|
| 233 | + if ($this->needToThrow(SchematorException::BAD_FILTER_CONFIG)) { |
|
| 234 | 234 | throw SchematorException::createAsBadFilterConfig($filterContext, $e); |
| 235 | 235 | } |
| 236 | 236 | return null; |
| 237 | - } catch(Throwable $e) { |
|
| 238 | - if($this->needToThrow(SchematorException::FILTER_ERROR)) { |
|
| 237 | + } catch (Throwable $e) { |
|
| 238 | + if ($this->needToThrow(SchematorException::FILTER_ERROR)) { |
|
| 239 | 239 | throw SchematorException::createAsFilterError($filterContext); |
| 240 | 240 | } |
| 241 | 241 | return null; |