Skip to content

Commit e073ca0

Browse files
Content Length Logic Condition (JhumanJ#63)
1 parent 9708933 commit e073ca0

File tree

4 files changed

+267
-1
lines changed

4 files changed

+267
-1
lines changed

app/Rules/FormPropertyLogicRule.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
5050
'type' => 'enum',
5151
'values' => [true]
5252
]
53+
],
54+
'content_length_equals' => [
55+
'expected_type' => 'number'
56+
],
57+
'content_length_does_not_equal' => [
58+
'expected_type' => 'number'
59+
],
60+
'content_length_greater_than' => [
61+
'expected_type' => 'number'
62+
],
63+
'content_length_greater_than_or_equal_to' => [
64+
'expected_type' => 'number'
65+
],
66+
'content_length_less_than' => [
67+
'expected_type' => 'number'
68+
],
69+
'content_length_less_than_or_equal_to' => [
70+
'expected_type' => 'number'
5371
]
5472
]
5573
],
@@ -86,6 +104,24 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
86104
'type' => 'enum',
87105
'values' => [true]
88106
]
107+
],
108+
'content_length_equals' => [
109+
'expected_type' => 'number'
110+
],
111+
'content_length_does_not_equal' => [
112+
'expected_type' => 'number'
113+
],
114+
'content_length_greater_than' => [
115+
'expected_type' => 'number'
116+
],
117+
'content_length_greater_than_or_equal_to' => [
118+
'expected_type' => 'number'
119+
],
120+
'content_length_less_than' => [
121+
'expected_type' => 'number'
122+
],
123+
'content_length_less_than_or_equal_to' => [
124+
'expected_type' => 'number'
89125
]
90126
]
91127
],
@@ -122,6 +158,24 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
122158
'type' => 'enum',
123159
'values' => [true]
124160
]
161+
],
162+
'content_length_equals' => [
163+
'expected_type' => 'number'
164+
],
165+
'content_length_does_not_equal' => [
166+
'expected_type' => 'number'
167+
],
168+
'content_length_greater_than' => [
169+
'expected_type' => 'number'
170+
],
171+
'content_length_greater_than_or_equal_to' => [
172+
'expected_type' => 'number'
173+
],
174+
'content_length_less_than' => [
175+
'expected_type' => 'number'
176+
],
177+
'content_length_less_than_or_equal_to' => [
178+
'expected_type' => 'number'
125179
]
126180
]
127181
],
@@ -158,6 +212,24 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
158212
'type' => 'enum',
159213
'values' => [true]
160214
]
215+
],
216+
'content_length_equals' => [
217+
'expected_type' => 'number'
218+
],
219+
'content_length_does_not_equal' => [
220+
'expected_type' => 'number'
221+
],
222+
'content_length_greater_than' => [
223+
'expected_type' => 'number'
224+
],
225+
'content_length_greater_than_or_equal_to' => [
226+
'expected_type' => 'number'
227+
],
228+
'content_length_less_than' => [
229+
'expected_type' => 'number'
230+
],
231+
'content_length_less_than_or_equal_to' => [
232+
'expected_type' => 'number'
161233
]
162234
]
163235
],
@@ -194,6 +266,24 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
194266
'type' => 'enum',
195267
'values' => [true]
196268
]
269+
],
270+
'content_length_equals' => [
271+
'expected_type' => 'number'
272+
],
273+
'content_length_does_not_equal' => [
274+
'expected_type' => 'number'
275+
],
276+
'content_length_greater_than' => [
277+
'expected_type' => 'number'
278+
],
279+
'content_length_greater_than_or_equal_to' => [
280+
'expected_type' => 'number'
281+
],
282+
'content_length_less_than' => [
283+
'expected_type' => 'number'
284+
],
285+
'content_length_less_than_or_equal_to' => [
286+
'expected_type' => 'number'
197287
]
198288
]
199289
],

app/Service/Forms/FormLogicConditionChecker.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,25 @@ private function checkNextYear ($condition, $fieldValue): bool {
173173
return ($fieldDate >= now()->toDateString() && $fieldDate <= now()->addYears(1)->toDateString());
174174
}
175175

176+
private function checkLength ($condition, $fieldValue, $operator = '==='): bool {
177+
if(!$fieldValue || strlen($fieldValue) === 0) return false;
178+
switch ($operator) {
179+
case '===':
180+
return strlen($fieldValue) === (int)$condition['value'];
181+
case '!==':
182+
return strlen($fieldValue) !== (int)$condition['value'];
183+
case '>':
184+
return strlen($fieldValue) > (int)$condition['value'];
185+
case '>=':
186+
return strlen($fieldValue) >= (int)$condition['value'];
187+
case '<':
188+
return strlen($fieldValue) < (int)$condition['value'];
189+
case '<=':
190+
return strlen($fieldValue) <= (int)$condition['value'];
191+
}
192+
return false;
193+
}
194+
176195

177196
private function textConditionMet (array $propertyCondition, $value): bool {
178197
switch ($propertyCondition['operator']) {
@@ -192,6 +211,18 @@ private function textConditionMet (array $propertyCondition, $value): bool {
192211
return $this->checkIsEmpty($propertyCondition, $value);
193212
case 'is_not_empty':
194213
return !$this->checkIsEmpty($propertyCondition, $value);
214+
case 'content_length_equals':
215+
return $this->checkLength($propertyCondition, $value, '===');
216+
case 'content_length_does_not_equal':
217+
return $this->checkLength($propertyCondition, $value, '!==');
218+
case 'content_length_greater_than':
219+
return $this->checkLength($propertyCondition, $value, '>');
220+
case 'content_length_greater_than_or_equal_to':
221+
return $this->checkLength($propertyCondition, $value, '>=');
222+
case 'content_length_less_than':
223+
return $this->checkLength($propertyCondition, $value, '<');
224+
case 'content_length_less_than_or_equal_to':
225+
return $this->checkLength($propertyCondition, $value, '<=');
195226
}
196227
return false;
197228
}
@@ -214,6 +245,18 @@ private function numberConditionMet (array $propertyCondition, $value): bool {
214245
return $this->checkIsEmpty($propertyCondition, $value);
215246
case 'is_not_empty':
216247
return !$this->checkIsEmpty($propertyCondition, $value);
248+
case 'content_length_equals':
249+
return $this->checkLength($propertyCondition, $value, '===');
250+
case 'content_length_does_not_equal':
251+
return $this->checkLength($propertyCondition, $value, '!==');
252+
case 'content_length_greater_than':
253+
return $this->checkLength($propertyCondition, $value, '>');
254+
case 'content_length_greater_than_or_equal_to':
255+
return $this->checkLength($propertyCondition, $value, '>=');
256+
case 'content_length_less_than':
257+
return $this->checkLength($propertyCondition, $value, '<');
258+
case 'content_length_less_than_or_equal_to':
259+
return $this->checkLength($propertyCondition, $value, '<=');
217260
}
218261
return false;
219262
}

resources/data/open_filters.json

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@
3636
true
3737
]
3838
}
39+
},
40+
"content_length_equals": {
41+
"expected_type": "number"
42+
},
43+
"content_length_does_not_equal": {
44+
"expected_type": "number"
45+
},
46+
"content_length_greater_than": {
47+
"expected_type": "number"
48+
},
49+
"content_length_greater_than_or_equal_to": {
50+
"expected_type": "number"
51+
},
52+
"content_length_less_than": {
53+
"expected_type": "number"
54+
},
55+
"content_length_less_than_or_equal_to": {
56+
"expected_type": "number"
3957
}
4058
}
4159
},
@@ -76,6 +94,24 @@
7694
true
7795
]
7896
}
97+
},
98+
"content_length_equals": {
99+
"expected_type": "number"
100+
},
101+
"content_length_does_not_equal": {
102+
"expected_type": "number"
103+
},
104+
"content_length_greater_than": {
105+
"expected_type": "number"
106+
},
107+
"content_length_greater_than_or_equal_to": {
108+
"expected_type": "number"
109+
},
110+
"content_length_less_than": {
111+
"expected_type": "number"
112+
},
113+
"content_length_less_than_or_equal_to": {
114+
"expected_type": "number"
79115
}
80116
}
81117
},
@@ -116,6 +152,24 @@
116152
true
117153
]
118154
}
155+
},
156+
"content_length_equals": {
157+
"expected_type": "number"
158+
},
159+
"content_length_does_not_equal": {
160+
"expected_type": "number"
161+
},
162+
"content_length_greater_than": {
163+
"expected_type": "number"
164+
},
165+
"content_length_greater_than_or_equal_to": {
166+
"expected_type": "number"
167+
},
168+
"content_length_less_than": {
169+
"expected_type": "number"
170+
},
171+
"content_length_less_than_or_equal_to": {
172+
"expected_type": "number"
119173
}
120174
}
121175
},
@@ -156,6 +210,24 @@
156210
true
157211
]
158212
}
213+
},
214+
"content_length_equals": {
215+
"expected_type": "number"
216+
},
217+
"content_length_does_not_equal": {
218+
"expected_type": "number"
219+
},
220+
"content_length_greater_than": {
221+
"expected_type": "number"
222+
},
223+
"content_length_greater_than_or_equal_to": {
224+
"expected_type": "number"
225+
},
226+
"content_length_less_than": {
227+
"expected_type": "number"
228+
},
229+
"content_length_less_than_or_equal_to": {
230+
"expected_type": "number"
159231
}
160232
}
161233
},
@@ -196,6 +268,24 @@
196268
true
197269
]
198270
}
271+
},
272+
"content_length_equals": {
273+
"expected_type": "number"
274+
},
275+
"content_length_does_not_equal": {
276+
"expected_type": "number"
277+
},
278+
"content_length_greater_than": {
279+
"expected_type": "number"
280+
},
281+
"content_length_greater_than_or_equal_to": {
282+
"expected_type": "number"
283+
},
284+
"content_length_less_than": {
285+
"expected_type": "number"
286+
},
287+
"content_length_less_than_or_equal_to": {
288+
"expected_type": "number"
199289
}
200290
}
201291
},

resources/js/forms/FormLogicConditionChecker.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,25 @@ function checkNextYear (condition, fieldValue) {
159159
return (fieldDate >= today && fieldDate <= new Date(today.getFullYear() + 1, today.getMonth(), today.getDate()))
160160
}
161161

162+
function checkLength (condition, fieldValue, operator = '===') {
163+
if(!fieldValue || fieldValue.length === 0) return false;
164+
switch (operator) {
165+
case '===':
166+
return fieldValue.length === parseInt(condition.value)
167+
case '!==':
168+
return fieldValue.length !== parseInt(condition.value)
169+
case '>':
170+
return fieldValue.length > parseInt(condition.value)
171+
case '>=':
172+
return fieldValue.length >= parseInt(condition.value)
173+
case '<':
174+
return fieldValue.length < parseInt(condition.value)
175+
case '<=':
176+
return fieldValue.length <= parseInt(condition.value)
177+
}
178+
return false
179+
}
180+
162181
function textConditionMet (propertyCondition, value) {
163182
switch (propertyCondition.operator) {
164183
case 'equals':
@@ -177,6 +196,18 @@ function textConditionMet (propertyCondition, value) {
177196
return checkIsEmpty(propertyCondition, value)
178197
case 'is_not_empty':
179198
return !checkIsEmpty(propertyCondition, value)
199+
case 'content_length_equals':
200+
return checkLength(propertyCondition, value, '===')
201+
case 'content_length_does_not_equal':
202+
return checkLength(propertyCondition, value, '!==')
203+
case 'content_length_greater_than':
204+
return checkLength(propertyCondition, value, '>')
205+
case 'content_length_greater_than_or_equal_to':
206+
return checkLength(propertyCondition, value, '>=')
207+
case 'content_length_less_than':
208+
return checkLength(propertyCondition, value, '<')
209+
case 'content_length_less_than_or_equal_to':
210+
return checkLength(propertyCondition, value, '<=')
180211
}
181212
return false
182213
}
@@ -198,7 +229,19 @@ function numberConditionMet (propertyCondition, value) {
198229
case 'is_empty':
199230
return checkIsEmpty(propertyCondition, value)
200231
case 'is_not_empty':
201-
return !checkIsEmpty(propertyCondition, value)
232+
return checkIsEmpty(propertyCondition, value)
233+
case 'content_length_equals':
234+
return checkLength(propertyCondition, value, '===')
235+
case 'content_length_does_not_equal':
236+
return checkLength(propertyCondition, value, '!==')
237+
case 'content_length_greater_than':
238+
return checkLength(propertyCondition, value, '>')
239+
case 'content_length_greater_than_or_equal_to':
240+
return checkLength(propertyCondition, value, '>=')
241+
case 'content_length_less_than':
242+
return checkLength(propertyCondition, value, '<')
243+
case 'content_length_less_than_or_equal_to':
244+
return checkLength(propertyCondition, value, '<=')
202245
}
203246
return false
204247
}

0 commit comments

Comments
 (0)