Passed
Push — develop ( d76358...3e7721 )
by Richard
04:12 queued 12s
created

LsfInput::errorMessages()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 17
rs 10
cc 4
nc 2
nop 0
1
<?php
2
3
namespace Riclep\StoryblokForms\Blocks;
4
5
use Illuminate\Support\Str;
6
use Riclep\StoryblokForms\Input;
7
8
class LsfInput extends Input
9
{
10
	// Interface this....
11
12
	protected $type = 'input';
13
14
	/**
15
	 * TODO - can this be moved into the validator and done at source?
16
	 *
17
	 * All the Validation rules for this Input
18
	 *
19
	 * @return mixed
20
	 */
21
	public function validationRules() {
22
		$rules = $this->validators->validationRules();
0 ignored issues
show
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

22
		/** @scrutinizer ignore-call */ 
23
  $rules = $this->validators->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...
Bug Best Practice introduced by
The property validators does not exist on Riclep\StoryblokForms\Blocks\LsfInput. Since you implemented __get, consider adding a @property annotation.
Loading history...
23
24
		/**
25
		 * Rewrite required to required_if for items inside conditional selects
26
		 */
27
		if ($this->parent() instanceof LsfConditionalSelect) {
28
			if (is_array($this->settings('lsf_is_conditional')['when_parent_is'])) {
29
				$requiredWhen = implode(',', $this->settings('lsf_is_conditional')['when_parent_is']);
30
			} else {
31
				$requiredWhen = $this->settings('lsf_is_conditional')['when_parent_is'];
32
			}
33
34
			foreach ($rules as $key => $rule) {
35
				if (in_array('required', $rule)) {
36
					$requiredKey = array_search('required', $rule);
37
38
					$rules[$key][$requiredKey] = 'required_if:' . $this->parent()->input_dot_name . '.selected,' . $requiredWhen;
0 ignored issues
show
Bug Best Practice introduced by
The property input_dot_name does not exist on Riclep\StoryblokForms\Blocks\LsfConditionalSelect. Since you implemented __get, consider adding a @property annotation.
Loading history...
39
				}
40
			}
41
		}
42
43
		return $rules;
44
	}
45
46
	/**
47
	 * TODO - can this be moved into the validator and done at source?
48
	 *
49
	 * All the Validation rules for this Input
50
	 *
51
	 * @return mixed
52
	 */
53
	public function errorMessages() {
54
		$messages = $this->validators->errorMessages();
0 ignored issues
show
Bug Best Practice introduced by
The property validators does not exist on Riclep\StoryblokForms\Blocks\LsfInput. Since you implemented __get, consider adding a @property annotation.
Loading history...
55
56
		/**
57
		 * Rewrite required to required_if for items inside conditional selects
58
		 */
59
		if ($this->parent() instanceof LsfConditionalSelect) {
60
			foreach ($messages as $key => $rule) {
61
				if (Str::endsWith($key, 'required')) {
62
					$messages[$key . '_if'] = $messages[$key];
63
64
					unset($messages[$key]);
65
				}
66
			}
67
		}
68
69
		return $messages;
70
	}
71
72
	/**
73
	 * Returns the Input’s response after the form has been submitted and validated
74
	 *
75
	 * @param $input
76
	 * @return mixed
77
	 */
78
	public function response($input) {
79
		return [
80
			'label' => $this->label,
0 ignored issues
show
Bug Best Practice introduced by
The property label does not exist on Riclep\StoryblokForms\Blocks\LsfInput. Since you implemented __get, consider adding a @property annotation.
Loading history...
81
			'response' => $input,
82
			'type' => $this->type,
83
		];
84
	}
85
}