| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services; |
| 4 |
|
| 5 |
use FluentForm\App\Helpers\Str; |
| 6 |
use FluentForm\Framework\Helpers\ArrayHelper as Arr; |
| 7 |
|
| 8 |
class ConditionAssesor |
| 9 |
{ |
| 10 |
public static function evaluate(&$field, &$inputs) |
| 11 |
{ |
| 12 |
$status = Arr::get($field, 'conditionals.status'); |
| 13 |
|
| 14 |
$conditionals = $status ? Arr::get($field, 'conditionals.conditions') : false; |
| 15 |
|
| 16 |
|
| 17 |
$hasConditionMet = true; |
| 18 |
|
| 19 |
if ($conditionals) { |
| 20 |
$toMatch = Arr::get($field, 'conditionals.type'); |
| 21 |
|
| 22 |
foreach ($conditionals as $conditional) { |
| 23 |
|
| 24 |
$hasConditionMet = static::assess($conditional, $inputs); |
| 25 |
|
| 26 |
if($hasConditionMet && $toMatch == 'any') { |
| 27 |
return true; |
| 28 |
} |
| 29 |
|
| 30 |
if ($toMatch === 'all' && !$hasConditionMet) { |
| 31 |
return false; |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
return $hasConditionMet; |
| 37 |
} |
| 38 |
|
| 39 |
public static function assess(&$conditional, &$inputs) |
| 40 |
{ |
| 41 |
if ($conditional['field']) { |
| 42 |
$inputValue = Arr::get($inputs, $conditional['field']); |
| 43 |
|
| 44 |
switch ($conditional['operator']) { |
| 45 |
case '=': |
| 46 |
if(is_array($inputValue)) { |
| 47 |
return in_array($conditional['value'], $inputValue); |
| 48 |
} |
| 49 |
return $inputValue === $conditional['value']; |
| 50 |
break; |
| 51 |
case '!=': |
| 52 |
if(is_array($inputValue)) { |
| 53 |
return !in_array($conditional['value'], $inputValue); |
| 54 |
} |
| 55 |
return $inputValue !== $conditional['value']; |
| 56 |
break; |
| 57 |
case '>': |
| 58 |
return $inputValue > $conditional['value']; |
| 59 |
break; |
| 60 |
case '<': |
| 61 |
return $inputValue < $conditional['value']; |
| 62 |
break; |
| 63 |
case '>=': |
| 64 |
return $inputValue >= $conditional['value']; |
| 65 |
break; |
| 66 |
case '<=': |
| 67 |
return $inputValue <= $conditional['value']; |
| 68 |
break; |
| 69 |
case 'startsWith': |
| 70 |
return Str::startsWith($inputValue, $conditional['value']); |
| 71 |
break; |
| 72 |
case 'endsWith': |
| 73 |
return Str::endsWith($inputValue, $conditional['value']); |
| 74 |
break; |
| 75 |
case 'contains': |
| 76 |
return Str::contains($inputValue, $conditional['value']); |
| 77 |
break; |
| 78 |
case 'doNotContains': |
| 79 |
return !Str::contains($inputValue, $conditional['value']); |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return false; |
| 85 |
} |
| 86 |
} |
| 87 |
|