Passed
Push — develop ( ee0fc4...945824 )
by Richard
03:22
created

LsfFieldset::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 2
dl 0
loc 10
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
	/**
15
	 * Returns all the validation rules for the fields in this Fieldset
16
	 *
17
	 * @return array
18
	 */
19
	public function validationRules() {
20
		$rules = [];
21
22
		$this->fields->each(function ($field) use (&$rules) {
0 ignored issues
show
Bug introduced by
The method each() 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

22
		$this->fields->/** @scrutinizer ignore-call */ 
23
                 each(function ($field) use (&$rules) {

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...
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...
23
			$rules = array_merge($rules, $field->validationRules());
24
		});
25
26
		return $rules;
27
	}
28
29
	/**
30
	 * Returns all the error messages for the fields in this Fieldset
31
	 *
32
	 * @return array
33
	 */
34
	public function errorMessages() {
35
		$rules = [];
36
37
		$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...
38
			$rules = array_merge($rules, $field->errorMessages());
39
		});
40
41
		return $rules;
42
	}
43
44
	public function response($input) {
45
		return [
46
			'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...
47
			'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...
48
49
				// Handle empty radio buttons sending nothing in POST request
50
				if ($field instanceof \Riclep\StoryblokForms\Blocks\LsfRadioButton) {
51
					if (!array_key_exists($field->name, $input)) {
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Riclep\StoryblokForms\Blocks\LsfRadioButton. Since you implemented __get, consider adding a @property annotation.
Loading history...
52
						$input[$field->name] = null;
53
					}
54
				}
55
56
				return $field->response($input[$field->name]);
57
			})->toArray()
58
		];
59
	}
60
}