|
| 1 | +<?php |
| 2 | +namespace JsonLogic; |
| 3 | + |
| 4 | +use JsonLogic\Operators\CallableOperator; |
| 5 | +use JsonLogic\Rule; |
| 6 | +use JsonLogic\Support\Logical; |
| 7 | + |
| 8 | +use function array_keys; |
| 9 | +use function array_merge; |
| 10 | +use function array_unique; |
| 11 | +use function count; |
| 12 | +use function is_array; |
| 13 | +use function is_numeric; |
| 14 | +use function is_string; |
| 15 | + |
| 16 | +/** |
| 17 | + * For fully fallback to official JsonLogic lib |
| 18 | + */ |
| 19 | +trait Fallbacks |
| 20 | +{ |
| 21 | + /** @deprecated 1.0.0 */ |
| 22 | + public static function get_operator($logic) |
| 23 | + { |
| 24 | + return array_keys($logic)[0]; |
| 25 | + } |
| 26 | + |
| 27 | + /** @deprecated 1.0.0 */ |
| 28 | + public static function get_values($logic, $fix_unary = true) |
| 29 | + { |
| 30 | + $op = static::get_operator($logic); |
| 31 | + $values = $logic[$op]; |
| 32 | + |
| 33 | + if ($fix_unary && (!is_array($values) or static::is_logic($values))) { |
| 34 | + $values = [ $values ]; |
| 35 | + } |
| 36 | + return $values; |
| 37 | + } |
| 38 | + |
| 39 | + /** @deprecated 1.0.0 */ |
| 40 | + public static function is_logic($array): bool |
| 41 | + { |
| 42 | + return Rule::isValid($array); |
| 43 | + } |
| 44 | + |
| 45 | + /** @deprecated 1.0.0 */ |
| 46 | + public static function truthy($logic): bool |
| 47 | + { |
| 48 | + return Logical::truthy($logic); |
| 49 | + } |
| 50 | + |
| 51 | + /** @deprecated 1.0.0 */ |
| 52 | + public static function uses_data($logic): array |
| 53 | + { |
| 54 | + if (is_object($logic)) { |
| 55 | + $logic = (array) $logic; |
| 56 | + } |
| 57 | + |
| 58 | + $collection = []; |
| 59 | + |
| 60 | + if (self::is_logic($logic)) { |
| 61 | + $op = array_keys($logic)[0]; |
| 62 | + $values = (array) $logic[$op]; |
| 63 | + |
| 64 | + if ($op === 'var') { |
| 65 | + $collection[] = $values[0]; |
| 66 | + } else { |
| 67 | + foreach ($values as $value) { |
| 68 | + $collection = array_merge($collection, self::uses_data($value)); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return array_unique($collection); |
| 74 | + } |
| 75 | + |
| 76 | + /** @deprecated 1.0.0 */ |
| 77 | + public static function rule_like($rule, $pattern): bool |
| 78 | + { |
| 79 | + if (is_string($pattern) and $pattern[0] === '{') { |
| 80 | + $pattern = json_decode($pattern, true); |
| 81 | + } |
| 82 | + |
| 83 | + switch ($pattern) { |
| 84 | + case $rule: |
| 85 | + case '@': |
| 86 | + return true; |
| 87 | + case 'number': |
| 88 | + return is_numeric($rule); |
| 89 | + case 'string': |
| 90 | + return is_string($rule); |
| 91 | + case 'array': |
| 92 | + return is_array($rule) && !static::is_logic($rule); |
| 93 | + default: |
| 94 | + if (static::is_logic($pattern) && static::is_logic($rule)) { |
| 95 | + $patternOp = static::get_operator($pattern); |
| 96 | + if ($patternOp === '@' || $patternOp === static::get_operator($rule)) { |
| 97 | + return static::rule_like( |
| 98 | + static::get_values($rule, false), |
| 99 | + static::get_values($pattern, false) |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | + return is_array($pattern) |
| 104 | + && is_array($rule) |
| 105 | + && count($pattern) === count($rule) |
| 106 | + && !in_array(false, array_map('static::rule_like', $pattern, $rule)); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** @deprecated 1.0.0 */ |
| 111 | + public static function add_operation(string $name, callable $callable): void |
| 112 | + { |
| 113 | + JsonLogic::operators()->register( |
| 114 | + $name, |
| 115 | + new CallableOperator($callable) |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
0 commit comments