1 | <?php |
||
28 | abstract class Comparison implements Filter, Satisfiable |
||
29 | { |
||
30 | protected const EQ = '='; |
||
31 | |||
32 | protected const NEQ = '<>'; |
||
33 | |||
34 | protected const LT = '<'; |
||
35 | |||
36 | protected const LTE = '<='; |
||
37 | |||
38 | protected const GT = '>'; |
||
39 | |||
40 | protected const GTE = '>='; |
||
41 | |||
42 | /** |
||
43 | * @var Operand|string |
||
44 | */ |
||
45 | private $field; |
||
46 | |||
47 | /** |
||
48 | * @var Operand|string |
||
49 | */ |
||
50 | private $value; |
||
51 | |||
52 | /** |
||
53 | * @var string|null |
||
54 | */ |
||
55 | private $context; |
||
56 | |||
57 | /** |
||
58 | * @var string[] |
||
59 | */ |
||
60 | private static $operators = [ |
||
61 | self::EQ, self::NEQ, |
||
62 | self::LT, self::LTE, |
||
63 | self::GT, self::GTE, |
||
64 | ]; |
||
65 | |||
66 | /** |
||
67 | * @var string |
||
68 | */ |
||
69 | private $operator; |
||
70 | |||
71 | /** |
||
72 | * Make sure the $field has a value equals to $value. |
||
73 | * |
||
74 | * @param string $operator |
||
75 | * @param Operand|string $field |
||
76 | * @param Operand|mixed $value |
||
77 | * @param string|null $context |
||
78 | * |
||
79 | * @throws InvalidArgumentException |
||
80 | */ |
||
81 | public function __construct(string $operator, $field, $value, ?string $context = null) |
||
82 | { |
||
83 | if (!in_array($operator, self::$operators, true)) { |
||
84 | throw new InvalidArgumentException(sprintf( |
||
85 | '"%s" is not a valid comparison operator. Valid operators are: "%s"', |
||
86 | $operator, |
||
87 | implode(', ', self::$operators) |
||
88 | )); |
||
89 | } |
||
90 | |||
91 | $this->operator = $operator; |
||
92 | $this->field = $field; |
||
93 | $this->value = $value; |
||
94 | $this->context = $context; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param QueryBuilder $qb |
||
99 | * @param string $context |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | public function getFilter(QueryBuilder $qb, string $context): string |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function filterCollection(iterable $collection, ?string $context = null): iterable |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | public function isSatisfiedBy($candidate, ?string $context = null): bool |
||
146 | |||
147 | /** |
||
148 | * @param string|null $context |
||
149 | * |
||
150 | * @return string|null |
||
151 | */ |
||
152 | private function resolveContext(?string $context): ?string |
||
164 | |||
165 | /** |
||
166 | * @param mixed $field |
||
167 | * @param mixed $value |
||
168 | * |
||
169 | * @return bool |
||
170 | */ |
||
171 | abstract protected function compare($field, $value): bool; |
||
172 | } |
||
173 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.