Passed
Push — develop ( bd344d...2cd0ec )
by Richard
03:46
created

Validators   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 37
c 5
b 0
f 0
dl 0
loc 124
rs 10
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A validationRules() 0 12 2
A offsetGet() 0 3 1
A errorMessages() 0 12 2
A nameToValidationKey() 0 13 1
A offsetSet() 0 6 2
A process() 0 3 1
A offsetUnset() 0 3 1
A offsetExists() 0 3 1
A __construct() 0 5 1
1
<?php
2
3
namespace Riclep\StoryblokForms;
4
5
use ArrayAccess;
6
use Illuminate\Support\Arr;
7
8
class Validators implements ArrayAccess
9
{
10
	/**
11
	 * @var
12
	 */
13
	public $rules;
14
15
	/**
16
	 * @var
17
	 */
18
	protected $field;
19
20
	/**
21
	 * @param $validators
22
	 * @param $field
23
	 */
24
	public function __construct($validators, $field)
25
	{
26
		$this->process($validators);
27
28
		$this->field = $field;
29
	}
30
31
	/**
32
	 * @return array
33
	 */
34
	public function validationRules() {
35
		$rules = [];
36
37
		$hasRules = array_values($this->rules->map(function ($rule) {
38
			return $rule->rule();
39
		})->toArray());
40
41
		if ($hasRules) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $hasRules of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
42
			$rules[$this->nameToValidationKey()] = $hasRules;
43
		}
44
45
		return $rules;
46
	}
47
48
	/**
49
	 * @return array
50
	 */
51
	public function errorMessages() {
52
		$messages = [];
53
54
		$this->rules->each(function ($rule) use (&$messages) {
55
			if ($rule->errorMessage()) {
56
				$messageKey = $this->nameToValidationKey() . '.' . $rule->rule();
0 ignored issues
show
Bug introduced by
Are you sure $this->nameToValidationKey() of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

56
				$messageKey = /** @scrutinizer ignore-type */ $this->nameToValidationKey() . '.' . $rule->rule();
Loading history...
57
58
				$messages = array_merge($messages, [$messageKey => $rule->errorMessage()]);
59
			}
60
		})->toArray();
61
62
		return $messages;
63
	}
64
65
	/**
66
	 * @return array|string|string[]
67
	 */
68
	protected function nameToValidationKey(): string|array
69
	{
70
		$validationKey = str_replace([
71
			'[]',
72
			'[',
73
			']'
74
		], [
75
			'.*',
76
			'.',
77
			''
78
		], $this->field->name);
79
80
		return $validationKey;
81
	}
82
83
	/**
84
	 * @param $validators
85
	 * @return void
86
	 */
87
	protected function process($validators) {
88
		$this->rules = collect($validators)->transform(function ($validator) {
89
			return (new Validator(array_diff_key($validator, array_flip(['_editable', '_uid']))));
90
		});
91
	}
92
93
	/**
94
	 * @param $offset
95
	 * @return bool
96
	 */
97
	public function offsetExists($offset)
98
	{
99
		return isset($this->rules[$offset]);
100
	}
101
102
	/**
103
	 * @param $offset
104
	 * @return mixed|null
105
	 */
106
	public function offsetGet($offset)
107
	{
108
		return $this->rules[$offset] ?? null;
109
	}
110
111
	/**
112
	 * @param $offset
113
	 * @param $value
114
	 * @return void
115
	 */
116
	public function offsetSet($offset, $value)
117
	{
118
		if (is_null($offset)) {
119
			$this->rules[] = $value;
120
		} else {
121
			$this->rules[$offset] = $value;
122
		}
123
	}
124
125
	/**
126
	 * @param $offset
127
	 * @return void
128
	 */
129
	public function offsetUnset($offset)
130
	{
131
		unset($this->rules[$offset]);
132
	}
133
}