LsfFieldset::validationRules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Riclep\StoryblokForms\Blocks;
4
5
use Riclep\StoryblokForms\Traits\HasNames;
6
use Riclep\StoryblokForms\Traits\InFieldset;
7
use Riclep\StoryblokForms\Traits\ToJson;
8
9
class LsfFieldset extends \Riclep\Storyblok\Block
10
{
11
	use HasNames, InFieldset, ToJson;
0 ignored issues
show
introduced by
The trait Riclep\StoryblokForms\Traits\HasNames requires some properties which are not provided by Riclep\StoryblokForms\Blocks\LsfFieldset: $input_name, $key
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\LsfFieldset.
Loading history...
12
13
	protected string $type = 'fieldset';
14
15
	//// potentially all fields in a fieldset could be name <input name="fieldsetname[fieldname]">
16
	/// this would out a multidimensional array in the response.
17
	/// makes validation harder?
18
19
20
	/**
21
	 * Returns all the validation rules for the fields in this Fieldset
22
	 *
23
	 * @return array
24
	 */
25
	public function validationRules(): array
26
	{
27
		$rules = [];
28
29
		$this->fields->filter(function($field) {
0 ignored issues
show
Bug Best Practice introduced by
The property fields does not exist on Riclep\StoryblokForms\Blocks\LsfFieldset. 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

29
		$this->fields->/** @scrutinizer ignore-call */ 
30
                 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...
30
			return $field->component() !== 'lsf-text-note';
31
		})->each(function ($field) use (&$rules) {
32
			$rules = array_merge($rules, $field->validationRules());
33
		});
34
35
		return $rules;
36
	}
37
38
	/**
39
	 * Returns all the error messages for the fields in this Fieldset
40
	 *
41
	 * @return array
42
	 */
43
	public function errorMessages(): array
44
	{
45
		$rules = [];
46
47
		$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\LsfFieldset. Since you implemented __get, consider adding a @property annotation.
Loading history...
48
			$rules = array_merge($rules, $field->errorMessages());
49
		});
50
51
		return $rules;
52
	}
53
54
	/**
55
	 * Returns the Fieldset’s response after the form has been submitted and validated
56
	 *
57
	 * @param $input
58
	 * @return array
59
	 */
60
	public function response($input): array
61
	{
62
		return [
63
			'label' => $this->label,
0 ignored issues
show
Bug Best Practice introduced by
The property label does not exist on Riclep\StoryblokForms\Blocks\LsfFieldset. Since you implemented __get, consider adding a @property annotation.
Loading history...
64
			'name' => $this->name,
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Riclep\StoryblokForms\Blocks\LsfFieldset. Since you implemented __get, consider adding a @property annotation.
Loading history...
65
			'response' => $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\LsfFieldset. Since you implemented __get, consider adding a @property annotation.
Loading history...
66
67
				// Handle empty radio buttons etc. sending nothing in POST request
68
				// does allow empty $input break anything?
69
				if (!$input || !array_key_exists($field->name, $input)) {
70
					$input[$field->name] = null;
71
				}
72
73
				return $field->response($input[$field->name]);
74
			})->keyBy('name')->toArray(),
75
			'type' => $this->type,
76
		];
77
	}
78
}