@@ -16,54 +16,54 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | public static function evaluate($value, string $rule, array $args): bool |
| 18 | 18 | { |
| 19 | - switch($rule) { |
|
| 19 | + switch ($rule) { |
|
| 20 | 20 | case '=': |
| 21 | - if((string)$value === (string)$args[0]) { |
|
| 21 | + if ((string) $value === (string) $args[0]) { |
|
| 22 | 22 | return true; |
| 23 | 23 | } |
| 24 | 24 | break; |
| 25 | 25 | case '!=': |
| 26 | - if((string)$value !== (string)$args[0]) { |
|
| 26 | + if ((string) $value !== (string) $args[0]) { |
|
| 27 | 27 | return true; |
| 28 | 28 | } |
| 29 | 29 | break; |
| 30 | 30 | case '>': |
| 31 | - if($value > $args[0]) { |
|
| 31 | + if ($value > $args[0]) { |
|
| 32 | 32 | return true; |
| 33 | 33 | } |
| 34 | 34 | break; |
| 35 | 35 | case '>=': |
| 36 | - if($value >= $args[0]) { |
|
| 36 | + if ($value >= $args[0]) { |
|
| 37 | 37 | return true; |
| 38 | 38 | } |
| 39 | 39 | break; |
| 40 | 40 | case '<': |
| 41 | - if($value < $args[0]) { |
|
| 41 | + if ($value < $args[0]) { |
|
| 42 | 42 | return true; |
| 43 | 43 | } |
| 44 | 44 | break; |
| 45 | 45 | case '<=': |
| 46 | - if($value <= $args[0]) { |
|
| 46 | + if ($value <= $args[0]) { |
|
| 47 | 47 | return true; |
| 48 | 48 | } |
| 49 | 49 | break; |
| 50 | 50 | case 'between': |
| 51 | - if($value >= $args[0] && $value <= $args[1]) { |
|
| 51 | + if ($value >= $args[0] && $value <= $args[1]) { |
|
| 52 | 52 | return true; |
| 53 | 53 | } |
| 54 | 54 | break; |
| 55 | 55 | case 'in': |
| 56 | - if(in_array($value, $args[0])) { |
|
| 56 | + if (in_array($value, $args[0])) { |
|
| 57 | 57 | return true; |
| 58 | 58 | } |
| 59 | 59 | break; |
| 60 | 60 | case 'not in': |
| 61 | - if(!in_array($value, $args[0])) { |
|
| 61 | + if (!in_array($value, $args[0])) { |
|
| 62 | 62 | return true; |
| 63 | 63 | } |
| 64 | 64 | break; |
| 65 | 65 | case 'between strict': |
| 66 | - if($value > $args[0] && $value < $args[1]) { |
|
| 66 | + if ($value > $args[0] && $value < $args[1]) { |
|
| 67 | 67 | return true; |
| 68 | 68 | } |
| 69 | 69 | break; |
@@ -15,8 +15,8 @@ |
||
| 15 | 15 | public static function flatten(array $arr): array |
| 16 | 16 | { |
| 17 | 17 | $tmp = []; |
| 18 | - foreach($arr as $val) { |
|
| 19 | - if(is_array($val)) { |
|
| 18 | + foreach ($arr as $val) { |
|
| 19 | + if (is_array($val)) { |
|
| 20 | 20 | $tmp = array_merge($tmp, static::flatten($val)); |
| 21 | 21 | } else { |
| 22 | 22 | $tmp[] = $val; |
@@ -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::evaluate($item, $rule, $args)) { |
|
| 155 | + if (RuleHelper::evaluate($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 ArrayHelper::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::evaluate($item, $rule, $args)) { |
|
| 273 | + if (RuleHelper::evaluate($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 | |