| 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 |
return false; |
| 77 |
} |
| 78 |
$inputValue = Arr::get($inputs, $accessor); |
| 79 |
|
| 80 |
if ($numericFormatter = Arr::get($conditional, 'numeric_formatter')) { |
| 81 |
$inputValue = Helper::getNumericValue($inputValue, $numericFormatter); |
| 82 |
} |
| 83 |
$conditionValue = Arr::get($conditional, 'value'); |
| 84 |
$isArrayAcceptable = in_array($conditional['operator'], ['=', '!=']); |
| 85 |
if (!empty($conditionValue) && is_string($conditionValue)) { |
| 86 |
$conditionValue = self::processSmartCodesInValue($conditionValue, $inputs, $form, $isArrayAcceptable); |
| 87 |
} |
| 88 |
|
| 89 |
$conditionValue = is_null($conditionValue) ? '' : $conditionValue; |
| 90 |
|
| 91 |
switch ($conditional['operator']) { |
| 92 |
case '=': |
| 93 |
if (is_array($inputValue) && is_array($conditionValue)) { |
| 94 |
$flatInput = Arr::flatten($inputValue); |
| 95 |
$flatCondition = Arr::flatten($conditionValue); |
| 96 |
sort($flatInput); |
| 97 |
sort($flatCondition); |
| 98 |
return $flatInput == $flatCondition; |
| 99 |
} |
| 100 |
|
| 101 |
if (is_array($conditionValue)) { |
| 102 |
return in_array($inputValue, Arr::flatten($conditionValue)); |
| 103 |
} |
| 104 |
if (is_array($inputValue)) { |
| 105 |
return in_array($conditionValue, Arr::flatten($inputValue)); |
| 106 |
} |
| 107 |
return $inputValue == $conditionValue; |
| 108 |
case '!=': |
| 109 |
if (is_array($inputValue) && is_array($conditionValue)) { |
| 110 |
return count(array_intersect(Arr::flatten($inputValue), Arr::flatten($conditionValue))) == 0; |
| 111 |
} |
| 112 |
if (is_array($conditionValue)) { |
| 113 |
return !in_array($inputValue, Arr::flatten($conditionValue)); |
| 114 |
} |
| 115 |
if (is_array($inputValue)) { |
| 116 |
return !in_array($conditionValue, Arr::flatten($inputValue)); |
| 117 |
} |
| 118 |
return $inputValue != $conditionValue; |
| 119 |
case '>': |
| 120 |
return $inputValue > $conditionValue; |
| 121 |
case '<': |
| 122 |
return $inputValue < $conditionValue; |
| 123 |
case '>=': |
| 124 |
return $inputValue >= $conditionValue; |
| 125 |
case '<=': |
| 126 |
return $inputValue <= $conditionValue; |
| 127 |
case 'startsWith': |
| 128 |
return Str::startsWith($inputValue, $conditionValue); |
| 129 |
case 'endsWith': |
| 130 |
return Str::endsWith($inputValue, $conditionValue); |
| 131 |
case 'contains': |
| 132 |
return Str::contains($inputValue, $conditionValue); |
| 133 |
case 'doNotContains': |
| 134 |
return !Str::contains($inputValue, $conditionValue); |
| 135 |
case 'length_equal': |
| 136 |
if (is_array($inputValue)) { |
| 137 |
return count($inputValue) == $conditionValue; |
| 138 |
} |
| 139 |
$inputValue = (string)$inputValue; |
| 140 |
return strlen($inputValue) == $conditionValue; |
| 141 |
case 'length_less_than': |
| 142 |
if (is_array($inputValue)) { |
| 143 |
return count($inputValue) < $conditionValue; |
| 144 |
} |
| 145 |
$inputValue = (string)$inputValue; |
| 146 |
return strlen($inputValue) < $conditionValue; |
| 147 |
case 'length_greater_than': |
| 148 |
if (is_array($inputValue)) { |
| 149 |
return count($inputValue) > $conditionValue; |
| 150 |
} |
| 151 |
$inputValue = (string)$inputValue; |
| 152 |
return strlen($inputValue) > $conditionValue; |
| 153 |
case 'test_regex': |
| 154 |
if (is_array($inputValue)) { |
| 155 |
$inputValue = implode(' ', $inputValue); |
| 156 |
} |
| 157 |
$pattern = '/' . $conditionValue . '/'; |
| 158 |
$result = @preg_match($pattern, $inputValue); |
| 159 |
if ($result === false) { |
| 160 |
// Invalid regex pattern, handle gracefully |
| 161 |
return false; |
| 162 |
} |
| 163 |
return (bool) $result; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
private static function processSmartCodesInValue($value, &$inputs, $form = null, $isArrayAcceptable = true) |
| 171 |
{ |
| 172 |
if (strpos($value, '{') === false) { |
| 173 |
return $value; |
| 174 |
} |
| 175 |
|
| 176 |
if (preg_match('/^{inputs\.([^}]+)}$/', $value, $inputMatches)) { |
| 177 |
$fieldName = $inputMatches[1]; |
| 178 |
$fieldKey = str_replace(['[', ']'], ['.', ''], $fieldName); |
| 179 |
|
| 180 |
$resolvedValue = Arr::get($inputs, $fieldKey); |
| 181 |
|
| 182 |
if ($resolvedValue === null && $fieldKey !== $fieldName) { |
| 183 |
$resolvedValue = Arr::get($inputs, $fieldName); |
| 184 |
} |
| 185 |
|
| 186 |
// Return array if it's an array |
| 187 |
if (is_array($resolvedValue) && $isArrayAcceptable) { |
| 188 |
return $resolvedValue; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
try { |
| 193 |
$processedValue = preg_replace_callback('/{+(.*?)}/', function ($matches) use ($inputs, $form) { |
| 194 |
$smartCode = $matches[1]; |
| 195 |
|
| 196 |
if (false !== strpos($smartCode, 'inputs.')) { |
| 197 |
$fieldName = substr($smartCode, strlen('inputs.')); |
| 198 |
|
| 199 |
$fieldKey = str_replace(['[', ']'], ['.', ''], $fieldName); |
| 200 |
|
| 201 |
$value = Arr::get($inputs, $fieldKey, ''); |
| 202 |
|
| 203 |
if ($value === '' && $fieldKey !== $fieldName) { |
| 204 |
$value = Arr::get($inputs, $fieldName, ''); |
| 205 |
} |
| 206 |
|
| 207 |
if (is_array($value)) { |
| 208 |
$value = fluentImplodeRecursive(', ', $value); |
| 209 |
} |
| 210 |
|
| 211 |
return $value !== null && $value !== '' ? $value : ''; |
| 212 |
} |
| 213 |
|
| 214 |
// @todo Support general shortcodes in future |
| 215 |
|
| 216 |
// Always return the original value if we don't have a match |
| 217 |
return $matches[0]; |
| 218 |
}, $value); |
| 219 |
|
| 220 |
return $processedValue; |
| 221 |
} catch (\Exception $e) { |
| 222 |
return $value; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|