|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Dimtrovich/Validation. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2023 Dimitri Sitchet Tomkeu <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Dimtrovich\Validation\Rules; |
|
13
|
|
|
|
|
14
|
|
|
use BlitzPHP\Utilities\String\Text; |
|
15
|
|
|
use Dimtrovich\Validation\Utils\Translator; |
|
16
|
|
|
use Rakit\Validation\Helper; |
|
17
|
|
|
use Rakit\Validation\Rule; |
|
18
|
|
|
|
|
19
|
|
|
abstract class AbstractRule extends Rule |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Validation rule name |
|
23
|
|
|
*/ |
|
24
|
|
|
protected const NAME = ''; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Translation locale |
|
28
|
|
|
*/ |
|
29
|
|
|
protected string $locale = 'en'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Aliases for attributes keys |
|
33
|
|
|
* |
|
34
|
|
|
* @var array<string, string> |
|
35
|
|
|
*/ |
|
36
|
|
|
private array $_alias = []; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct() |
|
39
|
|
|
{ |
|
40
|
132 |
|
$this->findTranslatedMessage($this->locale); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function locale(string $locale): self |
|
44
|
|
|
{ |
|
45
|
|
|
if ($locale !== $this->locale) { |
|
46
|
132 |
|
$this->findTranslatedMessage($locale); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
132 |
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get the name of the validation rule |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function name(): string |
|
56
|
|
|
{ |
|
57
|
|
|
if ('' !== static::NAME) { |
|
|
|
|
|
|
58
|
132 |
|
return static::NAME; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
132 |
|
$parts = explode('\\', static::class); |
|
62
|
132 |
|
$name = array_pop($parts); |
|
63
|
|
|
|
|
64
|
132 |
|
return Text::snake($name); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the translation for a given key |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function translate(?string $key, bool $strict = false): ?string |
|
71
|
|
|
{ |
|
72
|
132 |
|
return Translator::translate($key ?: static::name(), $this->locale, $strict); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function getRuleValidator(string $rule): Rule |
|
76
|
|
|
{ |
|
77
|
|
|
$validator = $this->validation->getValidator(); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
return $validator->getValidator($rule); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function getAttributeAlias(string $key): string |
|
83
|
|
|
{ |
|
84
|
|
|
if (! empty($this->_alias[$key])) { |
|
85
|
2 |
|
return $this->_alias[$key]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
$alias = $this->validation->getAlias($key); |
|
89
|
|
|
|
|
90
|
2 |
|
return $this->_alias[$key] = empty($alias) ? $key : $alias; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritDoc} |
|
95
|
|
|
*/ |
|
96
|
|
|
public function parameter(string $key, mixed $default = null): mixed |
|
97
|
|
|
{ |
|
98
|
|
|
if (null === $value = parent::parameter($key)) { |
|
99
|
8 |
|
$value = $default; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
66 |
|
return $value; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function fillAllowedParameters(array $params, string $name): self |
|
106
|
|
|
{ |
|
107
|
|
|
if (count($params) === 1 && is_array($params[0])) { |
|
108
|
4 |
|
$params = $params[0]; |
|
109
|
|
|
} |
|
110
|
24 |
|
$this->params[$name] = $params; |
|
111
|
|
|
|
|
112
|
24 |
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
protected function fillAllowedValuesParameters(array $params): self |
|
116
|
|
|
{ |
|
117
|
16 |
|
return $this->fillAllowedParameters($params, 'allowed_values'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
protected function setAllowedValues(array $values, string $boolean = 'or'): void |
|
121
|
|
|
{ |
|
122
|
16 |
|
$this->setParameterTextValues($values, 'allowed_values', $boolean); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
protected function setParameterTextValues(mixed $values, string $name, string $boolean = 'or'): void |
|
126
|
|
|
{ |
|
127
|
16 |
|
$values = (array) $values; |
|
128
|
16 |
|
$boolean = $this->validation ? $this->validation->getTranslation($boolean) : $boolean; |
|
129
|
16 |
|
$allowedValuesText = Helper::join(Helper::wraps($values, "'"), ', ', " {$boolean} "); |
|
130
|
16 |
|
$this->setParameterText($name, $allowedValuesText); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Parse named parameters to $key => $value items. |
|
135
|
|
|
* |
|
136
|
|
|
* @param array<int, int|string> $parameters |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function parseNamedParameters(array $parameters): array |
|
139
|
|
|
{ |
|
140
|
|
|
return array_reduce($parameters, function ($result, $item) { |
|
141
|
2 |
|
[$key, $value] = array_pad(explode('=', $item, 2), 2, null); |
|
142
|
|
|
|
|
143
|
2 |
|
$result[$key] = $value; |
|
144
|
|
|
|
|
145
|
2 |
|
return $result; |
|
146
|
2 |
|
}); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
private function findTranslatedMessage($locale) |
|
150
|
|
|
{ |
|
151
|
132 |
|
$this->locale = $locale; |
|
152
|
|
|
|
|
153
|
|
|
if ($this->message === 'The :attribute is invalid') { |
|
154
|
|
|
if (null !== $translation = $this->translate(static::name(), true)) { |
|
155
|
132 |
|
$this->setMessage($translation); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|