Passed
Push — develop ( 77a882...b7c152 )
by Richard
03:22 queued 11s
created

Validator::ruleForValidation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Riclep\StoryblokForms;
4
5
use Illuminate\Support\Str;
6
7
class Validator
8
{
9
	/**
10
	 * @var array The definition of the rule from Storyblok
11
	 */
12
	protected array $definition;
13
14
15
	/**
16
	 * @var Input The Field on which the validation is being applied
17
	 */
18
	protected Input $field;
19
20
	/**
21
	 * @param $definition
22
	 * @param $field
23
	 */
24
	public function __construct($definition, $field)
25
	{
26
		$this->definition = $definition;
27
		$this->field = $field;
28
	}
29
30
	/**
31
	 * Converts the Rule from Storyblok into Laravel’s format
32
	 *
33
	 * @return mixed
34
	 */
35
	public function rule(): mixed
36
	{
37
		// If using a Class based rule return a new instant
38
		if ($this->definition['component'] === 'lsf-validator-class') {
39
			$class = 'App\Rules\\' . $this->definition['class'];
40
41
			if (array_key_exists('parameter', $this->definition) && $this->definition['parameter']) {
42
				return new $class($this->definition['parameter']);
43
			}
44
45
			return new $class;
46
		}
47
48
		// named rule with a parameter
49
		if (array_key_exists('parameter', $this->definition)) {
50
			if (array_key_exists('value', $this->definition)) {
51
				return Str::afterLast($this->definition['component'], '-') . ':' . $this->definition['parameter'] . ',' . $this->definition['value'];
52
			}
53
54
			return Str::afterLast($this->definition['component'], '-') . ':' . $this->definition['parameter'];
55
		}
56
57
		// simple text only rule lsf-validator-required
58
		return Str::afterLast($this->definition['component'], '-');
59
	}
60
61
	/**
62
	 * Returns the validation rule in a format for the message bag
63
	 * removing :somevalue etc.
64
	 *
65
	 * @return string
66
	 */
67
	public function ruleForValidation(): string
68
	{
69
		return Str::before($this->rule(), ':');
70
	}
71
72
	/**
73
	 * Get the error messages for the rule. Class validators must implement
74
	 * an errorMessage() method
75
	 *
76
	 * @return mixed
77
	 */
78
	public function errorMessage()
79
	{
80
		// Class validators
81
		if ($this->definition['component'] === 'lsf-validator-class') {
82
			$class = 'App\Rules\\' . $this->definition['class'];
0 ignored issues
show
Unused Code introduced by
The assignment to $class is dead and can be removed.
Loading history...
83
84
			return;
85
		}
86
87
		// If an error message was defined in Storyblok
88
		if (array_key_exists('error_message', $this->definition) && $this->definition['error_message']) {
89
			return $this->definition['error_message'];
90
		}
91
92
		// Get the default message from Laravel
93
		if (trans()->has('validation.' . $this->rule())) {
94
			return __('validation.' . $this->rule(), ['attribute' => $this->field->label]);
0 ignored issues
show
Bug Best Practice introduced by
The property label does not exist on Riclep\StoryblokForms\Input. Since you implemented __get, consider adding a @property annotation.
Loading history...
95
		}
96
97
		return '';
98
	}
99
}