ConditionallyRequired   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 11
Bugs 0 Features 0
Metric Value
wmc 18
eloc 35
c 11
b 0
f 0
dl 0
loc 88
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
C __invoke() 0 41 15
A __toString() 0 3 1
A setData() 0 5 1
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
0 ignored issues
show
Deprecated Code introduced by
The interface Illuminate\Contracts\Validation\InvokableRule has been deprecated: see ValidationRule ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

9
class ConditionallyRequired implements DataAwareRule, /** @scrutinizer ignore-deprecated */ InvokableRule

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.

Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
95
96
		return $this;
97
	}
98
}
99