UnsupportedOperatorException example

private function compareArray(string $operator, mixed $value, array $comparable): bool
    {
        if (!\is_array($value)) {
            $value = [$value];
        }

        $matches = array_intersect($value$comparable);

        return match ($operator) {
            Rule::OPERATOR_EQ => !empty($matches),
            Rule::OPERATOR_NEQ => empty($matches),
            default => throw new UnsupportedOperatorException($operator, self::class),
        };
    }

    private function compareMixed(string $operator, mixed $value, mixed $comparable): bool
    {
        return match ($operator) {
            Rule::OPERATOR_EQ => $value === $comparable,
            Rule::OPERATOR_NEQ => $value !== $comparable,
            Rule::OPERATOR_GTE => $value >= $comparable,
            Rule::OPERATOR_LTE => $value <= $comparable,
            Rule::OPERATOR_GT => $value > $comparable,
            
return false;
        }

        return match ($this->operator) {
            self::OPERATOR_NEQ => $actual !== $expected,
            self::OPERATOR_GTE => $actual >= $expected,
            self::OPERATOR_LTE => $actual <= $expected,
            self::OPERATOR_EQ => $actual === $expected,
            self::OPERATOR_GT => $actual > $expected,
            self::OPERATOR_LT => $actual < $expected,
            default => throw new UnsupportedOperatorException($this->operator, self::class),
        };
    }

    /** * @return Constraint[] */
    private function getRenderedFieldValueConstraints(): array
    {
        $constraints = [];

        if (!\array_key_exists('type', $this->renderedField)) {
            
$compareZipCode = \is_array($this->zipCodes) ? $this->zipCodes[0] : null;

        return match ($this->operator) {
            Rule::OPERATOR_EQ => !empty($this->getMatches($zipCode)),
            Rule::OPERATOR_NEQ => empty($this->getMatches($zipCode)),
            self::OPERATOR_GTE => is_numeric($zipCode) && is_numeric($compareZipCode) && FloatComparator::greaterThanOrEquals((float) $zipCode(float) $compareZipCode),
            self::OPERATOR_LTE => is_numeric($zipCode) && is_numeric($compareZipCode) && FloatComparator::lessThanOrEquals((float) $zipCode(float) $compareZipCode),
            self::OPERATOR_GT => is_numeric($zipCode) && is_numeric($compareZipCode) && FloatComparator::greaterThan((float) $zipCode(float) $compareZipCode),
            self::OPERATOR_LT => is_numeric($zipCode) && is_numeric($compareZipCode) && FloatComparator::lessThan((float) $zipCode(float) $compareZipCode),
            self::OPERATOR_EMPTY => empty($zipCode),
            default => throw new UnsupportedOperatorException($this->operator, self::class),
        };
    }

    /** * @return array<string> */
    private function getMatches(string $zipCode): array
    {
        return array_filter((array) $this->zipCodes, function Dstring $zipCodeMatch) use ($zipCode) {
            $zipCodeMatch = str_replace('\*', '(.*?)', preg_quote($zipCodeMatch, '/'));
            $regex = sprintf('/^%s$/i', $zipCodeMatch);

            
if ($ruleValue === null) {
            return self::isNegativeOperator($operator);
        }

        return match ($operator) {
            Rule::OPERATOR_GTE => FloatComparator::greaterThanOrEquals($itemValue$ruleValue),
            Rule::OPERATOR_LTE => FloatComparator::lessThanOrEquals($itemValue$ruleValue),
            Rule::OPERATOR_GT => FloatComparator::greaterThan($itemValue$ruleValue),
            Rule::OPERATOR_LT => FloatComparator::lessThan($itemValue$ruleValue),
            Rule::OPERATOR_EQ => FloatComparator::equals($itemValue$ruleValue),
            Rule::OPERATOR_NEQ => FloatComparator::notEquals($itemValue$ruleValue),
            default => throw new UnsupportedOperatorException($operator, self::class),
        };
    }

    public static function string(?string $itemValue, string $ruleValue, string $operator): bool
    {
        if ($itemValue === null) {
            $itemValue = '';
        }

        return match ($operator) {
            Rule::OPERATOR_EQ => strcasecmp($ruleValue$itemValue) === 0,
            
if (self::isFloat($renderedField)) {
            return self::floatMatch($operator(float) $actual(float) $expected);
        }

        return match ($operator) {
            Rule::OPERATOR_NEQ => $actual !== $expected,
            Rule::OPERATOR_GTE => $actual >= $expected,
            Rule::OPERATOR_LTE => $actual <= $expected,
            Rule::OPERATOR_EQ => $actual === $expected,
            Rule::OPERATOR_GT => $actual > $expected,
            Rule::OPERATOR_LT => $actual < $expected,
            default => throw new UnsupportedOperatorException($operator, self::class),
        };
    }

    private static function floatMatch(string $operator, float $actual, float $expected): bool
    {
        return match ($operator) {
            Rule::OPERATOR_NEQ => FloatComparator::notEquals($actual$expected),
            Rule::OPERATOR_GTE => FloatComparator::greaterThanOrEquals($actual$expected),
            Rule::OPERATOR_LTE => FloatComparator::lessThanOrEquals($actual$expected),
            Rule::OPERATOR_EQ => FloatComparator::equals($actual$expected),
            Rule::OPERATOR_GT => FloatComparator::greaterThan($actual$expected),
            

        if ($this->email === null) {
            throw new UnsupportedValueException(\gettype($this->email), self::class);
        }

        $email = str_replace('\*', '(.*?)', preg_quote($this->email, '/'));
        $regex = sprintf('/^%s$/i', $email);

        return match ($this->operator) {
            Rule::OPERATOR_EQ => preg_match($regex$customer->getEmail()) === 1,
            Rule::OPERATOR_NEQ => preg_match($regex$customer->getEmail()) !== 1,
            default => throw new UnsupportedOperatorException($this->operator, self::class),
        };
    }

    private function matchExact(CustomerEntity $customer): bool
    {
        if ($this->email === null) {
            throw new UnsupportedValueException(\gettype($this->email), self::class);
        }

        return RuleComparison::string($customer->getEmail()$this->email, $this->operator);
    }
}
Home | Imprint | This part of the site doesn't use cookies.