Passed
Push — develop ( e45dc6...39f27f )
by Richard
14:40 queued 18s
created

LsfConditionalSelect   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 13
eloc 56
c 7
b 0
f 0
dl 0
loc 130
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validationRules() 0 22 2
A response() 0 32 5
B errorMessages() 0 43 6
1
<?php
2
3
namespace Riclep\StoryblokForms\Blocks;
4
5
use Illuminate\Support\Str;
6
use Riclep\StoryblokForms\MultiInput;
7
use Riclep\StoryblokForms\Traits\HasNames;
8
use Riclep\StoryblokForms\Traits\InFieldset;
9
use Riclep\StoryblokForms\Traits\ToJson;
10
11
class LsfConditionalSelect extends MultiInput
12
{
13
	use HasNames, InFieldset, ToJson;
0 ignored issues
show
Bug introduced by
The trait Riclep\StoryblokForms\Traits\HasNames requires the property $input_name which is not provided by Riclep\StoryblokForms\Blocks\LsfConditionalSelect.
Loading history...
Bug introduced by
The trait Riclep\StoryblokForms\Traits\ToJson requires the property $input_json_dot_name which is not provided by Riclep\StoryblokForms\Blocks\LsfConditionalSelect.
Loading history...
14
15
	/**
16
	 * @var string
17
	 */
18
	protected string $optionsName = 'options';
19
20
	protected string $type = 'conditional-select';
21
22
	/**
23
	 * Returns all the validation rules for the field
24
	 *
25
	 * @return array
26
	 */
27
	public function validationRules(): array
28
	{
29
		// TODO - loop over all children looking for required validation and update to required_if
30
31
		$rules = [];
32
33
		$this->fields->filter(function($field) {
0 ignored issues
show
Bug Best Practice introduced by
The property fields does not exist on Riclep\StoryblokForms\Blocks\LsfConditionalSelect. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method filter() does not exist on null. ( Ignorable by Annotation )

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

33
		$this->fields->/** @scrutinizer ignore-call */ 
34
                 filter(function($field) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
			return $field->component() !== 'lsf-text-note';
35
		})->each(function ($field) use (&$rules) {
36
			$rules = array_merge($rules, $field->validationRules());
37
		});
38
39
		$fieldRules = parent::validationRules();
40
41
		// Should the Dot name always do this? Probably not as that would break children?
42
		$selectKey = $this->getInputDotNameAttribute() . '.selected';  //- is that needed in some places?
43
44
		if (array_key_exists($this->getInputDotNameAttribute(), $fieldRules)) {
45
			return array_merge($rules, [$selectKey => $fieldRules[$this->getInputDotNameAttribute()]]);
46
		}
47
48
		return $rules;
49
	}
50
51
	/**
52
	 * Returns all the error messages for the field
53
	 * @return array
54
	 */
55
	public function errorMessages(): array
56
	{
57
		$rules = [];
58
		$selectMessage = [];
59
60
		$this->fields->each(function ($field) use (&$rules) {
0 ignored issues
show
Bug Best Practice introduced by
The property fields does not exist on Riclep\StoryblokForms\Blocks\LsfConditionalSelect. Since you implemented __get, consider adding a @property annotation.
Loading history...
61
			$rules = array_merge($rules, $field->errorMessages());
62
		});
63
64
		$messages = $this->validators->errorMessages();
0 ignored issues
show
Bug Best Practice introduced by
The property validators does not exist on Riclep\StoryblokForms\Blocks\LsfConditionalSelect. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method errorMessages() does not exist on null. ( Ignorable by Annotation )

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

64
		/** @scrutinizer ignore-call */ 
65
  $messages = $this->validators->errorMessages();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
66
		if ($this->parent() instanceof LsfConditionalSelect) {
67
			foreach ($messages as $key => $rule) {
68
				if (Str::endsWith($key, 'required')) {
69
					$messages[$key . '_if'] = $rule;
70
71
					unset($messages[$key]);
72
				}
73
			}
74
75
			$selectKey = $this->getInputDotNameAttribute()  . '.selected.required_if';
76
			$selectMessage = [$selectKey => $messages[$this->getInputDotNameAttribute() . '.required_if']];
77
		} else {
78
			if (array_key_exists($this->getInputDotNameAttribute() . '.selected.required', $this->validationRules())) {
79
				$selectKey = $this->getInputDotNameAttribute()  . '.selected.required';
80
				$selectMessage = [
81
					$selectKey => $messages[
82
						$this->getInputDotNameAttribute() . '.required'
83
					]
84
				];
85
			}
86
87
			if (array_key_exists($this->getInputDotNameAttribute() . '.selected', $this->validationRules())) {
88
				$selectKey = $this->getInputDotNameAttribute()  . '.selected';
89
				$selectMessage = [
90
					$selectKey => $messages[
91
						$this->getInputDotNameAttribute() . '.required'
92
					]
93
				];
94
			}
95
		}
96
97
		return array_merge($rules, $selectMessage);
98
	}
99
100
101
	/**
102
	 * Returns the Input’s response after the form has been submitted and validated
103
	 * All options are returned as an array with their name and a selected boolean
104
	 * based on the user’s input
105
	 *
106
	 * @param $input
107
	 * @return array
108
	 */
109
	public function response($input): array
110
	{
111
		$formatted = [
112
			'label' => $this->label,
0 ignored issues
show
Bug Best Practice introduced by
The property label does not exist on Riclep\StoryblokForms\Blocks\LsfConditionalSelect. Since you implemented __get, consider adding a @property annotation.
Loading history...
113
			'name' => $this->name,
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Riclep\StoryblokForms\Blocks\LsfConditionalSelect. Since you implemented __get, consider adding a @property annotation.
Loading history...
114
			'response' => ['select' => ['selected' => [], 'unselected' => []]],
115
			'type' => $this->type,
116
		];
117
118
		$this->options()->map(function ($formInput) use ($input, &$formatted) {
119
			if ($input && $formInput['value'] === $input['selected']) {
120
				return $formatted['response']['select']['selected'][$formInput['value']] = $formInput['label'];
121
			}
122
123
			return $formatted['response']['select']['unselected'][$formInput['value']] = $formInput['label'];
124
		})->toArray();
125
126
		$formatted['response']['fields'] = $this->fields->map(function ($field) use ($input) {
0 ignored issues
show
Bug Best Practice introduced by
The property fields does not exist on Riclep\StoryblokForms\Blocks\LsfConditionalSelect. Since you implemented __get, consider adding a @property annotation.
Loading history...
127
			if (!$input) {
128
				return $field->response($input);
129
			}
130
131
			///// no required child fields?
132
			// Handle empty radio buttons etc. sending nothing in POST request
133
			if (!array_key_exists($field->name, $input)) {
134
				$input[$field->name] = null;
135
			}
136
137
			return $field->response($input[$field->name]);
138
		})->keyBy('name')->toArray();
139
140
		return $formatted;
141
	}
142
}