@@ -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, |
@@ -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}; |
@@ -53,9 +53,9 @@ discard block |
||
53 | 53 | { |
54 | 54 | $toAccessor = $this->nestedAccessorFactory->create($result, $this->pathDelimiter); |
55 | 55 | |
56 | - foreach($schema as $keyTo => $keyFrom) { |
|
56 | + foreach ($schema as $keyTo => $keyFrom) { |
|
57 | 57 | $value = $this->getValue($source, $keyFrom, $strict); |
58 | - if($keyTo === '') { |
|
58 | + if ($keyTo === '') { |
|
59 | 59 | return $value; |
60 | 60 | } |
61 | 61 | $toAccessor->set($keyTo, $value, $strict); |
@@ -74,19 +74,19 @@ discard block |
||
74 | 74 | */ |
75 | 75 | public function getValue($source, $key, bool $strict = false) |
76 | 76 | { |
77 | - if($key === '' || $key === null) { |
|
77 | + if ($key === '' || $key === null) { |
|
78 | 78 | return $source; |
79 | 79 | } |
80 | 80 | |
81 | - if($source === null || (!is_array($source) && !is_object($source))) { |
|
81 | + if ($source === null || (!is_array($source) && !is_object($source))) { |
|
82 | 82 | return $this->getValueFromUnsupportedSource($source, $key, $strict); |
83 | 83 | } |
84 | 84 | |
85 | - if(is_string($key)) { |
|
85 | + if (is_string($key)) { |
|
86 | 86 | return $this->getValueByKey($source, $key, $strict); |
87 | 87 | } |
88 | 88 | |
89 | - if(is_array($key)) { |
|
89 | + if (is_array($key)) { |
|
90 | 90 | return $this->getValueByFilters($source, $key, $strict); |
91 | 91 | } |
92 | 92 | |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | try { |
119 | 119 | $fromAccessor = $this->nestedAccessorFactory->create($source, $this->pathDelimiter); |
120 | 120 | return $fromAccessor->get($key, $strict); |
121 | - } catch(NestedAccessorException $e) { |
|
121 | + } catch (NestedAccessorException $e) { |
|
122 | 122 | throw SchematorException::createAsCannotGetValue($source, $key, $e); |
123 | 123 | } |
124 | 124 | } |
@@ -134,13 +134,13 @@ discard block |
||
134 | 134 | protected function getValueByFilters(array $source, array $filters, bool $strict) |
135 | 135 | { |
136 | 136 | $result = $source; |
137 | - foreach($filters as $filterConfig) { |
|
138 | - if(is_string($filterConfig)) { |
|
137 | + foreach ($filters as $filterConfig) { |
|
138 | + if (is_string($filterConfig)) { |
|
139 | 139 | $result = $this->getValue($result, $filterConfig, $strict); |
140 | - } elseif(is_array($filterConfig)) { |
|
140 | + } elseif (is_array($filterConfig)) { |
|
141 | 141 | $result = $this->runFilter($filterConfig, $result, $source, $strict); |
142 | 142 | } else { |
143 | - if($strict) { |
|
143 | + if ($strict) { |
|
144 | 144 | throw SchematorException::createAsUnsupportedFilterConfigType($filterConfig); |
145 | 145 | } |
146 | 146 | $result = null; |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | */ |
161 | 161 | protected function getValueFromUnsupportedSource($source, $key, bool $strict) |
162 | 162 | { |
163 | - if(!$strict) { |
|
163 | + if (!$strict) { |
|
164 | 164 | return null; |
165 | 165 | } |
166 | 166 | throw SchematorException::createAsUnsupportedSourceType($source, $key); |
@@ -176,7 +176,7 @@ discard block |
||
176 | 176 | */ |
177 | 177 | protected function getValueByUnsupportedKey($source, $key, bool $strict) |
178 | 178 | { |
179 | - if(!$strict) { |
|
179 | + if (!$strict) { |
|
180 | 180 | return null; |
181 | 181 | } |
182 | 182 | throw SchematorException::createAsUnsupportedKeyType($source, $key); |
@@ -202,8 +202,8 @@ discard block |
||
202 | 202 | new FilterContext($this, $source, $rootSource), |
203 | 203 | ...$filterConfig |
204 | 204 | ); |
205 | - } catch(Throwable $e) { |
|
206 | - if($strict) { |
|
205 | + } catch (Throwable $e) { |
|
206 | + if ($strict) { |
|
207 | 207 | throw SchematorException::createAsFilterError($filterName, $filterConfig, $source, $e); |
208 | 208 | } |
209 | 209 | return null; |
@@ -23,10 +23,10 @@ discard block |
||
23 | 23 | public static function date(FilterContextInterface $context, string $format, ?int $timezone = null) |
24 | 24 | { |
25 | 25 | $source = $context->getSource(); |
26 | - if($source === null) { |
|
26 | + if ($source === null) { |
|
27 | 27 | return null; |
28 | 28 | } |
29 | - if($timezone === null) { |
|
29 | + if ($timezone === null) { |
|
30 | 30 | return date($format, $source); |
31 | 31 | } |
32 | 32 | return gmdate($format, $source+3600*$timezone); |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | public static function implode(FilterContextInterface $context, string $delimiter = ', '): ?string |
36 | 36 | { |
37 | 37 | $source = $context->getSource(); |
38 | - if($source === null) { |
|
38 | + if ($source === null) { |
|
39 | 39 | return null; |
40 | 40 | } |
41 | 41 | return implode($delimiter, $source); |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | public static function explode(FilterContextInterface $context, string $delimiter = ', ') |
45 | 45 | { |
46 | 46 | $source = $context->getSource(); |
47 | - if($source === null) { |
|
47 | + if ($source === null) { |
|
48 | 48 | return null; |
49 | 49 | } |
50 | 50 | return explode($delimiter, $source); |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | public static function sum(FilterContextInterface $context) |
54 | 54 | { |
55 | 55 | $source = $context->getSource(); |
56 | - if($source === null) { |
|
56 | + if ($source === null) { |
|
57 | 57 | return null; |
58 | 58 | } |
59 | 59 | return array_sum($source); |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | public static function average(FilterContextInterface $context) |
63 | 63 | { |
64 | 64 | $source = $context->getSource(); |
65 | - if($source === null) { |
|
65 | + if ($source === null) { |
|
66 | 66 | return null; |
67 | 67 | } |
68 | 68 | return array_sum($source)/count($source); |
@@ -71,21 +71,21 @@ discard block |
||
71 | 71 | public static function filter(FilterContextInterface $context, $filterConfig): ?array |
72 | 72 | { |
73 | 73 | $source = $context->getSource(); |
74 | - if($source === null) { |
|
74 | + if ($source === null) { |
|
75 | 75 | return null; |
76 | 76 | } |
77 | 77 | |
78 | - if(is_callable($filterConfig)) { |
|
78 | + if (is_callable($filterConfig)) { |
|
79 | 79 | return array_values(array_filter($source, $filterConfig)); |
80 | 80 | } |
81 | 81 | |
82 | 82 | $result = []; |
83 | 83 | |
84 | - foreach($source as $item) { |
|
85 | - foreach($filterConfig as $args) { |
|
84 | + foreach ($source as $item) { |
|
85 | + foreach ($filterConfig as $args) { |
|
86 | 86 | $rule = array_shift($args); |
87 | 87 | |
88 | - if(RuleHelper::check($item, $rule, $args)) { |
|
88 | + if (RuleHelper::check($item, $rule, $args)) { |
|
89 | 89 | $result[] = $item; |
90 | 90 | break; |
91 | 91 | } |
@@ -98,10 +98,10 @@ discard block |
||
98 | 98 | public static function sort(FilterContextInterface $context, ?callable $sortCallback = null): ?array |
99 | 99 | { |
100 | 100 | $source = $context->getSource(); |
101 | - if($source === null) { |
|
101 | + if ($source === null) { |
|
102 | 102 | return null; |
103 | 103 | } |
104 | - if($sortCallback !== null) { |
|
104 | + if ($sortCallback !== null) { |
|
105 | 105 | usort($source, $sortCallback); |
106 | 106 | } else { |
107 | 107 | sort($source); |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | public static function rsort(FilterContextInterface $context): ?array |
113 | 113 | { |
114 | 114 | $source = $context->getSource(); |
115 | - if($source === null) { |
|
115 | + if ($source === null) { |
|
116 | 116 | return null; |
117 | 117 | } |
118 | 118 | |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | public static function path(FilterContextInterface $context) |
124 | 124 | { |
125 | 125 | $source = $context->getSource(); |
126 | - if($source === null) { |
|
126 | + if ($source === null) { |
|
127 | 127 | return null; |
128 | 128 | } |
129 | 129 | return $context->getSchemator()->getValue($context->getRootSource(), $source); |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | public static function flatten(FilterContextInterface $context): ?array |
133 | 133 | { |
134 | 134 | $source = $context->getSource(); |
135 | - if($source === null) { |
|
135 | + if ($source === null) { |
|
136 | 136 | return null; |
137 | 137 | } |
138 | 138 | return ArrHelper::flatten($source); |
@@ -141,33 +141,33 @@ discard block |
||
141 | 141 | public static function replace(FilterContextInterface $context, array $rules) |
142 | 142 | { |
143 | 143 | $source = $context->getSource(); |
144 | - if($source === null) { |
|
144 | + if ($source === null) { |
|
145 | 145 | return null; |
146 | 146 | } |
147 | 147 | |
148 | 148 | $isArray = is_array($source); |
149 | 149 | |
150 | - if(!$isArray) { |
|
150 | + if (!$isArray) { |
|
151 | 151 | $source = [$source]; |
152 | 152 | } |
153 | 153 | |
154 | 154 | $result = []; |
155 | 155 | |
156 | - foreach($source as $item) { |
|
156 | + foreach ($source as $item) { |
|
157 | 157 | $isReplaced = false; |
158 | 158 | $elseValue = $item; |
159 | 159 | |
160 | - foreach($rules as $args) { |
|
160 | + foreach ($rules as $args) { |
|
161 | 161 | $value = array_shift($args); |
162 | 162 | $rule = array_shift($args); |
163 | 163 | |
164 | - if($rule === 'else') { |
|
164 | + if ($rule === 'else') { |
|
165 | 165 | $elseValue = $value; |
166 | 166 | } |
167 | 167 | |
168 | 168 | $replace = null; |
169 | 169 | |
170 | - if(RuleHelper::check($item, $rule, $args)) { |
|
170 | + if (RuleHelper::check($item, $rule, $args)) { |
|
171 | 171 | $replace = $value; |
172 | 172 | $isReplaced = true; |
173 | 173 | |
@@ -176,12 +176,12 @@ discard block |
||
176 | 176 | } |
177 | 177 | } |
178 | 178 | |
179 | - if(!$isReplaced) { |
|
179 | + if (!$isReplaced) { |
|
180 | 180 | $result[] = $elseValue; |
181 | 181 | } |
182 | 182 | } |
183 | 183 | |
184 | - if(!$isArray) { |
|
184 | + if (!$isArray) { |
|
185 | 185 | $result = $result[0]; |
186 | 186 | } |
187 | 187 |
@@ -20,11 +20,11 @@ |
||
20 | 20 | { |
21 | 21 | $builder = static::createBuilder(); |
22 | 22 | |
23 | - if($withBaseFilters) { |
|
23 | + if ($withBaseFilters) { |
|
24 | 24 | $builder->withFilters(new BaseFiltersStorage()); |
25 | 25 | } |
26 | 26 | |
27 | - if(count($extraFilters)) { |
|
27 | + if (count($extraFilters)) { |
|
28 | 28 | $builder->withFilters($extraFilters); |
29 | 29 | } |
30 | 30 |
@@ -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; |