1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Riclep\StoryblokForms\Rules; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Validation\DataAwareRule; |
6
|
|
|
use Illuminate\Contracts\Validation\InvokableRule; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
class ConditionallyRequired implements DataAwareRule, InvokableRule |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The condition we want to check the validity of. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected Collection $conditional; |
18
|
|
|
|
19
|
|
|
public function __construct($conditional) { |
20
|
|
|
$this->conditional = $conditional; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Run the validation rule. |
25
|
|
|
* |
26
|
|
|
* @param string $attribute |
27
|
|
|
* @param mixed $value |
28
|
|
|
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function __invoke($attribute, $value, $fail): void |
32
|
|
|
{ |
33
|
|
|
// $causationFieldValue = data_get($this->data, $this->conditional['field'])[0]; |
34
|
|
|
$causationFieldValue = data_get($this->data, $this->conditional['field']); |
35
|
|
|
|
36
|
|
|
if (is_array($causationFieldValue)) { |
37
|
|
|
$causationFieldValue = $causationFieldValue[0]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// $causationFieldValue is an array? always? |
41
|
|
|
|
42
|
|
|
$condition = $this->conditional['condition']; |
43
|
|
|
$operator = $this->conditional['operator']; |
44
|
|
|
|
45
|
|
|
$fieldIsRequired = false; |
46
|
|
|
|
47
|
|
|
if (!is_null($causationFieldValue)) { |
48
|
|
|
if ($operator === '>') { |
49
|
|
|
$fieldIsRequired = $causationFieldValue > $condition; |
50
|
|
|
} else if ($operator === '<') { |
51
|
|
|
$fieldIsRequired = $causationFieldValue < $condition; |
52
|
|
|
} else if ($operator === '==') { |
53
|
|
|
$fieldIsRequired = $causationFieldValue == $condition; |
54
|
|
|
} else if ($operator === '===') { |
55
|
|
|
$fieldIsRequired = $causationFieldValue === $condition; |
56
|
|
|
} else if ($operator === '!=') { |
57
|
|
|
$fieldIsRequired = $causationFieldValue != $condition; |
58
|
|
|
} else if ($operator === '!==') { |
59
|
|
|
$fieldIsRequired = $causationFieldValue !== $condition; |
60
|
|
|
} else if ($operator === '>=') { |
61
|
|
|
$fieldIsRequired = $causationFieldValue >= $condition; |
62
|
|
|
} else if ($operator === '<=') { |
63
|
|
|
$fieldIsRequired = $causationFieldValue <= $condition; |
64
|
|
|
} else if ($operator === 'one_of') { |
65
|
|
|
$fieldIsRequired = in_array((int) $causationFieldValue, $condition); // always array? |
66
|
|
|
} else if ($operator === 'not_one_of') { |
67
|
|
|
$fieldIsRequired = !in_array((int) $causationFieldValue, $condition); // always array? |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($fieldIsRequired && !$value) { |
71
|
|
|
$fail('This field is required'); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Outputs the HTML version of the validation |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function __toString() |
82
|
|
|
{ |
83
|
|
|
return ''; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set the data under validation. |
88
|
|
|
* |
89
|
|
|
* @param array $data |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
|
|
public function setData($data) |
93
|
|
|
{ |
94
|
|
|
$this->data = $data; |
|
|
|
|
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This interface has been deprecated. The supplier of the interface has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.