PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.3
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.3
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Services / ConditionAssesor.php

ConditionAssesor.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.3, at app/Services/ConditionAssesor.php

230 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Services;
4
5 use FluentForm\App\Helpers\Helper;
6 use FluentForm\App\Helpers\Str;
7 use FluentForm\App\Services\FormBuilder\EditorShortcodeParser;
8 use FluentForm\Framework\Helpers\ArrayHelper as Arr;
9
10 class ConditionAssesor
11 {
12 public static function evaluate(&$field, &$inputs, $form = null)
13 {
14 $status = Arr::get($field, 'conditionals.status');
15 if (!$status) {
16 return true;
17 }
18
19 $type = Arr::get($field, 'conditionals.type', 'any');
20
21 // Handle group conditions
22 if ($type === 'group' && $conditionGroups = Arr::get($field, 'conditionals.condition_groups')) {
23 return self::evaluateGroupConditions($conditionGroups, $inputs, $form);
24 }
25
26 // Handle 'any', 'all' conditions
27 if ($type !== 'group' && $conditions = Arr::get($field, 'conditionals.conditions')) {
28 return self::evaluateConditions($conditions, $inputs, $type, $form);
29 }
30 return true;
31 }
32
33 private static function evaluateGroupConditions($conditionGroups, &$inputs, $form = null)
34 {
35 $hasGroupConditionsMet = true;
36 foreach ($conditionGroups as $group) {
37 if ($conditions = Arr::get($group, 'rules')) {
38 $hasGroupConditionsMet = self::evaluateConditions($conditions, $inputs, 'all', $form);
39 if ($hasGroupConditionsMet) {
40 return true;
41 }
42 }
43 }
44 return $hasGroupConditionsMet;
45 }
46
47 private static function evaluateConditions($conditions, &$inputs, $type, $form = null)
48 {
49 $hasConditionMet = true;
50
51 foreach ($conditions as $condition) {
52 if (!Arr::get($condition, 'field') || !Arr::get($condition, 'operator')) {
53 continue;
54 }
55
56 $hasConditionMet = static::assess($condition, $inputs, $form);
57
58 if ($hasConditionMet && $type == 'any') {
59 return true;
60 }
61
62 if ($type === 'all' && !$hasConditionMet) {
63 return false;
64 }
65 }
66
67 return $hasConditionMet;
68 }
69
70 public static function assess(&$conditional, &$inputs, $form = null)
71 {
72 if ($conditional['field']) {
73 $accessor = rtrim(str_replace(['[', ']', '*'], ['.'], $conditional['field']), '.');
74
75 if (!Arr::has($inputs, $accessor)) {
76 // A missing field is "not equal" to any value, matching JS behavior
77 if ($conditional['operator'] === '!=') {
78 return true;
79 }
80 return false;
81 }
82 $inputValue = Arr::get($inputs, $accessor);
83
84 if ($numericFormatter = Arr::get($conditional, 'numeric_formatter')) {
85 $inputValue = Helper::getNumericValue($inputValue, $numericFormatter);
86 }
87 $conditionValue = Arr::get($conditional, 'value');
88 $isArrayAcceptable = in_array($conditional['operator'], ['=', '!=']);
89 if (!empty($conditionValue) && is_string($conditionValue)) {
90 $conditionValue = self::processSmartCodesInValue($conditionValue, $inputs, $form, $isArrayAcceptable);
91 }
92
93 $conditionValue = is_null($conditionValue) ? '' : $conditionValue;
94
95 switch ($conditional['operator']) {
96 case '=':
97 if (is_array($inputValue) && is_array($conditionValue)) {
98 $flatInput = Arr::flatten($inputValue);
99 $flatCondition = Arr::flatten($conditionValue);
100 sort($flatInput);
101 sort($flatCondition);
102 return $flatInput == $flatCondition;
103 }
104
105 if (is_array($conditionValue)) {
106 return in_array($inputValue, Arr::flatten($conditionValue));
107 }
108 if (is_array($inputValue)) {
109 return in_array($conditionValue, Arr::flatten($inputValue));
110 }
111 return $inputValue == $conditionValue;
112 case '!=':
113 if (is_array($inputValue) && is_array($conditionValue)) {
114 return count(array_intersect(Arr::flatten($inputValue), Arr::flatten($conditionValue))) == 0;
115 }
116 if (is_array($conditionValue)) {
117 return !in_array($inputValue, Arr::flatten($conditionValue));
118 }
119 if (is_array($inputValue)) {
120 return !in_array($conditionValue, Arr::flatten($inputValue));
121 }
122 return $inputValue != $conditionValue;
123 case '>':
124 return $inputValue > $conditionValue;
125 case '<':
126 return $inputValue < $conditionValue;
127 case '>=':
128 return $inputValue >= $conditionValue;
129 case '<=':
130 return $inputValue <= $conditionValue;
131 case 'startsWith':
132 return Str::startsWith($inputValue, $conditionValue);
133 case 'endsWith':
134 return Str::endsWith($inputValue, $conditionValue);
135 case 'contains':
136 return Str::contains($inputValue, $conditionValue);
137 case 'doNotContains':
138 return !Str::contains($inputValue, $conditionValue);
139 case 'length_equal':
140 if (is_array($inputValue)) {
141 return count($inputValue) == $conditionValue;
142 }
143 $inputValue = (string)$inputValue;
144 return strlen($inputValue) == $conditionValue;
145 case 'length_less_than':
146 if (is_array($inputValue)) {
147 return count($inputValue) < $conditionValue;
148 }
149 $inputValue = (string)$inputValue;
150 return strlen($inputValue) < $conditionValue;
151 case 'length_greater_than':
152 if (is_array($inputValue)) {
153 return count($inputValue) > $conditionValue;
154 }
155 $inputValue = (string)$inputValue;
156 return strlen($inputValue) > $conditionValue;
157 case 'test_regex':
158 if (is_array($inputValue)) {
159 $inputValue = implode(' ', $inputValue);
160 }
161 $pattern = '/' . $conditionValue . '/';
162 $result = @preg_match($pattern, $inputValue);
163 if ($result === false) {
164 // Invalid regex pattern, handle gracefully
165 return false;
166 }
167 return (bool) $result;
168 }
169 }
170
171 return false;
172 }
173
174 private static function processSmartCodesInValue($value, &$inputs, $form = null, $isArrayAcceptable = true)
175 {
176 if (strpos($value, '{') === false) {
177 return $value;
178 }
179
180 if (preg_match('/^{inputs\.([^}]+)}$/', $value, $inputMatches)) {
181 $fieldName = $inputMatches[1];
182 $fieldKey = str_replace(['[', ']'], ['.', ''], $fieldName);
183
184 $resolvedValue = Arr::get($inputs, $fieldKey);
185
186 if ($resolvedValue === null && $fieldKey !== $fieldName) {
187 $resolvedValue = Arr::get($inputs, $fieldName);
188 }
189
190 // Return array if it's an array
191 if (is_array($resolvedValue) && $isArrayAcceptable) {
192 return $resolvedValue;
193 }
194 }
195
196 try {
197 $processedValue = preg_replace_callback('/{+(.*?)}/', function ($matches) use ($inputs, $form) {
198 $smartCode = $matches[1];
199
200 if (false !== strpos($smartCode, 'inputs.')) {
201 $fieldName = substr($smartCode, strlen('inputs.'));
202
203 $fieldKey = str_replace(['[', ']'], ['.', ''], $fieldName);
204
205 $value = Arr::get($inputs, $fieldKey, '');
206
207 if ($value === '' && $fieldKey !== $fieldName) {
208 $value = Arr::get($inputs, $fieldName, '');
209 }
210
211 if (is_array($value)) {
212 $value = fluentImplodeRecursive(', ', $value);
213 }
214
215 return $value !== null && $value !== '' ? $value : '';
216 }
217
218 // @todo Support general shortcodes in future
219
220 // Always return the original value if we don't have a match
221 return $matches[0];
222 }, $value);
223
224 return $processedValue;
225 } catch (\Exception $e) {
226 return $value;
227 }
228 }
229 }
230