Skip to content

Commit 94d693e

Browse files
authored
Make abstraction over Translator
1 parent 955c837 commit 94d693e

21 files changed

Lines changed: 108 additions & 125 deletions

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
"yiisoft/arrays": "dev-master",
3131
"yiisoft/friendly-exception": "^1.0",
3232
"yiisoft/network-utilities": "dev-master",
33-
"yiisoft/strings": "^1.0",
34-
"yiisoft/translator": "dev-master"
33+
"yiisoft/strings": "^1.0"
3534
},
3635
"require-dev": {
3736
"phpunit/phpunit": "^9.5",

src/Formatter.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Validator;
6+
7+
class Formatter implements FormatterInterface
8+
{
9+
public function format(string $message, array $parameters = []): string
10+
{
11+
$replacements = [];
12+
foreach ($parameters as $key => $value) {
13+
if (is_array($value)) {
14+
$value = 'array';
15+
} elseif (is_object($value)) {
16+
$value = 'object';
17+
} elseif (is_resource($value)) {
18+
$value = 'resource';
19+
}
20+
$replacements['{' . $key . '}'] = $value;
21+
}
22+
return strtr($message, $replacements);
23+
}
24+
}

src/FormatterInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Validator;
6+
7+
interface FormatterInterface
8+
{
9+
public function format(string $message, array $parameters = []): string;
10+
}

src/Rule.php

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44

55
namespace Yiisoft\Validator;
66

7-
use Yiisoft\Translator\TranslatorInterface;
8-
97
/**
108
* Rule represents a single value validation rule.
119
*/
1210
abstract class Rule
1311
{
14-
private ?TranslatorInterface $translator = null;
12+
private ?FormatterInterface $formatter = null;
1513
private ?string $translationDomain = null;
1614
private ?string $translationLocale = null;
1715
private bool $skipOnEmpty = false;
@@ -57,38 +55,22 @@ final public function validate($value, DataSetInterface $dataSet = null, bool $p
5755
*/
5856
abstract protected function validateValue($value, DataSetInterface $dataSet = null): Result;
5957

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
6859
{
6960
$new = clone $this;
70-
$new->translationDomain = $translation;
61+
$new->formatter = $formatter;
7162
return $new;
7263
}
7364

74-
public function translationLocale(string $locale): self
65+
protected function formatMessage(string $message, array $parameters = []): string
7566
{
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();
8569
}
8670

87-
return $this->translator->translate(
71+
return $this->formatter->format(
8872
$message,
89-
$parameters,
90-
$this->translationDomain ?? 'validators',
91-
$this->translationLocale
73+
$parameters
9274
);
9375
}
9476

@@ -138,22 +120,6 @@ public function skipOnEmpty(bool $value): self
138120
return $new;
139121
}
140122

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-
157123
/**
158124
* Checks if the given value is empty.
159125
* A value is considered empty if it is null, an empty array, or an empty string.

src/Rule/AtLeast.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function validateValue($value, DataSetInterface $dataSet = null): Resu
5454

5555
if ($filledCount < $this->min) {
5656
$result->addError(
57-
$this->translateMessage(
57+
$this->formatMessage(
5858
$this->message,
5959
[
6060
'min' => $this->min,
@@ -83,7 +83,7 @@ public function getOptions(): array
8383
return array_merge(
8484
parent::getOptions(),
8585
['min' => $this->min],
86-
['message' => $this->translateMessage($this->message, ['min' => $this->min])],
86+
['message' => $this->formatMessage($this->message, ['min' => $this->min])],
8787
);
8888
}
8989
}

src/Rule/Boolean.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected function validateValue($value, DataSetInterface $dataSet = null): Resu
6565

6666
if (!$valid) {
6767
$result->addError(
68-
$this->translateMessage(
68+
$this->formatMessage(
6969
$this->message,
7070
[
7171
'true' => $this->trueValue === true ? 'true' : $this->trueValue,
@@ -86,7 +86,7 @@ public function getOptions(): array
8686
'strict' => $this->strict,
8787
'trueValue' => $this->trueValue,
8888
'falseValue' => $this->falseValue,
89-
'message' => $this->translateMessage(
89+
'message' => $this->formatMessage(
9090
$this->message,
9191
[
9292
'true' => $this->trueValue === true ? 'true' : $this->trueValue,

src/Rule/Callback.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function validateValue($value, DataSetInterface $dataSet = null): Resu
3434

3535
if ($callbackResult->isValid() === false) {
3636
foreach ($callbackResult->getErrors() as $message) {
37-
$result->addError($this->translateMessage($message));
37+
$result->addError($this->formatMessage($message));
3838
}
3939
}
4040
return $result;

src/Rule/CompareTo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ protected function validateValue($value, DataSetInterface $dataSet = null): Resu
136136

137137
if (!$this->compareValues($this->operator, $this->type, $value, $this->compareValue)) {
138138
$result->addError(
139-
$this->translateMessage(
139+
$this->formatMessage(
140140
$this->getMessage(),
141141
[
142142
'value' => $this->compareValue,
@@ -197,7 +197,7 @@ public function getOptions(): array
197197
'type' => $this->type,
198198
'operator' => $this->operator,
199199
'compareValue' => $this->compareValue,
200-
'message' => $this->translateMessage($this->getMessage(), ['value' => $this->compareValue]),
200+
'message' => $this->formatMessage($this->getMessage(), ['value' => $this->compareValue]),
201201
],
202202
);
203203
}

src/Rule/Each.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected function validateValue($value, DataSetInterface $dataSet = null): Resu
4040
if ($itemResult->isValid() === false) {
4141
foreach ($itemResult->getErrors() as $error) {
4242
$result->addError(
43-
$this->translateMessage(
43+
$this->formatMessage(
4444
$this->message,
4545
[
4646
'error' => $error,

src/Rule/Email.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected function validateValue($value, DataSetInterface $dataSet = null): Resu
104104
}
105105

106106
if ($valid === false) {
107-
$result->addError($this->translateMessage($this->message));
107+
$result->addError($this->formatMessage($this->message));
108108
}
109109

110110
return $result;
@@ -155,7 +155,7 @@ public function getOptions(): array
155155
'allowName' => $this->allowName,
156156
'checkDNS' => $this->checkDNS,
157157
'enableIDN' => $this->enableIDN,
158-
'message' => $this->translateMessage($this->message),
158+
'message' => $this->formatMessage($this->message),
159159
],
160160
);
161161
}

0 commit comments

Comments
 (0)