Skip to content

Beautiful Open-Source Form Builder

33.1 KB
Raw Blame
<?php

namespace App\Service\Forms;

use App\Models\Forms\Form;
use App\Models\Forms\FormSubmission;
use App\Service\Formulas\ComputedVariableEvaluator;
use Illuminate\Support\Facades\DB;

class FormLogicConditionChecker
{
    private ?array $computedValues = null;

    private array $dateFields = [];

    public function __construct(private ?array $conditions, private ?array $formData)
    {
    }

    public static function conditionsMet(?array $conditions, array $formData): bool
    {
        return (new self($conditions, $formData))->conditionsAreMet($conditions, $formData);
    }

    /**
     * Check conditions with computed variable support
     */
    public static function conditionsMetWithForm(?array $conditions, array $formData, ?Form $form): bool
    {
        // Submission input must never override a declared computed variable.
        foreach ($form?->computed_variables ?? [] as $variable) {
            unset($formData[$variable['id']]);
        }
        $checker = new self($conditions, $formData);
        foreach ($form?->properties ?? [] as $property) {
            if (($property['type'] ?? null) === 'date') {
                $checker->dateFields[$property['id']] = $property;
            }
        }

        // If form has computed variables, evaluate them
        if ($form && !empty($form->computed_variables)) {
            $checker->computedValues = ComputedVariableEvaluator::evaluateForSubmission($form, $formData);
        }

        return $checker->conditionsAreMet($conditions, $formData);
    }

    /**
     * Set computed variable values
     */
    public function setComputedValues(array $values): self
    {
        $this->computedValues = $values;
        return $this;
    }

    /**
     * Get value for a field or computed variable
     */
    private function getValue(string $fieldId)
    {
        if ($this->computedValues !== null && array_key_exists($fieldId, $this->computedValues)) {
            return $this->computedValues[$fieldId];
        }

        if (isset($this->formData[$fieldId])) {
            return $this->formData[$fieldId];
        }

        return null;
    }

    /**
     * Resolve mention references in a condition value.
     * Single mention with no surrounding text returns the raw field value (preserving type).
     * Mixed content or multiple mentions resolves to a plain-text string.
     */
    private function resolveConditionValue($value)
    {
        if (!is_string($value) || !str_contains($value, 'mention-field-id')) {
            return $value;
        }

        preg_match_all('/mention-field-id="([^"]+)"/', $value, $matches);
        $mentionCount = count($matches[1] ?? []);

        if ($mentionCount === 1) {
            $withoutSpan = preg_replace('/<span[^>]*mention[^>]*>.*?<\/span>/s', '', $value);

            if (trim($withoutSpan) === '') {
                $fieldId = $matches[1][0];
                $resolvedValue = $this->getValue($fieldId);

                if ($resolvedValue !== null) {
                    return $resolvedValue;
                }

                if (preg_match('/mention-fallback="([^"]*)"/', $value, $fb) && $fb[1] !== '') {
                    return $fb[1];
                }

                return null;
            }
        }

        $data = collect($this->formData)
            ->map(fn ($val, $id) => ['id' => $id, 'value' => $val])
            ->values()
            ->toArray();

        $parser = new \App\Open\MentionParser($value, $data, $this->computedValues ?? []);

        return $parser->parseAsText();
    }

    private function conditionsAreMet(?array $conditions, array $formData): bool
    {
        if (!$conditions) {
            return false;
        }

        // If it's not a group, just a single condition
        if (!isset($conditions['operatorIdentifier'])) {
            $fieldId = $conditions['value']['property_meta']['id'] ?? null;
            $value = $fieldId ? $this->getValue($fieldId) : null;

            $condition = $conditions['value'];
            if (isset($condition['value'])) {
                $condition['value'] = $this->resolveConditionValue($condition['value']);
            }

            return $this->propertyConditionMet($condition, $value);
        }

        if (in_array($conditions['operatorIdentifier'], ['and', 'or'], true)
            && (!is_array($conditions['children'] ?? null) || $conditions['children'] === [])) {
            return false;
        }

        if ($conditions['operatorIdentifier'] === 'and') {
            $isvalid = true;
            foreach ($conditions['children'] as $childrenCondition) {
                if (!$this->conditionsAreMet($childrenCondition, $formData)) {
                    $isvalid = false;
                    break;
                }
            }

            return $isvalid;
        } elseif ($conditions['operatorIdentifier'] === 'or') {
            $isvalid = false;
            foreach ($conditions['children'] as $childrenCondition) {
                if ($this->conditionsAreMet($childrenCondition, $formData)) {
                    $isvalid = true;
                    break;
                }
            }

            return $isvalid;
        }

        throw new \Exception('Unexcepted operatorIdentifier:' . $conditions['operatorIdentifier']);
    }

    private function propertyConditionMet(array $propertyCondition, $value): bool
    {
        $type = $propertyCondition['property_meta']['type'] ?? null;
        $fieldId = $propertyCondition['property_meta']['id'] ?? '';

        // Handle computed variables (cv_ prefix)
        if (str_starts_with($fieldId, 'cv_') || $type === 'computed') {
            return $this->computedVariableConditionMet($propertyCondition, $value);
        }

        switch ($type) {
            case 'text':
            case 'url':
            case 'email':
            case 'phone_number':
                return $this->textConditionMet($propertyCondition, $value);
            case 'number':
            case 'rating':
            case 'scale':
            case 'slider':
                return $this->numberConditionMet($propertyCondition, $value);
            case 'checkbox':
                return $this->checkboxConditionMet($propertyCondition, $value);
            case 'select':
                return $this->selectConditionMet($propertyCondition, $value);
            case 'date':
                return $this->dateConditionMet($propertyCondition, $value);
            case 'multi_select':
                return $this->multiSelectConditionMet($propertyCondition, $value);
            case 'files':
                return $this->filesConditionMet($propertyCondition, $value);
            case 'matrix':
                return $this->matrixConditionMet($propertyCondition, $value);
            case 'payment':
                return $this->paymentConditionMet($propertyCondition, $value);
        }

        return false;
    }

    /**
     * Handle conditions for computed variables
     * Computed variables can be numbers, text, or booleans
     */
    private function computedVariableConditionMet(array $propertyCondition, $value): bool
    {
        $operator = $propertyCondition['operator'] ?? null;

        // Check if value is numeric and use number conditions
        if (is_numeric($value)) {
            return $this->numberConditionMet($propertyCondition, $value);
        }

        // Check if value is boolean
        if (is_bool($value)) {
            return match ($operator) {
                'equals' => $value === ($propertyCondition['value'] ?? null),
                'does_not_equal' => $value !== ($propertyCondition['value'] ?? null),
                'is_checked' => $value === true,
                'is_not_checked' => $value === false,
                default => false
            };
        }

        // Default to text conditions
        return $this->textConditionMet($propertyCondition, $value);
    }

    private function checkEquals($condition, $fieldValue): bool
    {
        if (!isset($condition['value'])) {
            return false;
        }
        // For numeric values, convert to numbers before comparison
        if (
            $this->areValidNumbers($condition, $fieldValue) &&
            is_numeric($condition['value']) &&
            is_numeric($fieldValue)
        ) {
            return (float) $condition['value'] === (float) $fieldValue;
        }

        return $condition['value'] === $fieldValue;
    }

    private function checkContains($condition, $fieldValue): bool
    {
        if (!isset($condition['value'])) {
            return false;
        }
        if (is_array($fieldValue)) {
            return in_array($condition['value'], $fieldValue);
        }
        if (!is_string($fieldValue)) {
            return false;
        }
        if (!is_string($condition['value'])) {
            return false;
        }
        return \Illuminate\Support\Str::contains($fieldValue, $condition['value']);
    }

    private function checkMatrixContains($condition, $fieldValue): bool
    {
        if (!isset($condition['value']) || !is_array($condition['value'])) {
            return false;
        }
        if (!is_array($fieldValue)) {
            return false;
        }

        foreach ($condition['value'] as $key => $value) {
            // Skip rows that don't exist in the field value
            if (!array_key_exists($key, $fieldValue)) {
                continue;
            }
            // If any row matches, return true (contains semantics)
            if ($condition['value'][$key] == $fieldValue[$key]) {
                return true;
            }
        }
        return false;
    }

    private function checkMatrixEquals($condition, $fieldValue): bool
    {
        if (!isset($condition['value'])) {
            return false;
        }
        if (!is_array($fieldValue)) {
            return false;
        }
        foreach ($condition['value'] as $key => $value) {
            // Check if the key exists in the field value before comparing
            if (!array_key_exists($key, $fieldValue)) {
                return false;
            }
            if ($condition['value'][$key] !== $fieldValue[$key]) {
                return false;
            }
        }
        return true;
    }

    private function checkListContains($condition, $fieldValue): bool
    {
        if (is_null($fieldValue)) {
            return false;
        }

        if (!is_array($fieldValue)) {
            return $this->checkEquals($condition, $fieldValue);
        }

        if (!isset($condition['value'])) {
            return false;
        }
        if (is_array($condition['value'])) {
            return count(array_intersect($condition['value'], $fieldValue)) === count($condition['value']);
        } else {
            return in_array($condition['value'], $fieldValue);
        }
    }

    private function checkStartsWith($condition, $fieldValue): bool
    {
        if (!isset($condition['value'])) {
            return false;
        }
        if (!is_string($fieldValue) || !is_string($condition['value'])) {
            return false;
        }
        return str_starts_with($fieldValue, $condition['value']);
    }

    private function checkEndsWith($condition, $fieldValue): bool
    {
        if (!isset($condition['value'])) {
            return false;
        }
        if (!is_string($fieldValue) || !is_string($condition['value'])) {
            return false;
        }
        return str_ends_with($fieldValue, $condition['value']);
    }

    private function checkIsEmpty($condition, $fieldValue): bool
    {
        if (is_array($fieldValue)) {
            return count($fieldValue) === 0;
        }

        return $fieldValue === '' || $fieldValue === null;
    }

    /**
     * Helper function to check if values are valid for numeric comparison
     */
    private function areValidNumbers($condition, $fieldValue): bool
    {
        return isset($condition['value']) &&
            $fieldValue !== null &&
            $fieldValue !== '' &&
            is_numeric($condition['value']) &&
            is_numeric($fieldValue);
    }

    private function checkGreaterThan($condition, $fieldValue): bool
    {
        if (!$this->areValidNumbers($condition, $fieldValue)) {
            return false;
        }
        return (float) $fieldValue > (float) $condition['value'];
    }

    private function checkGreaterThanEqual($condition, $fieldValue): bool
    {
        if (!$this->areValidNumbers($condition, $fieldValue)) {
            return false;
        }
        return (float) $fieldValue >= (float) $condition['value'];
    }

    private function checkLessThan($condition, $fieldValue): bool
    {
        if (!$this->areValidNumbers($condition, $fieldValue)) {
            return false;
        }
        return (float) $fieldValue < (float) $condition['value'];
    }

    private function checkLessThanEqual($condition, $fieldValue): bool
    {
        if (!$this->areValidNumbers($condition, $fieldValue)) {
            return false;
        }
        return (float) $fieldValue <= (float) $condition['value'];
    }

    private function checkPastWeek($condition, $fieldValue): bool
    {
        if (!$fieldValue) {
            return false;
        }
        $fieldDate = date('Y-m-d', strtotime($fieldValue));

        return $fieldDate <= now()->toDateString() && $fieldDate >= now()->subDays(7)->toDateString();
    }

    private function checkPastMonth($condition, $fieldValue): bool
    {
        if (!$fieldValue) {
            return false;
        }
        $fieldDate = date('Y-m-d', strtotime($fieldValue));

        return $fieldDate <= now()->toDateString() && $fieldDate >= now()->subMonths(1)->toDateString();
    }

    private function checkPastYear($condition, $fieldValue): bool
    {
        if (!$fieldValue) {
            return false;
        }
        $fieldDate = date('Y-m-d', strtotime($fieldValue));

        return $fieldDate <= now()->toDateString() && $fieldDate >= now()->subYears(1)->toDateString();
    }

    private function checkNextWeek($condition, $fieldValue): bool
    {
        if (!$fieldValue) {
            return false;
        }
        $fieldDate = date('Y-m-d', strtotime($fieldValue));

        return $fieldDate >= now()->toDateString() && $fieldDate <= now()->addDays(7)->toDateString();
    }

    private function checkNextMonth($condition, $fieldValue): bool
    {
        if (!$fieldValue) {
            return false;
        }
        $fieldDate = date('Y-m-d', strtotime($fieldValue));

        return $fieldDate >= now()->toDateString() && $fieldDate <= now()->addMonths(1)->toDateString();
    }

    private function checkNextYear($condition, $fieldValue): bool
    {
        if (!$fieldValue) {
            return false;
        }
        $fieldDate = date('Y-m-d', strtotime($fieldValue));

        return $fieldDate >= now()->toDateString() && $fieldDate <= now()->addYears(1)->toDateString();
    }

    private function checkAtLeastXDaysAgo($condition, $fieldValue): bool
    {
        if (!$fieldValue || !$condition['value']) {
            return false;
        }

        // Validate date is valid
        $timestamp = strtotime($fieldValue);
        if ($timestamp === false) {
            return false;
        }
        $fieldDate = date('Y-m-d', $timestamp);

        // Validate days is a valid positive number
        if (!is_numeric($condition['value'])) {
            return false;
        }
        $daysBefore = (int) $condition['value'];
        if ($daysBefore < 0) {
            return false;
        }

        // Create target date by subtracting days from today
        $targetDate = now()->subDays($daysBefore)->toDateString();

        // Return true if fieldDate is on or before the target date (X days before today)
        return $fieldDate <= $targetDate;
    }

    private function checkAtLeastXDaysFromNow($condition, $fieldValue): bool
    {
        if (!$fieldValue || !$condition['value']) {
            return false;
        }

        // Validate date is valid
        $timestamp = strtotime($fieldValue);
        if ($timestamp === false) {
            return false;
        }
        $fieldDate = date('Y-m-d', $timestamp);

        // Validate days is a valid positive number
        if (!is_numeric($condition['value'])) {
            return false;
        }
        $daysAfter = (int) $condition['value'];
        if ($daysAfter < 0) {
            return false;
        }

        // Create target date by adding days to today
        $targetDate = now()->addDays($daysAfter)->toDateString();

        // Return true if fieldDate is on or after the target date (X days after today)
        return $fieldDate >= $targetDate;
    }

    private function checkWithinPastXDays($condition, $fieldValue): bool
    {
        if (!$fieldValue || !$condition['value']) {
            return false;
        }

        // Validate date is valid
        $timestamp = strtotime($fieldValue);
        if ($timestamp === false) {
            return false;
        }
        $fieldDate = date('Y-m-d', $timestamp);

        // Validate days is a valid positive number
        if (!is_numeric($condition['value'])) {
            return false;
        }
        $daysBefore = (int) $condition['value'];
        if ($daysBefore < 0) {
            return false;
        }

        // Create target date by subtracting days from today
        $targetDate = now()->subDays($daysBefore)->toDateString();
        $today = now()->toDateString();

        // Return true if fieldDate is between target date (X days ago) and today (inclusive)
        return $fieldDate >= $targetDate && $fieldDate <= $today;
    }

    private function checkWithinNextXDays($condition, $fieldValue): bool
    {
        if (!$fieldValue || !$condition['value']) {
            return false;
        }

        // Validate date is valid
        $timestamp = strtotime($fieldValue);
        if ($timestamp === false) {
            return false;
        }
        $fieldDate = date('Y-m-d', $timestamp);

        // Validate days is a valid positive number
        if (!is_numeric($condition['value'])) {
            return false;
        }
        $daysAfter = (int) $condition['value'];
        if ($daysAfter < 0) {
            return false;
        }

        // Create target date by adding days to today
        $targetDate = now()->addDays($daysAfter)->toDateString();
        $today = now()->toDateString();

        // Return true if fieldDate is between today and target date (X days from now) (inclusive)
        return $fieldDate >= $today && $fieldDate <= $targetDate;
    }

    private function checkLength($condition, $fieldValue, $operator = '==='): bool
    {
        if (!$fieldValue || !is_string($fieldValue) || strlen($fieldValue) === 0) {
            return false;
        }
        if (!isset($condition['value']) || !is_numeric($condition['value'])) {
            return false;
        }
        switch ($operator) {
            case '===':
                return strlen($fieldValue) === (int) $condition['value'];
            case '!==':
                return strlen($fieldValue) !== (int) $condition['value'];
            case '>':
                return strlen($fieldValue) > (int) $condition['value'];
            case '>=':
                return strlen($fieldValue) >= (int) $condition['value'];
            case '<':
                return strlen($fieldValue) < (int) $condition['value'];
            case '<=':
                return strlen($fieldValue) <= (int) $condition['value'];
        }

        return false;
    }

    private function checkExistsInSubmissions($condition, $fieldValue): bool
    {
        if (!$fieldValue || !isset($condition['property_meta']['id'])) {
            return false;
        }

        $formId = $this->formData['form']['id'] ?? null;
        if (!$formId) {
            return false;
        }

        $fieldId = $condition['property_meta']['id'];

        // Validate field ID format to prevent SQL injection
        // Field IDs should only contain alphanumeric characters, underscores, and hyphens
        if (!preg_match('/^[a-zA-Z0-9_-]+$/', $fieldId)) {
            return false;
        }

        $dbConnection = DB::connection()->getDriverName();

        $query = FormSubmission::where('form_id', $formId)
            ->where('status', '!=', FormSubmission::STATUS_PARTIAL);

        // SQLite does not support row-level locking for this query path.
        if ($dbConnection !== 'sqlite') {
            $query->lockForUpdate();
        }

        if ($dbConnection === 'mysql') {
            // MySQL: Use fully parameterized JSON path query
            // JSON_EXTRACT with CONCAT for safe field ID handling
            if (is_array($fieldValue)) {
                // For array values (multi_select, matrix)
                $query->whereRaw(
                    "JSON_CONTAINS(JSON_EXTRACT(data, CONCAT('\$.\"', ?, '\"')), ?)",
                    [$fieldId, json_encode($fieldValue)]
                );
            } else {
                // For scalar values
                $query->whereRaw(
                    "JSON_UNQUOTE(JSON_EXTRACT(data, CONCAT('\$.\"', ?, '\"'))) = ?",
                    [$fieldId, $fieldValue]
                );
            }
        } elseif ($dbConnection === 'pgsql') {
            // PostgreSQL: Use parameterized queries with -> operator
            if (is_array($fieldValue)) {
                // For array values (multi_select, matrix)
                $query->whereRaw("data->? @> ?::jsonb", [
                    $fieldId,
                    json_encode($fieldValue)
                ]);
            } else {
                // For scalar values
                $query->whereRaw("data->? = ?::jsonb", [$fieldId, json_encode($fieldValue)]);
            }
        } elseif ($dbConnection === 'sqlite') {
            // SQLite JSON1: compare extracted JSON values from submissions.
            $jsonPath = '$."' . $fieldId . '"';

            if (is_array($fieldValue)) {
                $query->whereRaw("json_extract(data, ?) = json(?)", [
                    $jsonPath,
                    json_encode($fieldValue),
                ]);
            } else {
                $query->whereRaw("json_extract(data, ?) = ?", [$jsonPath, $fieldValue]);
            }
        } else {
            return false;
        }

        return $query->exists();
    }

    private function textConditionMet(array $propertyCondition, $value): bool
    {
        switch ($propertyCondition['operator']) {
            case 'equals':
                return $this->checkEquals($propertyCondition, $value);
            case 'does_not_equal':
                return !$this->checkEquals($propertyCondition, $value);
            case 'contains':
                return $this->checkContains($propertyCondition, $value);
            case 'does_not_contain':
                return !$this->checkContains($propertyCondition, $value);
            case 'starts_with':
                return $this->checkStartsWith($propertyCondition, $value);
            case 'ends_with':
                return $this->checkEndsWith($propertyCondition, $value);
            case 'is_empty':
                return $this->checkIsEmpty($propertyCondition, $value);
            case 'is_not_empty':
                return !$this->checkIsEmpty($propertyCondition, $value);
            case 'content_length_equals':
                return $this->checkLength($propertyCondition, $value, '===');
            case 'content_length_does_not_equal':
                return $this->checkLength($propertyCondition, $value, '!==');
            case 'content_length_greater_than':
                return $this->checkLength($propertyCondition, $value, '>');
            case 'content_length_greater_than_or_equal_to':
                return $this->checkLength($propertyCondition, $value, '>=');
            case 'content_length_less_than':
                return $this->checkLength($propertyCondition, $value, '<');
            case 'content_length_less_than_or_equal_to':
                return $this->checkLength($propertyCondition, $value, '<=');
            case 'matches_regex':
                if (! is_string($propertyCondition['value']) || ! is_string($value)) {
                    return false;
                }
                return FormRegex::matches($propertyCondition['value'], $value) ?? false;
            case 'does_not_match_regex':
                if (! is_string($propertyCondition['value']) || ! is_string($value)) {
                    return true;
                }
                $matches = FormRegex::matches($propertyCondition['value'], $value);

                return $matches === null ? false : ! $matches;
            case 'exists_in_submissions':
                return $this->checkExistsInSubmissions($propertyCondition, $value);
            case 'does_not_exist_in_submissions':
                return !$this->checkExistsInSubmissions($propertyCondition, $value);
        }

        return false;
    }

    private function numberConditionMet(array $propertyCondition, $value): bool
    {
        switch ($propertyCondition['operator']) {
            case 'equals':
                return $this->checkEquals($propertyCondition, $value);
            case 'does_not_equal':
                return !$this->checkEquals($propertyCondition, $value);
            case 'greater_than':
                return $this->checkGreaterThan($propertyCondition, $value);
            case 'less_than':
                return $this->checkLessThan($propertyCondition, $value);
            case 'greater_than_or_equal_to':
                return $this->checkGreaterThanEqual($propertyCondition, $value);
            case 'less_than_or_equal_to':
                return $this->checkLessThanEqual($propertyCondition, $value);
            case 'is_empty':
                return $this->checkIsEmpty($propertyCondition, $value);
            case 'is_not_empty':
                return !$this->checkIsEmpty($propertyCondition, $value);
            case 'content_length_equals':
                return $this->checkLength($propertyCondition, $value, '===');
            case 'content_length_does_not_equal':
                return $this->checkLength($propertyCondition, $value, '!==');
            case 'content_length_greater_than':
                return $this->checkLength($propertyCondition, $value, '>');
            case 'content_length_greater_than_or_equal_to':
                return $this->checkLength($propertyCondition, $value, '>=');
            case 'content_length_less_than':
                return $this->checkLength($propertyCondition, $value, '<');
            case 'content_length_less_than_or_equal_to':
                return $this->checkLength($propertyCondition, $value, '<=');
            case 'exists_in_submissions':
                return $this->checkExistsInSubmissions($propertyCondition, $value);
            case 'does_not_exist_in_submissions':
                return !$this->checkExistsInSubmissions($propertyCondition, $value);
        }

        return false;
    }

    private function checkboxConditionMet(array $propertyCondition, $value): bool
    {
        // Treat null or missing values as false
        if ($value === null || !isset($value)) {
            $value = false;
        }

        switch ($propertyCondition['operator']) {
            case 'is_checked':
                return $value === true;
            case 'is_not_checked':
                return $value === false;
                // Legacy operators
            case 'equals':
                return $value === true;
            case 'does_not_equal':
                return $value === false;
        }

        return false;
    }

    private function selectConditionMet(array $propertyCondition, $value): bool
    {
        switch ($propertyCondition['operator']) {
            case 'equals':
                return $this->checkEquals($propertyCondition, $value);
            case 'does_not_equal':
                return !$this->checkEquals($propertyCondition, $value);
            case 'is_empty':
                return $this->checkIsEmpty($propertyCondition, $value);
            case 'is_not_empty':
                return !$this->checkIsEmpty($propertyCondition, $value);
            case 'exists_in_submissions':
                return $this->checkExistsInSubmissions($propertyCondition, $value);
            case 'does_not_exist_in_submissions':
                return !$this->checkExistsInSubmissions($propertyCondition, $value);
        }

        return false;
    }

    private function dateConditionMet(array $propertyCondition, $value): bool
    {
        if (in_array($propertyCondition['operator'], ['equals', 'before', 'after', 'on_or_before', 'on_or_after'], true)) {
            $field = $this->dateFields[$propertyCondition['property_meta']['id'] ?? ''] ?? null;
            $withTime = $field !== null ? (bool) ($field['with_time'] ?? false) : (bool) ($propertyCondition['property_meta']['with_time'] ?? true);
            $left = FormDateValue::comparisonValue($value, $withTime);
            $right = FormDateValue::comparisonValue($propertyCondition['value'] ?? null, $withTime);
            if ($left === null || $right === null) {
                return false;
            }

            return match ($propertyCondition['operator']) {
                'equals' => $left === $right,
                'before' => $left < $right,
                'after' => $left > $right,
                'on_or_before' => $left <= $right,
                'on_or_after' => $left >= $right,
            };
        }
        switch ($propertyCondition['operator']) {
            case 'at_least_x_days_ago':
                return $this->checkAtLeastXDaysAgo($propertyCondition, $value);
            case 'at_least_x_days_from_now':
                return $this->checkAtLeastXDaysFromNow($propertyCondition, $value);
            case 'within_past_x_days':
                return $this->checkWithinPastXDays($propertyCondition, $value);
            case 'within_next_x_days':
                return $this->checkWithinNextXDays($propertyCondition, $value);
            case 'is_empty':
                return $this->checkIsEmpty($propertyCondition, $value);
            case 'past_week':
                return $this->checkPastWeek($propertyCondition, $value);
            case 'past_month':
                return $this->checkPastMonth($propertyCondition, $value);
            case 'past_year':
                return $this->checkPastYear($propertyCondition, $value);
            case 'next_week':
                return $this->checkNextWeek($propertyCondition, $value);
            case 'next_month':
                return $this->checkNextMonth($propertyCondition, $value);
            case 'next_year':
                return $this->checkNextYear($propertyCondition, $value);
            case 'exists_in_submissions':
                return $this->checkExistsInSubmissions($propertyCondition, $value);
            case 'does_not_exist_in_submissions':
                return !$this->checkExistsInSubmissions($propertyCondition, $value);
        }

        return false;
    }

    private function multiSelectConditionMet(array $propertyCondition, $value): bool
    {
        switch ($propertyCondition['operator']) {
            case 'contains':
                return $this->checkListContains($propertyCondition, $value);
            case 'does_not_contain':
                return !$this->checkListContains($propertyCondition, $value);
            case 'is_empty':
                return $this->checkIsEmpty($propertyCondition, $value);
            case 'is_not_empty':
                return !$this->checkIsEmpty($propertyCondition, $value);
            case 'exists_in_submissions':
                return $this->checkExistsInSubmissions($propertyCondition, $value);
            case 'does_not_exist_in_submissions':
                return !$this->checkExistsInSubmissions($propertyCondition, $value);
        }

        return false;
    }

    private function filesConditionMet(array $propertyCondition, $value): bool
    {
        switch ($propertyCondition['operator']) {
            case 'is_empty':
                return $this->checkIsEmpty($propertyCondition, $value);
            case 'is_not_empty':
                return !$this->checkIsEmpty($propertyCondition, $value);
        }

        return false;
    }

    private function matrixConditionMet(array $propertyCondition, $value): bool
    {
        switch ($propertyCondition['operator']) {
            case 'equals':
                return $this->checkMatrixEquals($propertyCondition, $value);
            case 'does_not_equal':
                return !$this->checkMatrixEquals($propertyCondition, $value);
            case 'contains':
                return $this->checkMatrixContains($propertyCondition, $value);
            case 'does_not_contain':
                return !$this->checkMatrixContains($propertyCondition, $value);
            case 'exists_in_submissions':
                return $this->checkExistsInSubmissions($propertyCondition, $value);
            case 'does_not_exist_in_submissions':
                return !$this->checkExistsInSubmissions($propertyCondition, $value);
        }

        return false;
    }

    private function paymentConditionMet(array $propertyCondition, $value): bool
    {
        switch ($propertyCondition['operator']) {
            case 'paid':
                return $this->checkPaid($propertyCondition, $value);
            case 'not_paid':
                return !$this->checkPaid($propertyCondition, $value);
        }

        return false;
    }

    private function checkPaid($propertyCondition, $value): bool
    {
        return ($value) ? str_starts_with($value, 'pi_') : false;
    }
}