Passed
Push — develop ( 2cd0ec...529aa7 )
by Richard
03:17
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
	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
40
	/**
41
	 * Returns all the validation rules for the fields in this Fieldset
42
	 *
43
	 * @return array
44
	 */
45
	public function validationRules() {
46
		$rules = [];
47
48
		$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...
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

48
		$this->fields->/** @scrutinizer ignore-call */ 
49
                 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...
49
			$rules = array_merge($rules, $field->validationRules());
50
		});
51
52
		return $rules;
53
	}
54
55
	/**
56
	 * Returns all the error messages for the fields in this Fieldset
57
	 *
58
	 * @return array
59
	 */
60
	public function errorMessages() {
61
		$rules = [];
62
63
		$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...
64
			$rules = array_merge($rules, $field->errorMessages());
65
		});
66
67
		return $rules;
68
	}
69
}