opnform/app/Rules/FormPropertyLogicRule.php

683 lines
22 KiB
PHP
Raw Permalink Normal View History

2022-09-20 19:59:52 +00:00
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\DataAwareRule;
2024-02-23 10:54:12 +00:00
use Illuminate\Contracts\Validation\Rule;
2022-09-20 19:59:52 +00:00
use Illuminate\Support\Str;
2024-02-23 10:54:12 +00:00
class FormPropertyLogicRule implements DataAwareRule, Rule
{
2024-02-23 10:54:12 +00:00
public const ACTIONS_VALUES = [
2022-09-20 19:59:52 +00:00
'show-block',
'hide-block',
'make-it-optional',
'require-answer',
'enable-block',
2024-02-23 10:54:12 +00:00
'disable-block',
2022-09-20 19:59:52 +00:00
];
2024-02-23 10:54:12 +00:00
public const CONDITION_MAPPING = [
2022-09-20 19:59:52 +00:00
'text' => [
'comparators' => [
'equals' => [
'expected_type' => 'string',
],
'does_not_equal' => [
'expected_type' => 'string',
],
'contains' => [
'expected_type' => 'string',
],
'does_not_contain' => [
'expected_type' => 'string',
],
'starts_with' => [
'expected_type' => 'string',
],
'ends_with' => [
'expected_type' => 'string',
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
2022-09-20 19:59:52 +00:00
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
],
2023-01-21 11:58:01 +00:00
'content_length_equals' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_does_not_equal' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_greater_than' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_greater_than_or_equal_to' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_less_than' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_less_than_or_equal_to' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
],
],
2022-09-20 19:59:52 +00:00
],
'url' => [
'comparators' => [
'equals' => [
'expected_type' => 'string',
],
'does_not_equal' => [
'expected_type' => 'string',
],
'contains' => [
'expected_type' => 'string',
],
'does_not_contain' => [
'expected_type' => 'string',
],
'starts_with' => [
'expected_type' => 'string',
],
'ends_with' => [
'expected_type' => 'string',
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
2022-09-20 19:59:52 +00:00
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
2023-01-21 11:58:01 +00:00
],
'content_length_equals' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_does_not_equal' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_greater_than' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_greater_than_or_equal_to' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_less_than' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_less_than_or_equal_to' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
],
],
2022-09-20 19:59:52 +00:00
],
'email' => [
'comparators' => [
'equals' => [
'expected_type' => 'string',
],
'does_not_equal' => [
'expected_type' => 'string',
],
'contains' => [
'expected_type' => 'string',
],
'does_not_contain' => [
'expected_type' => 'string',
],
'starts_with' => [
'expected_type' => 'string',
],
'ends_with' => [
'expected_type' => 'string',
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
2022-09-20 19:59:52 +00:00
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
2023-01-21 11:58:01 +00:00
],
'content_length_equals' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_does_not_equal' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_greater_than' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_greater_than_or_equal_to' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_less_than' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_less_than_or_equal_to' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
],
],
2022-09-20 19:59:52 +00:00
],
'phone_number' => [
'comparators' => [
'equals' => [
'expected_type' => 'string',
],
'does_not_equal' => [
'expected_type' => 'string',
],
'contains' => [
'expected_type' => 'string',
],
'does_not_contain' => [
'expected_type' => 'string',
],
'starts_with' => [
'expected_type' => 'string',
],
'ends_with' => [
'expected_type' => 'string',
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
2022-09-20 19:59:52 +00:00
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
2023-01-21 11:58:01 +00:00
],
'content_length_equals' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_does_not_equal' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_greater_than' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_greater_than_or_equal_to' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_less_than' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_less_than_or_equal_to' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
],
],
2022-09-20 19:59:52 +00:00
],
'number' => [
'comparators' => [
'equals' => [
'expected_type' => 'number',
],
'does_not_equal' => [
'expected_type' => 'number',
],
'greater_than' => [
'expected_type' => 'number',
],
'less_than' => [
'expected_type' => 'number',
],
'greater_than_or_equal_to' => [
'expected_type' => 'number',
],
'less_than_or_equal_to' => [
'expected_type' => 'number',
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
2022-09-20 19:59:52 +00:00
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
2023-01-21 11:58:01 +00:00
],
'content_length_equals' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_does_not_equal' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_greater_than' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_greater_than_or_equal_to' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_less_than' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
2023-01-21 11:58:01 +00:00
],
'content_length_less_than_or_equal_to' => [
2024-02-23 10:54:12 +00:00
'expected_type' => 'number',
],
],
2022-09-20 19:59:52 +00:00
],
'checkbox' => [
'comparators' => [
'equals' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
2022-09-20 19:59:52 +00:00
],
'does_not_equal' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
2022-09-20 19:59:52 +00:00
],
2024-02-23 10:54:12 +00:00
],
2022-09-20 19:59:52 +00:00
],
'select' => [
'comparators' => [
'equals' => [
'expected_type' => 'string',
],
'does_not_equal' => [
'expected_type' => 'string',
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
2022-09-20 19:59:52 +00:00
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
],
],
2022-09-20 19:59:52 +00:00
],
'multi_select' => [
'comparators' => [
'contains' => [
'expected_type' => ['object', 'string'],
2022-09-20 19:59:52 +00:00
'format' => [
'type' => 'uuid',
2024-02-23 10:54:12 +00:00
],
2022-09-20 19:59:52 +00:00
],
'does_not_contain' => [
'expected_type' => ['object', 'string'],
2022-09-20 19:59:52 +00:00
'format' => [
'type' => 'uuid',
2024-02-23 10:54:12 +00:00
],
2022-09-20 19:59:52 +00:00
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
2022-09-20 19:59:52 +00:00
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
],
],
2022-09-20 19:59:52 +00:00
],
'date' => [
'comparators' => [
'equals' => [
'expected_type' => 'string',
'format' => [
2024-02-23 10:54:12 +00:00
'type' => 'date',
],
2022-09-20 19:59:52 +00:00
],
'before' => [
'expected_type' => 'string',
'format' => [
2024-02-23 10:54:12 +00:00
'type' => 'date',
],
2022-09-20 19:59:52 +00:00
],
'after' => [
'expected_type' => 'string',
'format' => [
2024-02-23 10:54:12 +00:00
'type' => 'date',
],
2022-09-20 19:59:52 +00:00
],
'on_or_before' => [
'expected_type' => 'string',
'format' => [
2024-02-23 10:54:12 +00:00
'type' => 'date',
],
2022-09-20 19:59:52 +00:00
],
'on_or_after' => [
'expected_type' => 'string',
'format' => [
2024-02-23 10:54:12 +00:00
'type' => 'date',
],
2022-09-20 19:59:52 +00:00
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
2022-09-20 19:59:52 +00:00
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
2022-09-20 19:59:52 +00:00
],
'past_week' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
2024-02-23 10:54:12 +00:00
'values' => '{}',
],
2022-09-20 19:59:52 +00:00
],
'past_month' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
2024-02-23 10:54:12 +00:00
'values' => '{}',
],
2022-09-20 19:59:52 +00:00
],
'past_year' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
2024-02-23 10:54:12 +00:00
'values' => '{}',
],
2022-09-20 19:59:52 +00:00
],
'next_week' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
2024-02-23 10:54:12 +00:00
'values' => '{}',
],
2022-09-20 19:59:52 +00:00
],
'next_month' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
2024-02-23 10:54:12 +00:00
'values' => '{}',
],
2022-09-20 19:59:52 +00:00
],
'next_year' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
2024-02-23 10:54:12 +00:00
'values' => '{}',
],
],
],
2022-09-20 19:59:52 +00:00
],
'files' => [
'comparators' => [
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
2022-09-20 19:59:52 +00:00
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
2024-02-23 10:54:12 +00:00
'values' => [true],
],
],
],
2022-09-20 19:59:52 +00:00
],
];
private $isConditionCorrect = true;
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
private $isActionCorrect = true;
2024-02-23 10:54:12 +00:00
private $conditionErrors = [];
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
private $field = [];
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
private $data = [];
private function checkBaseCondition($condition)
{
2022-09-20 19:59:52 +00:00
2024-02-23 10:54:12 +00:00
if (! isset($condition['value'])) {
2022-09-20 19:59:52 +00:00
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition body';
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return;
}
2024-02-23 10:54:12 +00:00
if (! isset($condition['value']['property_meta'])) {
2022-09-20 19:59:52 +00:00
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition property';
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return;
}
2024-02-23 10:54:12 +00:00
if (! isset($condition['value']['property_meta']['type'])) {
2022-09-20 19:59:52 +00:00
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition property type';
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return;
}
2024-02-23 10:54:12 +00:00
if (! isset($condition['value']['operator'])) {
2022-09-20 19:59:52 +00:00
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition operator';
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return;
}
2024-02-23 10:54:12 +00:00
if (! isset($condition['value']['value'])) {
2022-09-20 19:59:52 +00:00
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition value';
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return;
}
$typeField = $condition['value']['property_meta']['type'];
$operator = $condition['value']['operator'];
$value = $condition['value']['value'];
2022-09-20 19:59:52 +00:00
2024-02-23 10:54:12 +00:00
if (! isset(self::CONDITION_MAPPING[$typeField])) {
2022-09-20 19:59:52 +00:00
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'configuration not found for condition type';
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return;
}
2024-02-23 10:54:12 +00:00
if (! isset(self::CONDITION_MAPPING[$typeField]['comparators'][$operator])) {
2022-09-20 19:59:52 +00:00
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'configuration not found for condition operator';
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return;
}
$type = self::CONDITION_MAPPING[$typeField]['comparators'][$operator]['expected_type'];
if (is_array($type)) {
$foundCorrectType = false;
foreach ($type as $subtype) {
if ($this->valueHasCorrectType($subtype, $value)) {
$foundCorrectType = true;
}
}
2024-02-23 10:54:12 +00:00
if (! $foundCorrectType) {
$this->isConditionCorrect = false;
}
} else {
2024-02-23 10:54:12 +00:00
if (! $this->valueHasCorrectType($type, $value)) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'wrong type of condition value';
}
}
}
private function valueHasCorrectType($type, $value)
{
2022-09-20 19:59:52 +00:00
if (
($type === 'string' && gettype($value) !== 'string') ||
2024-02-23 10:54:12 +00:00
($type === 'boolean' && ! is_bool($value)) ||
($type === 'number' && ! is_numeric($value)) ||
($type === 'object' && ! is_array($value))
2022-09-20 19:59:52 +00:00
) {
return false;
2022-09-20 19:59:52 +00:00
}
2024-02-23 10:54:12 +00:00
return true;
2022-09-20 19:59:52 +00:00
}
private function checkConditions($conditions)
{
if (array_key_exists('operatorIdentifier', $conditions)) {
2022-09-20 19:59:52 +00:00
if (($conditions['operatorIdentifier'] !== 'and') && ($conditions['operatorIdentifier'] !== 'or')) {
$this->conditionErrors[] = 'missing operator';
2022-09-20 19:59:52 +00:00
$this->isConditionCorrect = false;
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return;
}
if (isset($conditions['operatorIdentifier']['children'])) {
$this->conditionErrors[] = 'extra condition';
2022-09-20 19:59:52 +00:00
$this->isConditionCorrect = false;
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return;
}
2024-02-23 10:54:12 +00:00
if (! is_array($conditions['children'])) {
$this->conditionErrors[] = 'wrong sub-condition type';
2022-09-20 19:59:52 +00:00
$this->isConditionCorrect = false;
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return;
}
foreach ($conditions['children'] as &$child) {
$this->checkConditions($child);
}
2024-02-23 10:54:12 +00:00
} elseif (isset($conditions['identifier'])) {
2022-09-20 19:59:52 +00:00
$this->checkBaseCondition($conditions);
}
}
private function checkActions($actions)
{
if (is_array($actions) && count($actions) > 0) {
2024-02-23 10:54:12 +00:00
foreach ($actions as $val) {
if (! in_array($val, static::ACTIONS_VALUES) ||
(in_array($this->field['type'], ['nf-text', 'nf-code', 'nf-page-break', 'nf-divider', 'nf-image']) && ! in_array($val, ['hide-block', 'show-block'])) ||
(isset($this->field['hidden']) && $this->field['hidden'] && ! in_array($val, ['show-block', 'require-answer'])) ||
(isset($this->field['required']) && $this->field['required'] && ! in_array($val, ['make-it-optional', 'hide-block', 'disable-block'])) ||
(isset($this->field['disabled']) && $this->field['disabled'] && ! in_array($val, ['enable-block', 'require-answer', 'make-it-optional']))
2022-09-20 19:59:52 +00:00
) {
$this->isActionCorrect = false;
break;
}
}
} else {
$this->isActionCorrect = false;
2022-09-20 19:59:52 +00:00
}
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
2022-09-20 19:59:52 +00:00
$this->setProperty($attribute);
2024-02-23 10:54:12 +00:00
if (isset($value['conditions'])) {
$this->checkConditions($value['conditions']);
$this->checkActions($value['actions'] ?? null);
2022-09-20 19:59:52 +00:00
}
2024-02-23 10:54:12 +00:00
return $this->isConditionCorrect && $this->isActionCorrect;
2022-09-20 19:59:52 +00:00
}
/**
* Get the validation error message.
*/
public function message()
{
$message = null;
if (! $this->isConditionCorrect) {
$message = 'The logic conditions for '.$this->field['name'].' are not complete.';
2024-02-23 10:54:12 +00:00
} elseif (! $this->isActionCorrect) {
$message = 'The logic actions for '.$this->field['name'].' are not valid.';
2022-09-20 19:59:52 +00:00
}
if (count($this->conditionErrors) > 0) {
2024-02-23 10:54:12 +00:00
return $message.' Error detail(s): '.implode(', ', $this->conditionErrors);
2022-09-20 19:59:52 +00:00
}
return $message;
2022-09-20 19:59:52 +00:00
}
/**
* Set the data under validation.
*
* @param array $data
* @return $this
*/
public function setData($data)
{
$this->data = $data;
$this->isConditionCorrect = true;
$this->isActionCorrect = true;
$this->conditionErrors = [];
2022-09-20 19:59:52 +00:00
return $this;
}
private function setProperty(string $attributeKey)
{
$attributeKey = Str::of($attributeKey)->replace('.logic', '')->toString();
2022-09-20 19:59:52 +00:00
$this->field = \Arr::get($this->data, $attributeKey);
}
}