|
4 | 4 |
|
5 | 5 | namespace Yiisoft\Validator; |
6 | 6 |
|
7 | | -use Yiisoft\Translator\TranslatorInterface; |
8 | | - |
9 | 7 | /** |
10 | 8 | * Rule represents a single value validation rule. |
11 | 9 | */ |
12 | 10 | abstract class Rule |
13 | 11 | { |
14 | | - private ?TranslatorInterface $translator = null; |
| 12 | + private ?FormatterInterface $formatter = null; |
15 | 13 | private ?string $translationDomain = null; |
16 | 14 | private ?string $translationLocale = null; |
17 | 15 | private bool $skipOnEmpty = false; |
@@ -57,38 +55,22 @@ final public function validate($value, DataSetInterface $dataSet = null, bool $p |
57 | 55 | */ |
58 | 56 | abstract protected function validateValue($value, DataSetInterface $dataSet = null): Result; |
59 | 57 |
|
60 | | - public function translator(TranslatorInterface $translator): self |
61 | | - { |
62 | | - $new = clone $this; |
63 | | - $new->translator = $translator; |
64 | | - return $new; |
65 | | - } |
66 | | - |
67 | | - public function translationDomain(string $translation): self |
| 58 | + public function formatter(FormatterInterface $formatter): self |
68 | 59 | { |
69 | 60 | $new = clone $this; |
70 | | - $new->translationDomain = $translation; |
| 61 | + $new->formatter = $formatter; |
71 | 62 | return $new; |
72 | 63 | } |
73 | 64 |
|
74 | | - public function translationLocale(string $locale): self |
| 65 | + protected function formatMessage(string $message, array $parameters = []): string |
75 | 66 | { |
76 | | - $new = clone $this; |
77 | | - $new->translationLocale = $locale; |
78 | | - return $new; |
79 | | - } |
80 | | - |
81 | | - protected function translateMessage(string $message, array $parameters = []): string |
82 | | - { |
83 | | - if ($this->translator === null) { |
84 | | - return $this->formatMessage($message, $parameters); |
| 67 | + if ($this->formatter === null) { |
| 68 | + $this->formatter = new Formatter(); |
85 | 69 | } |
86 | 70 |
|
87 | | - return $this->translator->translate( |
| 71 | + return $this->formatter->format( |
88 | 72 | $message, |
89 | | - $parameters, |
90 | | - $this->translationDomain ?? 'validators', |
91 | | - $this->translationLocale |
| 73 | + $parameters |
92 | 74 | ); |
93 | 75 | } |
94 | 76 |
|
@@ -138,22 +120,6 @@ public function skipOnEmpty(bool $value): self |
138 | 120 | return $new; |
139 | 121 | } |
140 | 122 |
|
141 | | - private function formatMessage(string $message, array $arguments = []): string |
142 | | - { |
143 | | - $replacements = []; |
144 | | - foreach ($arguments as $key => $value) { |
145 | | - if (is_array($value)) { |
146 | | - $value = 'array'; |
147 | | - } elseif (is_object($value)) { |
148 | | - $value = 'object'; |
149 | | - } elseif (is_resource($value)) { |
150 | | - $value = 'resource'; |
151 | | - } |
152 | | - $replacements['{' . $key . '}'] = $value; |
153 | | - } |
154 | | - return strtr($message, $replacements); |
155 | | - } |
156 | | - |
157 | 123 | /** |
158 | 124 | * Checks if the given value is empty. |
159 | 125 | * A value is considered empty if it is null, an empty array, or an empty string. |
|
0 commit comments