@@ -23,11 +23,11 @@ discard block |
||
| 23 | 23 | { |
| 24 | 24 | $schemator = new Schemator(); |
| 25 | 25 | |
| 26 | - if($withBaseFilters) { |
|
| 26 | + if ($withBaseFilters) { |
|
| 27 | 27 | static::addBaseFilters($schemator); |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | - foreach($extraFilters as $filterName => $filterCallback) { |
|
| 30 | + foreach ($extraFilters as $filterName => $filterCallback) { |
|
| 31 | 31 | $schemator->addFilter($filterName, $filterCallback); |
| 32 | 32 | } |
| 33 | 33 | |
@@ -70,10 +70,10 @@ discard block |
||
| 70 | 70 | $schemator->addFilter( |
| 71 | 71 | 'date', |
| 72 | 72 | function(Schemator $schemator, ?int $source, array $rootSource, string $format, ?int $timezone = null) { |
| 73 | - if($source === null) { |
|
| 73 | + if ($source === null) { |
|
| 74 | 74 | return null; |
| 75 | 75 | } |
| 76 | - if($timezone === null) { |
|
| 76 | + if ($timezone === null) { |
|
| 77 | 77 | return date($format, $source); |
| 78 | 78 | } |
| 79 | 79 | return gmdate($format, $source+3600*$timezone); |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | $schemator->addFilter( |
| 84 | 84 | 'implode', |
| 85 | 85 | function(Schemator $schemator, ?array $source, array $rootSource, string $delimiter = ', ') { |
| 86 | - if($source === null) { |
|
| 86 | + if ($source === null) { |
|
| 87 | 87 | return null; |
| 88 | 88 | } |
| 89 | 89 | return implode($delimiter, $source); |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | $schemator->addFilter( |
| 94 | 94 | 'explode', |
| 95 | 95 | function(Schemator $schemator, ?string $source, array $rootSource, string $delimiter = ', ') { |
| 96 | - if($source === null) { |
|
| 96 | + if ($source === null) { |
|
| 97 | 97 | return null; |
| 98 | 98 | } |
| 99 | 99 | return explode($delimiter, $source); |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | $schemator->addFilter( |
| 104 | 104 | 'sum', |
| 105 | 105 | function(Schemator $schemator, ?array $source) { |
| 106 | - if($source === null) { |
|
| 106 | + if ($source === null) { |
|
| 107 | 107 | return null; |
| 108 | 108 | } |
| 109 | 109 | return array_sum($source); |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | $schemator->addFilter( |
| 114 | 114 | 'average', |
| 115 | 115 | function(Schemator $schemator, ?array $source) { |
| 116 | - if($source === null) { |
|
| 116 | + if ($source === null) { |
|
| 117 | 117 | return null; |
| 118 | 118 | } |
| 119 | 119 | return array_sum($source)/count($source); |
@@ -123,21 +123,21 @@ discard block |
||
| 123 | 123 | $schemator->addFilter( |
| 124 | 124 | 'filter', |
| 125 | 125 | function(Schemator $schemator, ?array $source, array $rootSource, $filterConfig) { |
| 126 | - if($source === null) { |
|
| 126 | + if ($source === null) { |
|
| 127 | 127 | return null; |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | - if(is_callable($filterConfig)) { |
|
| 130 | + if (is_callable($filterConfig)) { |
|
| 131 | 131 | return array_values(array_filter($source, $filterConfig)); |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | $result = []; |
| 135 | 135 | |
| 136 | - foreach($source as $item) { |
|
| 137 | - foreach($filterConfig as $args) { |
|
| 136 | + foreach ($source as $item) { |
|
| 137 | + foreach ($filterConfig as $args) { |
|
| 138 | 138 | $rule = array_shift($args); |
| 139 | 139 | |
| 140 | - if(RuleHelper::check($item, $rule, $args)) { |
|
| 140 | + if (RuleHelper::check($item, $rule, $args)) { |
|
| 141 | 141 | $result[] = $item; |
| 142 | 142 | break; |
| 143 | 143 | } |
@@ -151,10 +151,10 @@ discard block |
||
| 151 | 151 | $schemator->addFilter( |
| 152 | 152 | 'sort', |
| 153 | 153 | function(Schemator $schemator, ?array $source, array $rootSource, ?callable $sortCallback = null) { |
| 154 | - if($source === null) { |
|
| 154 | + if ($source === null) { |
|
| 155 | 155 | return null; |
| 156 | 156 | } |
| 157 | - if($sortCallback !== null) { |
|
| 157 | + if ($sortCallback !== null) { |
|
| 158 | 158 | usort($source, $sortCallback); |
| 159 | 159 | } else { |
| 160 | 160 | sort($source); |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | $schemator->addFilter( |
| 167 | 167 | 'rsort', |
| 168 | 168 | function(Schemator $schemator, ?array $source, array $rootSource) { |
| 169 | - if($source === null) { |
|
| 169 | + if ($source === null) { |
|
| 170 | 170 | return null; |
| 171 | 171 | } |
| 172 | 172 | |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | $schemator->addFilter( |
| 179 | 179 | 'path', |
| 180 | 180 | function(Schemator $schemator, ?string $source, array $rootSource) { |
| 181 | - if($source === null) { |
|
| 181 | + if ($source === null) { |
|
| 182 | 182 | return null; |
| 183 | 183 | } |
| 184 | 184 | return $schemator->getValue($rootSource, $source); |
@@ -188,7 +188,7 @@ discard block |
||
| 188 | 188 | $schemator->addFilter( |
| 189 | 189 | 'flatten', |
| 190 | 190 | function(Schemator $schemator, ?array $source) { |
| 191 | - if($source === null) { |
|
| 191 | + if ($source === null) { |
|
| 192 | 192 | return null; |
| 193 | 193 | } |
| 194 | 194 | return ArrHelper::flatten($source); |
@@ -198,34 +198,34 @@ discard block |
||
| 198 | 198 | $schemator->addFilter( |
| 199 | 199 | 'replace', |
| 200 | 200 | function(Schemator $schemator, $source, array $rootSource, array $rules) { |
| 201 | - if($source === null) { |
|
| 201 | + if ($source === null) { |
|
| 202 | 202 | return null; |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | $isArray = is_array($source); |
| 206 | 206 | |
| 207 | - if(!$isArray) { |
|
| 207 | + if (!$isArray) { |
|
| 208 | 208 | $source = [$source]; |
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | $result = []; |
| 212 | 212 | $elseValue = null; |
| 213 | 213 | |
| 214 | - foreach($source as $item) { |
|
| 214 | + foreach ($source as $item) { |
|
| 215 | 215 | $isReplaced = false; |
| 216 | 216 | $elseValue = $item; |
| 217 | 217 | |
| 218 | - foreach($rules as $args) { |
|
| 218 | + foreach ($rules as $args) { |
|
| 219 | 219 | $value = array_shift($args); |
| 220 | 220 | $rule = array_shift($args); |
| 221 | 221 | |
| 222 | - if($rule === 'else') { |
|
| 222 | + if ($rule === 'else') { |
|
| 223 | 223 | $elseValue = $value; |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | $replace = null; |
| 227 | 227 | |
| 228 | - if(RuleHelper::check($item, $rule, $args)) { |
|
| 228 | + if (RuleHelper::check($item, $rule, $args)) { |
|
| 229 | 229 | $replace = $value; |
| 230 | 230 | $isReplaced = true; |
| 231 | 231 | |
@@ -234,12 +234,12 @@ discard block |
||
| 234 | 234 | } |
| 235 | 235 | } |
| 236 | 236 | |
| 237 | - if(!$isReplaced) { |
|
| 237 | + if (!$isReplaced) { |
|
| 238 | 238 | $result[] = $elseValue; |
| 239 | 239 | } |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | - if(!$isArray) { |
|
| 242 | + if (!$isArray) { |
|
| 243 | 243 | $result = $result[0]; |
| 244 | 244 | } |
| 245 | 245 | |
@@ -26,7 +26,7 @@ |
||
| 26 | 26 | */ |
| 27 | 27 | public static function ensureFilterExists(array $filterMap, string $filterName): void |
| 28 | 28 | { |
| 29 | - if(!isset($filterMap[$filterName])) { |
|
| 29 | + if (!isset($filterMap[$filterName])) { |
|
| 30 | 30 | throw new SchematorException( |
| 31 | 31 | "filter '{$filterName}' not found", |
| 32 | 32 | SchematorException::FILTER_NOT_FOUND, |
@@ -52,9 +52,9 @@ discard block |
||
| 52 | 52 | { |
| 53 | 53 | $toAccessor = $this->nestedAccessorFactory->create($result, $this->pathDelimiter); |
| 54 | 54 | |
| 55 | - foreach($schema as $keyTo => $keyFrom) { |
|
| 55 | + foreach ($schema as $keyTo => $keyFrom) { |
|
| 56 | 56 | $value = $this->getValue($source, $keyFrom, $strict); |
| 57 | - if($keyTo === '') { |
|
| 57 | + if ($keyTo === '') { |
|
| 58 | 58 | return $value; |
| 59 | 59 | } |
| 60 | 60 | $toAccessor->set($keyTo, $value, $strict); |
@@ -73,19 +73,19 @@ discard block |
||
| 73 | 73 | */ |
| 74 | 74 | public function getValue($source, $key, bool $strict = false) |
| 75 | 75 | { |
| 76 | - if($key === '' || $key === null) { |
|
| 76 | + if ($key === '' || $key === null) { |
|
| 77 | 77 | return $source; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - if($source === null || (!is_array($source) && !is_object($source))) { |
|
| 80 | + if ($source === null || (!is_array($source) && !is_object($source))) { |
|
| 81 | 81 | return $this->getValueFromUnsupportedSource($source, $key, $strict); |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - if(is_string($key)) { |
|
| 84 | + if (is_string($key)) { |
|
| 85 | 85 | return $this->getValueByKey($source, $key, $strict); |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | - if(is_array($key)) { |
|
| 88 | + if (is_array($key)) { |
|
| 89 | 89 | return $this->getValueByFilters($source, $key, $strict); |
| 90 | 90 | } |
| 91 | 91 | |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | try { |
| 118 | 118 | $fromAccessor = $this->nestedAccessorFactory->create($source, $this->pathDelimiter); |
| 119 | 119 | return $fromAccessor->get($key, $strict); |
| 120 | - } catch(NestedAccessorException $e) { |
|
| 120 | + } catch (NestedAccessorException $e) { |
|
| 121 | 121 | throw SchematorException::createAsCannotGetValue($source, $key, $e); |
| 122 | 122 | } |
| 123 | 123 | } |
@@ -133,13 +133,13 @@ discard block |
||
| 133 | 133 | protected function getValueByFilters(array $source, array $filters, bool $strict) |
| 134 | 134 | { |
| 135 | 135 | $result = $source; |
| 136 | - foreach($filters as $filterConfig) { |
|
| 137 | - if(is_string($filterConfig)) { |
|
| 136 | + foreach ($filters as $filterConfig) { |
|
| 137 | + if (is_string($filterConfig)) { |
|
| 138 | 138 | $result = $this->getValue($result, $filterConfig, $strict); |
| 139 | - } elseif(is_array($filterConfig)) { |
|
| 139 | + } elseif (is_array($filterConfig)) { |
|
| 140 | 140 | $result = $this->runFilter($filterConfig, $result, $source, $strict); |
| 141 | 141 | } else { |
| 142 | - if($strict) { |
|
| 142 | + if ($strict) { |
|
| 143 | 143 | throw SchematorException::createAsUnsupportedFilterConfigType($filterConfig); |
| 144 | 144 | } |
| 145 | 145 | $result = null; |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | */ |
| 160 | 160 | protected function getValueFromUnsupportedSource($source, $key, bool $strict) |
| 161 | 161 | { |
| 162 | - if(!$strict) { |
|
| 162 | + if (!$strict) { |
|
| 163 | 163 | return null; |
| 164 | 164 | } |
| 165 | 165 | throw SchematorException::createAsUnsupportedSourceType($source, $key); |
@@ -175,7 +175,7 @@ discard block |
||
| 175 | 175 | */ |
| 176 | 176 | protected function getValueByUnsupportedKey($source, $key, bool $strict) |
| 177 | 177 | { |
| 178 | - if(!$strict) { |
|
| 178 | + if (!$strict) { |
|
| 179 | 179 | return null; |
| 180 | 180 | } |
| 181 | 181 | throw SchematorException::createAsUnsupportedKeyType($source, $key); |
@@ -198,8 +198,8 @@ discard block |
||
| 198 | 198 | |
| 199 | 199 | try { |
| 200 | 200 | return $this->filters[$filterName]($this, $source, $rootSource, ...$filterConfig); |
| 201 | - } catch(Throwable $e) { |
|
| 202 | - if($strict) { |
|
| 201 | + } catch (Throwable $e) { |
|
| 202 | + if ($strict) { |
|
| 203 | 203 | throw SchematorException::createAsFilterError($filterName, $filterConfig, $source, $e); |
| 204 | 204 | } |
| 205 | 205 | return null; |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | */ |
| 35 | 35 | public function generate(iterable $source, array $schema): Generator |
| 36 | 36 | { |
| 37 | - foreach($source as $item) { |
|
| 37 | + foreach ($source as $item) { |
|
| 38 | 38 | yield $this->schemator->exec($item, $schema); |
| 39 | 39 | } |
| 40 | 40 | } |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | $gen = $this->generate($source, $schema); |
| 52 | 52 | $result = []; |
| 53 | 53 | |
| 54 | - foreach($gen as $item) { |
|
| 54 | + foreach ($gen as $item) { |
|
| 55 | 55 | $result[] = $item; |
| 56 | 56 | } |
| 57 | 57 | |
@@ -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 | |
@@ -100,9 +100,9 @@ discard block |
||
| 100 | 100 | protected function _get($source, array $path, &$result, int &$errorsCount) |
| 101 | 101 | { |
| 102 | 102 | // if path stack is empty — we reached target value of given path in source argument |
| 103 | - if(!count($path)) { |
|
| 103 | + if (!count($path)) { |
|
| 104 | 104 | // so if result is multiple |
| 105 | - if(is_array($result)) { |
|
| 105 | + if (is_array($result)) { |
|
| 106 | 106 | // we append source to result |
| 107 | 107 | $result[] = $source; |
| 108 | 108 | } else { |
@@ -114,11 +114,11 @@ discard block |
||
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | // let's iterate every path part from stack |
| 117 | - while(count($path)) { |
|
| 117 | + while (count($path)) { |
|
| 118 | 118 | $key = array_pop($path); |
| 119 | 119 | |
| 120 | - if(is_array($source)) { |
|
| 121 | - if(!array_key_exists($key, $source)) { |
|
| 120 | + if (is_array($source)) { |
|
| 121 | + if (!array_key_exists($key, $source)) { |
|
| 122 | 122 | // path part key is missing in source array |
| 123 | 123 | $errorsCount++; |
| 124 | 124 | // we cannot go deeper |
@@ -126,8 +126,8 @@ discard block |
||
| 126 | 126 | } |
| 127 | 127 | // go to the next nested level |
| 128 | 128 | $source = $source[$key]; |
| 129 | - } elseif(is_object($source)) { |
|
| 130 | - if(!property_exists($source, $key)) { |
|
| 129 | + } elseif (is_object($source)) { |
|
| 130 | + if (!property_exists($source, $key)) { |
|
| 131 | 131 | // path part key is missing in source object |
| 132 | 132 | $errorsCount++; |
| 133 | 133 | // we cannot go deeper |
@@ -144,13 +144,13 @@ discard block |
||
| 144 | 144 | |
| 145 | 145 | // when it's not the last iteration of the stack |
| 146 | 146 | // and the source is non-associative array (list) |
| 147 | - if(count($path) && is_array($source) && !ArrHelper::isAssoc($source)) { |
|
| 147 | + if (count($path) && is_array($source) && !ArrHelper::isAssoc($source)) { |
|
| 148 | 148 | // the result will be multiple |
| 149 | - if(!is_array($result)) { |
|
| 149 | + if (!is_array($result)) { |
|
| 150 | 150 | $result = []; |
| 151 | 151 | } |
| 152 | 152 | // and we need to use recursive call for each item of this array |
| 153 | - foreach($source as $item) { |
|
| 153 | + foreach ($source as $item) { |
|
| 154 | 154 | $this->_get($item, $path, $result, $errorsCount); |
| 155 | 155 | } |
| 156 | 156 | // we don't need to do something in this recursive branch |
@@ -176,15 +176,15 @@ discard block |
||
| 176 | 176 | { |
| 177 | 177 | $temp = &$source; |
| 178 | 178 | // let's iterate every path part to go deeper into nesting |
| 179 | - foreach($path as $key) { |
|
| 180 | - if(isset($temp) && is_scalar($temp)) { |
|
| 179 | + foreach ($path as $key) { |
|
| 180 | + if (isset($temp) && is_scalar($temp)) { |
|
| 181 | 181 | // value in the middle of the path must me an array |
| 182 | 182 | $temp = []; |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | // go to the next nested level |
| 186 | - if(is_object($temp)) { |
|
| 187 | - if($strict && !property_exists($temp, $key)) { |
|
| 186 | + if (is_object($temp)) { |
|
| 187 | + if ($strict && !property_exists($temp, $key)) { |
|
| 188 | 188 | throw NestedAccessorException::createAsCannotSetValue($key); |
| 189 | 189 | } |
| 190 | 190 | $temp = &$temp->{$key}; |