From e073ca02a8f60e8d538e1ba11ebd21cdaeea7d5c Mon Sep 17 00:00:00 2001 From: Chirag <103994754+chiragnotionforms@users.noreply.github.com> Date: Sat, 21 Jan 2023 17:28:01 +0530 Subject: [PATCH] Content Length Logic Condition (#63) --- app/Rules/FormPropertyLogicRule.php | 90 +++++++++++++++++++ .../Forms/FormLogicConditionChecker.php | 43 +++++++++ resources/data/open_filters.json | 90 +++++++++++++++++++ .../js/forms/FormLogicConditionChecker.js | 45 +++++++++- 4 files changed, 267 insertions(+), 1 deletion(-) diff --git a/app/Rules/FormPropertyLogicRule.php b/app/Rules/FormPropertyLogicRule.php index 1c3ed40..5d3d300 100644 --- a/app/Rules/FormPropertyLogicRule.php +++ b/app/Rules/FormPropertyLogicRule.php @@ -50,6 +50,24 @@ class FormPropertyLogicRule implements Rule, DataAwareRule 'type' => 'enum', 'values' => [true] ] + ], + 'content_length_equals' => [ + 'expected_type' => 'number' + ], + 'content_length_does_not_equal' => [ + 'expected_type' => 'number' + ], + 'content_length_greater_than' => [ + 'expected_type' => 'number' + ], + 'content_length_greater_than_or_equal_to' => [ + 'expected_type' => 'number' + ], + 'content_length_less_than' => [ + 'expected_type' => 'number' + ], + 'content_length_less_than_or_equal_to' => [ + 'expected_type' => 'number' ] ] ], @@ -86,6 +104,24 @@ class FormPropertyLogicRule implements Rule, DataAwareRule 'type' => 'enum', 'values' => [true] ] + ], + 'content_length_equals' => [ + 'expected_type' => 'number' + ], + 'content_length_does_not_equal' => [ + 'expected_type' => 'number' + ], + 'content_length_greater_than' => [ + 'expected_type' => 'number' + ], + 'content_length_greater_than_or_equal_to' => [ + 'expected_type' => 'number' + ], + 'content_length_less_than' => [ + 'expected_type' => 'number' + ], + 'content_length_less_than_or_equal_to' => [ + 'expected_type' => 'number' ] ] ], @@ -122,6 +158,24 @@ class FormPropertyLogicRule implements Rule, DataAwareRule 'type' => 'enum', 'values' => [true] ] + ], + 'content_length_equals' => [ + 'expected_type' => 'number' + ], + 'content_length_does_not_equal' => [ + 'expected_type' => 'number' + ], + 'content_length_greater_than' => [ + 'expected_type' => 'number' + ], + 'content_length_greater_than_or_equal_to' => [ + 'expected_type' => 'number' + ], + 'content_length_less_than' => [ + 'expected_type' => 'number' + ], + 'content_length_less_than_or_equal_to' => [ + 'expected_type' => 'number' ] ] ], @@ -158,6 +212,24 @@ class FormPropertyLogicRule implements Rule, DataAwareRule 'type' => 'enum', 'values' => [true] ] + ], + 'content_length_equals' => [ + 'expected_type' => 'number' + ], + 'content_length_does_not_equal' => [ + 'expected_type' => 'number' + ], + 'content_length_greater_than' => [ + 'expected_type' => 'number' + ], + 'content_length_greater_than_or_equal_to' => [ + 'expected_type' => 'number' + ], + 'content_length_less_than' => [ + 'expected_type' => 'number' + ], + 'content_length_less_than_or_equal_to' => [ + 'expected_type' => 'number' ] ] ], @@ -194,6 +266,24 @@ class FormPropertyLogicRule implements Rule, DataAwareRule 'type' => 'enum', 'values' => [true] ] + ], + 'content_length_equals' => [ + 'expected_type' => 'number' + ], + 'content_length_does_not_equal' => [ + 'expected_type' => 'number' + ], + 'content_length_greater_than' => [ + 'expected_type' => 'number' + ], + 'content_length_greater_than_or_equal_to' => [ + 'expected_type' => 'number' + ], + 'content_length_less_than' => [ + 'expected_type' => 'number' + ], + 'content_length_less_than_or_equal_to' => [ + 'expected_type' => 'number' ] ] ], diff --git a/app/Service/Forms/FormLogicConditionChecker.php b/app/Service/Forms/FormLogicConditionChecker.php index ba20104..d2edd7f 100644 --- a/app/Service/Forms/FormLogicConditionChecker.php +++ b/app/Service/Forms/FormLogicConditionChecker.php @@ -173,6 +173,25 @@ class FormLogicConditionChecker return ($fieldDate >= now()->toDateString() && $fieldDate <= now()->addYears(1)->toDateString()); } + private function checkLength ($condition, $fieldValue, $operator = '==='): bool { + if(!$fieldValue || strlen($fieldValue) === 0) 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 textConditionMet (array $propertyCondition, $value): bool { switch ($propertyCondition['operator']) { @@ -192,6 +211,18 @@ class FormLogicConditionChecker 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, '<='); } return false; } @@ -214,6 +245,18 @@ class FormLogicConditionChecker 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, '<='); } return false; } diff --git a/resources/data/open_filters.json b/resources/data/open_filters.json index 78c42ab..16291fb 100644 --- a/resources/data/open_filters.json +++ b/resources/data/open_filters.json @@ -36,6 +36,24 @@ true ] } + }, + "content_length_equals": { + "expected_type": "number" + }, + "content_length_does_not_equal": { + "expected_type": "number" + }, + "content_length_greater_than": { + "expected_type": "number" + }, + "content_length_greater_than_or_equal_to": { + "expected_type": "number" + }, + "content_length_less_than": { + "expected_type": "number" + }, + "content_length_less_than_or_equal_to": { + "expected_type": "number" } } }, @@ -76,6 +94,24 @@ true ] } + }, + "content_length_equals": { + "expected_type": "number" + }, + "content_length_does_not_equal": { + "expected_type": "number" + }, + "content_length_greater_than": { + "expected_type": "number" + }, + "content_length_greater_than_or_equal_to": { + "expected_type": "number" + }, + "content_length_less_than": { + "expected_type": "number" + }, + "content_length_less_than_or_equal_to": { + "expected_type": "number" } } }, @@ -116,6 +152,24 @@ true ] } + }, + "content_length_equals": { + "expected_type": "number" + }, + "content_length_does_not_equal": { + "expected_type": "number" + }, + "content_length_greater_than": { + "expected_type": "number" + }, + "content_length_greater_than_or_equal_to": { + "expected_type": "number" + }, + "content_length_less_than": { + "expected_type": "number" + }, + "content_length_less_than_or_equal_to": { + "expected_type": "number" } } }, @@ -156,6 +210,24 @@ true ] } + }, + "content_length_equals": { + "expected_type": "number" + }, + "content_length_does_not_equal": { + "expected_type": "number" + }, + "content_length_greater_than": { + "expected_type": "number" + }, + "content_length_greater_than_or_equal_to": { + "expected_type": "number" + }, + "content_length_less_than": { + "expected_type": "number" + }, + "content_length_less_than_or_equal_to": { + "expected_type": "number" } } }, @@ -196,6 +268,24 @@ true ] } + }, + "content_length_equals": { + "expected_type": "number" + }, + "content_length_does_not_equal": { + "expected_type": "number" + }, + "content_length_greater_than": { + "expected_type": "number" + }, + "content_length_greater_than_or_equal_to": { + "expected_type": "number" + }, + "content_length_less_than": { + "expected_type": "number" + }, + "content_length_less_than_or_equal_to": { + "expected_type": "number" } } }, diff --git a/resources/js/forms/FormLogicConditionChecker.js b/resources/js/forms/FormLogicConditionChecker.js index dadd2e5..bfce0fc 100644 --- a/resources/js/forms/FormLogicConditionChecker.js +++ b/resources/js/forms/FormLogicConditionChecker.js @@ -159,6 +159,25 @@ function checkNextYear (condition, fieldValue) { return (fieldDate >= today && fieldDate <= new Date(today.getFullYear() + 1, today.getMonth(), today.getDate())) } +function checkLength (condition, fieldValue, operator = '===') { + if(!fieldValue || fieldValue.length === 0) return false; + switch (operator) { + case '===': + return fieldValue.length === parseInt(condition.value) + case '!==': + return fieldValue.length !== parseInt(condition.value) + case '>': + return fieldValue.length > parseInt(condition.value) + case '>=': + return fieldValue.length >= parseInt(condition.value) + case '<': + return fieldValue.length < parseInt(condition.value) + case '<=': + return fieldValue.length <= parseInt(condition.value) + } + return false +} + function textConditionMet (propertyCondition, value) { switch (propertyCondition.operator) { case 'equals': @@ -177,6 +196,18 @@ function textConditionMet (propertyCondition, value) { return checkIsEmpty(propertyCondition, value) case 'is_not_empty': return !checkIsEmpty(propertyCondition, value) + case 'content_length_equals': + return checkLength(propertyCondition, value, '===') + case 'content_length_does_not_equal': + return checkLength(propertyCondition, value, '!==') + case 'content_length_greater_than': + return checkLength(propertyCondition, value, '>') + case 'content_length_greater_than_or_equal_to': + return checkLength(propertyCondition, value, '>=') + case 'content_length_less_than': + return checkLength(propertyCondition, value, '<') + case 'content_length_less_than_or_equal_to': + return checkLength(propertyCondition, value, '<=') } return false } @@ -198,7 +229,19 @@ function numberConditionMet (propertyCondition, value) { case 'is_empty': return checkIsEmpty(propertyCondition, value) case 'is_not_empty': - return !checkIsEmpty(propertyCondition, value) + return checkIsEmpty(propertyCondition, value) + case 'content_length_equals': + return checkLength(propertyCondition, value, '===') + case 'content_length_does_not_equal': + return checkLength(propertyCondition, value, '!==') + case 'content_length_greater_than': + return checkLength(propertyCondition, value, '>') + case 'content_length_greater_than_or_equal_to': + return checkLength(propertyCondition, value, '>=') + case 'content_length_less_than': + return checkLength(propertyCondition, value, '<') + case 'content_length_less_than_or_equal_to': + return checkLength(propertyCondition, value, '<=') } return false }