Lint PHP code psr-12, add GH action
This commit is contained in:
@@ -2,23 +2,22 @@
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Illuminate\Contracts\Validation\DataAwareRule;
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class FormPropertyLogicRule implements Rule, DataAwareRule
|
||||
class FormPropertyLogicRule implements DataAwareRule, Rule
|
||||
{
|
||||
|
||||
const ACTIONS_VALUES = [
|
||||
public const ACTIONS_VALUES = [
|
||||
'show-block',
|
||||
'hide-block',
|
||||
'make-it-optional',
|
||||
'require-answer',
|
||||
'enable-block',
|
||||
'disable-block'
|
||||
'disable-block',
|
||||
];
|
||||
|
||||
const CONDITION_MAPPING = [
|
||||
public const CONDITION_MAPPING = [
|
||||
'text' => [
|
||||
'comparators' => [
|
||||
'equals' => [
|
||||
@@ -43,35 +42,35 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
'is_not_empty' => [
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
'content_length_equals' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_does_not_equal' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_greater_than' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_greater_than_or_equal_to' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_less_than' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_less_than_or_equal_to' => [
|
||||
'expected_type' => 'number'
|
||||
]
|
||||
]
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
],
|
||||
],
|
||||
'url' => [
|
||||
'comparators' => [
|
||||
@@ -97,35 +96,35 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
'is_not_empty' => [
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
'content_length_equals' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_does_not_equal' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_greater_than' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_greater_than_or_equal_to' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_less_than' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_less_than_or_equal_to' => [
|
||||
'expected_type' => 'number'
|
||||
]
|
||||
]
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
],
|
||||
],
|
||||
'email' => [
|
||||
'comparators' => [
|
||||
@@ -151,35 +150,35 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
'is_not_empty' => [
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
'content_length_equals' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_does_not_equal' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_greater_than' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_greater_than_or_equal_to' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_less_than' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_less_than_or_equal_to' => [
|
||||
'expected_type' => 'number'
|
||||
]
|
||||
]
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
],
|
||||
],
|
||||
'phone_number' => [
|
||||
'comparators' => [
|
||||
@@ -205,35 +204,35 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
'is_not_empty' => [
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
'content_length_equals' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_does_not_equal' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_greater_than' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_greater_than_or_equal_to' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_less_than' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_less_than_or_equal_to' => [
|
||||
'expected_type' => 'number'
|
||||
]
|
||||
]
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
],
|
||||
],
|
||||
'number' => [
|
||||
'comparators' => [
|
||||
@@ -259,35 +258,35 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
'is_not_empty' => [
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
'content_length_equals' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_does_not_equal' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_greater_than' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_greater_than_or_equal_to' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_less_than' => [
|
||||
'expected_type' => 'number'
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'content_length_less_than_or_equal_to' => [
|
||||
'expected_type' => 'number'
|
||||
]
|
||||
]
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
],
|
||||
],
|
||||
'checkbox' => [
|
||||
'comparators' => [
|
||||
@@ -295,18 +294,18 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
'does_not_equal' => [
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
|
||||
]
|
||||
],
|
||||
],
|
||||
'select' => [
|
||||
'comparators' => [
|
||||
@@ -320,17 +319,17 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
'is_not_empty' => [
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'multi_select' => [
|
||||
'comparators' => [
|
||||
@@ -338,119 +337,119 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
|
||||
'expected_type' => ['object', 'string'],
|
||||
'format' => [
|
||||
'type' => 'uuid',
|
||||
]
|
||||
],
|
||||
],
|
||||
'does_not_contain' => [
|
||||
'expected_type' => ['object', 'string'],
|
||||
'format' => [
|
||||
'type' => 'uuid',
|
||||
]
|
||||
],
|
||||
],
|
||||
'is_empty' => [
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
'is_not_empty' => [
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'date' => [
|
||||
'comparators' => [
|
||||
'equals' => [
|
||||
'expected_type' => 'string',
|
||||
'format' => [
|
||||
'type' => 'date'
|
||||
]
|
||||
'type' => 'date',
|
||||
],
|
||||
],
|
||||
'before' => [
|
||||
'expected_type' => 'string',
|
||||
'format' => [
|
||||
'type' => 'date'
|
||||
]
|
||||
'type' => 'date',
|
||||
],
|
||||
],
|
||||
'after' => [
|
||||
'expected_type' => 'string',
|
||||
'format' => [
|
||||
'type' => 'date'
|
||||
]
|
||||
'type' => 'date',
|
||||
],
|
||||
],
|
||||
'on_or_before' => [
|
||||
'expected_type' => 'string',
|
||||
'format' => [
|
||||
'type' => 'date'
|
||||
]
|
||||
'type' => 'date',
|
||||
],
|
||||
],
|
||||
'on_or_after' => [
|
||||
'expected_type' => 'string',
|
||||
'format' => [
|
||||
'type' => 'date'
|
||||
]
|
||||
'type' => 'date',
|
||||
],
|
||||
],
|
||||
'is_empty' => [
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
'is_not_empty' => [
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
'past_week' => [
|
||||
'expected_type' => 'object',
|
||||
'format' => [
|
||||
'type' => 'empty',
|
||||
'values' => '{}'
|
||||
]
|
||||
'values' => '{}',
|
||||
],
|
||||
],
|
||||
'past_month' => [
|
||||
'expected_type' => 'object',
|
||||
'format' => [
|
||||
'type' => 'empty',
|
||||
'values' => '{}'
|
||||
]
|
||||
'values' => '{}',
|
||||
],
|
||||
],
|
||||
'past_year' => [
|
||||
'expected_type' => 'object',
|
||||
'format' => [
|
||||
'type' => 'empty',
|
||||
'values' => '{}'
|
||||
]
|
||||
'values' => '{}',
|
||||
],
|
||||
],
|
||||
'next_week' => [
|
||||
'expected_type' => 'object',
|
||||
'format' => [
|
||||
'type' => 'empty',
|
||||
'values' => '{}'
|
||||
]
|
||||
'values' => '{}',
|
||||
],
|
||||
],
|
||||
'next_month' => [
|
||||
'expected_type' => 'object',
|
||||
'format' => [
|
||||
'type' => 'empty',
|
||||
'values' => '{}'
|
||||
]
|
||||
'values' => '{}',
|
||||
],
|
||||
],
|
||||
'next_year' => [
|
||||
'expected_type' => 'object',
|
||||
'format' => [
|
||||
'type' => 'empty',
|
||||
'values' => '{}'
|
||||
]
|
||||
]
|
||||
]
|
||||
'values' => '{}',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'files' => [
|
||||
'comparators' => [
|
||||
@@ -458,56 +457,65 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
'is_not_empty' => [
|
||||
'expected_type' => 'boolean',
|
||||
'format' => [
|
||||
'type' => 'enum',
|
||||
'values' => [true]
|
||||
]
|
||||
]
|
||||
]
|
||||
'values' => [true],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
private $isConditionCorrect = true;
|
||||
|
||||
private $isActionCorrect = true;
|
||||
|
||||
private $conditionErrors = [];
|
||||
|
||||
private $field = [];
|
||||
|
||||
private $data = [];
|
||||
|
||||
private function checkBaseCondition($condition)
|
||||
{
|
||||
|
||||
if (!isset($condition['value'])) {
|
||||
if (! isset($condition['value'])) {
|
||||
$this->isConditionCorrect = false;
|
||||
$this->conditionErrors[] = 'missing condition body';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($condition['value']['property_meta'])) {
|
||||
if (! isset($condition['value']['property_meta'])) {
|
||||
$this->isConditionCorrect = false;
|
||||
$this->conditionErrors[] = 'missing condition property';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($condition['value']['property_meta']['type'])) {
|
||||
if (! isset($condition['value']['property_meta']['type'])) {
|
||||
$this->isConditionCorrect = false;
|
||||
$this->conditionErrors[] = 'missing condition property type';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($condition['value']['operator'])) {
|
||||
if (! isset($condition['value']['operator'])) {
|
||||
$this->isConditionCorrect = false;
|
||||
$this->conditionErrors[] = 'missing condition operator';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($condition['value']['value'])) {
|
||||
if (! isset($condition['value']['value'])) {
|
||||
$this->isConditionCorrect = false;
|
||||
$this->conditionErrors[] = 'missing condition value';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -515,15 +523,17 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
|
||||
$operator = $condition['value']['operator'];
|
||||
$value = $condition['value']['value'];
|
||||
|
||||
if (!isset(self::CONDITION_MAPPING[$typeField])) {
|
||||
if (! isset(self::CONDITION_MAPPING[$typeField])) {
|
||||
$this->isConditionCorrect = false;
|
||||
$this->conditionErrors[] = 'configuration not found for condition type';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset(self::CONDITION_MAPPING[$typeField]['comparators'][$operator])) {
|
||||
if (! isset(self::CONDITION_MAPPING[$typeField]['comparators'][$operator])) {
|
||||
$this->isConditionCorrect = false;
|
||||
$this->conditionErrors[] = 'configuration not found for condition operator';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -536,11 +546,11 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
|
||||
$foundCorrectType = true;
|
||||
}
|
||||
}
|
||||
if (!$foundCorrectType) {
|
||||
if (! $foundCorrectType) {
|
||||
$this->isConditionCorrect = false;
|
||||
}
|
||||
} else {
|
||||
if (!$this->valueHasCorrectType($type, $value)) {
|
||||
if (! $this->valueHasCorrectType($type, $value)) {
|
||||
$this->isConditionCorrect = false;
|
||||
$this->conditionErrors[] = 'wrong type of condition value';
|
||||
}
|
||||
@@ -551,12 +561,13 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
|
||||
{
|
||||
if (
|
||||
($type === 'string' && gettype($value) !== 'string') ||
|
||||
($type === 'boolean' && !is_bool($value)) ||
|
||||
($type === 'number' && !is_numeric($value)) ||
|
||||
($type === 'object' && !is_array($value))
|
||||
($type === 'boolean' && ! is_bool($value)) ||
|
||||
($type === 'number' && ! is_numeric($value)) ||
|
||||
($type === 'object' && ! is_array($value))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -566,25 +577,28 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
|
||||
if (($conditions['operatorIdentifier'] !== 'and') && ($conditions['operatorIdentifier'] !== 'or')) {
|
||||
$this->conditionErrors[] = 'missing operator';
|
||||
$this->isConditionCorrect = false;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (isset($conditions['operatorIdentifier']['children'])) {
|
||||
$this->conditionErrors[] = 'extra condition';
|
||||
$this->isConditionCorrect = false;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_array($conditions['children'])) {
|
||||
if (! is_array($conditions['children'])) {
|
||||
$this->conditionErrors[] = 'wrong sub-condition type';
|
||||
$this->isConditionCorrect = false;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($conditions['children'] as &$child) {
|
||||
$this->checkConditions($child);
|
||||
}
|
||||
} else if (isset($conditions['identifier'])) {
|
||||
} elseif (isset($conditions['identifier'])) {
|
||||
$this->checkBaseCondition($conditions);
|
||||
}
|
||||
}
|
||||
@@ -592,12 +606,12 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
|
||||
private function checkActions($actions)
|
||||
{
|
||||
if (is_array($actions) && count($actions) > 0) {
|
||||
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']))
|
||||
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']))
|
||||
) {
|
||||
$this->isActionCorrect = false;
|
||||
break;
|
||||
@@ -618,28 +632,27 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
|
||||
public function passes($attribute, $value)
|
||||
{
|
||||
$this->setProperty($attribute);
|
||||
if(isset($value["conditions"])){
|
||||
$this->checkConditions($value["conditions"]);
|
||||
if (isset($value['conditions'])) {
|
||||
$this->checkConditions($value['conditions']);
|
||||
$this->checkActions($value['actions'] ?? null);
|
||||
}
|
||||
|
||||
return ($this->isConditionCorrect && $this->isActionCorrect);
|
||||
return $this->isConditionCorrect && $this->isActionCorrect;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
$message = null;
|
||||
if (! $this->isConditionCorrect) {
|
||||
$message = 'The logic conditions for '.$this->field['name'].' are not complete.';
|
||||
} else if (! $this->isActionCorrect) {
|
||||
} elseif (! $this->isActionCorrect) {
|
||||
$message = 'The logic actions for '.$this->field['name'].' are not valid.';
|
||||
}
|
||||
if (count($this->conditionErrors) > 0) {
|
||||
return $message . ' Error detail(s): '.implode(', ', $this->conditionErrors);
|
||||
return $message.' Error detail(s): '.implode(', ', $this->conditionErrors);
|
||||
}
|
||||
|
||||
return $message;
|
||||
|
||||
Reference in New Issue
Block a user