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']; |
|
|
|
|
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]); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return ''; |
98
|
|
|
} |
99
|
|
|
} |