Lines 100.00% 24 / 24
Methods 100.00% 1 / 1
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 compare 100.00% 24 / 24 100.00% 1 / 1 30
10enum ComparisonOperator
11{
12    case Equals;
13    case LooseEquals;
14    case NotEquals;
15    case StrictNotEquals;
16    case GreaterThan;
17    case GreaterThanOrEqual;
18    case LessThan;
19    case LessThanOrEqual;
20    case In;
21    case NotIn;
22    case Between;
23    case NotBetween;
24    case StartsWith;
25    case EndsWith;
26    case Contains;
27
28    /**
29     * Compare an actual column value against `$value` using this operator.
30     *
31     * `In` and `NotIn` require `$value` to be an array.
32     * `Between` and `NotBetween` require `$value` to be a two-element
33     * `[min, max]` array.
34     * The string operators require `$value` to be a string.
35     */
36    public function compare(mixed $actual, mixed $value): bool
37    {
38        return match ($this) {
39            ComparisonOperator::Equals => $actual === $value,
40            ComparisonOperator::LooseEquals => $actual == $value,
41            ComparisonOperator::NotEquals => $actual != $value,
42            ComparisonOperator::StrictNotEquals => $actual !== $value,
43            ComparisonOperator::GreaterThan => $actual > $value,
44            ComparisonOperator::GreaterThanOrEqual => $actual >= $value,
45            ComparisonOperator::LessThan => $actual < $value,
46            ComparisonOperator::LessThanOrEqual => $actual <= $value,
47            ComparisonOperator::In => is_array($value) && in_array($actual, $value, true),
48            ComparisonOperator::NotIn => is_array($value) && !in_array($actual, $value, true),
49            ComparisonOperator::Between => is_array($value)
50                && count($value) === 2
51                && $actual >= $value[0]
52                && $actual <= $value[1],
53            ComparisonOperator::NotBetween => is_array($value)
54                && count($value) === 2
55                && ($actual < $value[0] || $actual > $value[1]),
56            ComparisonOperator::StartsWith => is_string($actual) && is_string($value)
57                && str_starts_with($actual, $value),
58            ComparisonOperator::EndsWith => is_string($actual) && is_string($value)
59                && str_ends_with($actual, $value),
60            ComparisonOperator::Contains => is_string($actual) && is_string($value)
61                && str_contains($actual, $value),
62        };
63    }
64}