Passed
Push — develop ( 529aa7...f56b73 )
by Richard
02:56
created

LsfFieldset::response()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 17
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Riclep\StoryblokForms\Blocks;
4
5
class LsfFieldset extends \Riclep\Storyblok\Block
6
{
7
	//// potentially all fields in a fieldset could be name <input name="fieldsetname[fieldname]">
8
	/// this would out a multidimensional array in the response.
9
	/// makes validation herder?
10
11
	protected $inFieldSet = false;
12
	protected $isRepeating = false;
13
14
	public function __construct($content, $parent = null)
15
	{
16
		parent::__construct($content, $parent);
17
18
		if ($this->parent() instanceof LsfFieldset) {
19
			$this->inFieldSet = true;
20
		}
21
22
		if ($this->parent() instanceof LsfRepeatingFieldset) {
23
			$this->isRepeating = true;
24
		}
25
	}
26
27
	public function getInputNameAttribute() {
28
		if ($this->isRepeating) {
29
			return $this->parent()->input_name . '[' . $this->key . '][' . $this->content()['name'] . ']';
0 ignored issues
show
Bug Best Practice introduced by
The property key does not exist on Riclep\StoryblokForms\Blocks\LsfFieldset. Since you implemented __get, consider adding a @property annotation.
Loading history...
30
		}
31
32
		if ($this->inFieldSet) {
33
			return $this->parent()->input_name . '[' . $this->content()['name'] . ']';
34
		}
35
36
		return $this->content()['name'];
37
	}
38
39
	public function response($input) {
40
		//dd($input, $this);
41
42
		return $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...
Bug introduced by
The method map() 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

42
		return $this->fields->/** @scrutinizer ignore-call */ map(function ($field) use ($input) {

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...
43
44
//			dump($field, $input);
45
// TODO - handle radio buttons being empty
46
			dump($field, $input, $field->name);
47
48
			return $field->response($input[$field->name]);
49
			/*dd($field);
50
51
			return [
52
				'label' => $field->label,
53
				'response' => $field->response($input[$field->name] ?? ''),
54
			];*/
55
		})->toArray();
56
	}
57
58
59
	/**
60
	 * Returns all the validation rules for the fields in this Fieldset
61
	 *
62
	 * @return array
63
	 */
64
	public function validationRules() {
65
		$rules = [];
66
67
		$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...
68
			$rules = array_merge($rules, $field->validationRules());
69
		});
70
71
		return $rules;
72
	}
73
74
	/**
75
	 * Returns all the error messages for the fields in this Fieldset
76
	 *
77
	 * @return array
78
	 */
79
	public function errorMessages() {
80
		$rules = [];
81
82
		$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...
83
			$rules = array_merge($rules, $field->errorMessages());
84
		});
85
86
		return $rules;
87
	}
88
}