PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / trunk
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution vtrunk
2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 1.7.2 All 33 releases
fluent-booking / app / Services / ConditionAssesor.php

ConditionAssesor.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution trunk, at app/Services/ConditionAssesor.php

117 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBooking\App\Services;
4
5 use FluentBooking\Framework\Support\Arr;
6 use FluentBooking\Framework\Support\Str;
7
8 class ConditionAssesor
9 {
10 public static function evaluate(&$field, &$inputs)
11 {
12 $status = Arr::get($field, 'conditionals.status');
13
14
15 $conditionals = $status ? Arr::get($field, 'conditionals.conditions') : false;
16
17
18 $hasConditionMet = true;
19
20 if ($conditionals) {
21 $toMatch = Arr::get($field, 'conditionals.type');
22
23
24 foreach ($conditionals as $conditional) {
25
26 $hasConditionMet = static::assess($conditional, $inputs);
27
28 if($hasConditionMet && $toMatch == 'any') {
29 return true;
30 }
31
32 if ($toMatch === 'all' && !$hasConditionMet) {
33 return false;
34 }
35 }
36 }
37
38 return $hasConditionMet;
39 }
40
41 public static function assess(&$conditional, &$inputs)
42 {
43 if ($conditional['field']) {
44 $inputValue = Arr::get($inputs, $conditional['field'], '');
45 $conditionValue = Arr::get($conditional, 'value', '');
46 $stringInputValue = is_scalar($inputValue) ? (string) $inputValue : '';
47 $stringConditionValue = is_scalar($conditionValue) ? (string) $conditionValue : '';
48
49 switch ($conditional['operator']) {
50 case '=':
51 if(is_array($inputValue)) {
52 return in_array($conditionValue, $inputValue);
53 }
54 return $inputValue == $conditionValue;
55 break;
56 case '!=':
57 if(is_array($inputValue)) {
58 return !in_array($conditionValue, $inputValue);
59 }
60 return $inputValue != $conditionValue;
61 break;
62 case '>':
63 return $inputValue > $conditionValue;
64 break;
65 case '<':
66 return $inputValue < $conditionValue;
67 break;
68 case '>=':
69 return $inputValue >= $conditionValue;
70 break;
71 case '<=':
72 return $inputValue <= $conditionValue;
73 break;
74 case 'startsWith':
75 return Str::startsWith($stringInputValue, $stringConditionValue);
76 break;
77 case 'endsWith':
78 return Str::endsWith($stringInputValue, $stringConditionValue);
79 break;
80 case 'contains':
81 return Str::contains($stringInputValue, $stringConditionValue);
82 break;
83 case 'doNotContains':
84 return !Str::contains($stringInputValue, $stringConditionValue);
85 break;
86 case 'length_equal':
87 if(is_array($inputValue)) {
88 return count($inputValue) == $conditionValue;
89 }
90 return strlen($stringInputValue) == $conditionValue;
91 break;
92 case 'length_less_than':
93 if(is_array($inputValue)) {
94 return count($inputValue) < $conditionValue;
95 }
96 return strlen($stringInputValue) < $conditionValue;
97 break;
98 case 'length_greater_than':
99 if(is_array($inputValue)) {
100 return count($inputValue) > $conditionValue;
101 }
102 return strlen($stringInputValue) > $conditionValue;
103 break;
104 case 'test_regex':
105 if(is_array($inputValue)) {
106 $stringInputValue = implode(' ', $inputValue);
107 }
108 $result = preg_match('/'.$stringConditionValue.'/', $stringInputValue);
109 return !!$result;
110 break;
111 }
112 }
113
114 return false;
115 }
116 }
117