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

Input::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;
4
5
use Riclep\StoryblokForms\Blocks\LsfFieldset;
6
use Riclep\StoryblokForms\Blocks\LsfRepeatingFieldset;
7
use Riclep\StoryblokForms\Traits\ToJson;
8
9
class Input extends \Riclep\Storyblok\Block
10
{
11
	use ToJson;
12
13
	protected $inFieldSet = false;
14
	protected $isRepeating = false;
15
	protected $key;
16
17
	/**
18
	 * @var string[] All the Validators for this Input
19
	 */
20
	protected $_casts = ['validators' => Validators::class];
21
22
	public function __construct($content, $parent = null)
23
	{
24
		parent::__construct($content, $parent);
25
26
		if ($this->parent() instanceof LsfFieldset) {
27
			$this->inFieldSet = true;
28
		}
29
30
		if ($this->parent() instanceof LsfRepeatingFieldset) {
31
			$this->isRepeating = true;
32
		}
33
	}
34
35
	public function getInputNameAttribute() {
36
		if ($this->isRepeating) {
37
			return $this->parent()->input_name . '[' . $this->key . '][' . $this->content()['name'] . ']';
38
		}
39
40
		if ($this->inFieldSet) {
41
			return $this->parent()->input_name . '[' . $this->content()['name'] . ']';
42
		}
43
44
		return $this->content()['name'];
45
	}
46
47
48
	public function response($input) {
49
		//dd($input, $this);
50
51
		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\Input. 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

51
		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...
52
53
//			dump($field, $input);
54
// TODO - handle radio buttons being empty
55
			dump($field, $input, $field->name);
56
57
			return $field->response($input[$field->name]);
58
			/*dd($field);
59
60
			return [
61
				'label' => $field->label,
62
				'response' => $field->response($input[$field->name] ?? ''),
63
			];*/
64
		})->toArray();
65
	}
66
67
	public function loopKey($key) {
68
		$this->key = $key;
69
70
		return $this;
71
	}
72
73
74
	/**
75
	 * All the Validation rules for this Input
76
	 *
77
	 * @return mixed
78
	 */
79
	public function validationRules() {
80
		return $this->validators->validationRules();
0 ignored issues
show
Bug Best Practice introduced by
The property validators does not exist on Riclep\StoryblokForms\Input. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method validationRules() 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

80
		return $this->validators->/** @scrutinizer ignore-call */ validationRules();

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...
81
	}
82
83
	/**
84
	 * All the error messages for this Input
85
	 *
86
	 * @return mixed
87
	 */
88
	public function errorMessages() {
89
		return $this->validators->errorMessages();
0 ignored issues
show
Bug Best Practice introduced by
The property validators does not exist on Riclep\StoryblokForms\Input. Since you implemented __get, consider adding a @property annotation.
Loading history...
90
	}
91
}