Passed
Push — develop ( daadeb...04e613 )
by Richard
03:32
created

Validator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 15
eloc 27
c 7
b 0
f 0
dl 0
loc 97
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A rule() 0 24 6
A ruleForValidation() 0 3 1
B errorMessage() 0 26 7
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
			$className = 'App\Rules\\' . $this->definition['class'];
83
84
			if (array_key_exists('parameter', $this->definition) && $this->definition['parameter']) {
85
				return new $className($this->definition['parameter']);
86
			}
87
88
			$class = new $className();
89
90
			return $class->errorMessage();
91
		}
92
93
		// If an error message was defined in Storyblok
94
		if (array_key_exists('error_message', $this->definition) && $this->definition['error_message']) {
95
			return $this->definition['error_message'];
96
		}
97
98
		// Get the default message from Laravel
99
		if (trans()->has('validation.' . $this->rule())) {
100
			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...
101
		}
102
103
		return '';
104
	}
105
}