Skip to content

Commit bb8c375

Browse files
committed
Added trait TranslationsTrait and use it in Validator and Validation classes
1 parent 550c6c4 commit bb8c375

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

src/Traits/TranslationsTrait.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Rakit\Validation\Traits;
4+
5+
trait TranslationsTrait
6+
{
7+
8+
/** @var array */
9+
protected $translations = [];
10+
11+
/**
12+
* Given $key and $translation to set translation
13+
*
14+
* @param mixed $key
15+
* @param mixed $translation
16+
* @return void
17+
*/
18+
public function setTranslation(string $key, string $translation)
19+
{
20+
$this->translations[$key] = $translation;
21+
}
22+
23+
/**
24+
* Given $translations and set multiple translations
25+
*
26+
* @param array $translations
27+
* @return void
28+
*/
29+
public function setTranslations(array $translations)
30+
{
31+
$this->translations = array_merge($this->translations, $translations);
32+
}
33+
34+
/**
35+
* Given translation from given $key
36+
*
37+
* @param string $key
38+
* @return string
39+
*/
40+
public function getTranslation(string $key): string
41+
{
42+
return array_key_exists($key, $this->translations) ? $this->translations[$key] : $key;
43+
}
44+
45+
/**
46+
* Get all $translations
47+
*
48+
* @return array
49+
*/
50+
public function getTranslations(): array
51+
{
52+
return $this->translations;
53+
}
54+
}

src/Validation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
class Validation
1111
{
12+
use Traits\TranslationsTrait;
1213

1314
/** @var mixed */
1415
protected $validator;
@@ -386,7 +387,7 @@ protected function resolveAttributeName(Attribute $attribute): string
386387
protected function resolveMessage(Attribute $attribute, $value, Rule $validator): string
387388
{
388389
$primaryAttribute = $attribute->getPrimaryAttribute();
389-
$params = $validator->getParameters();
390+
$params = array_merge($validator->getParameters(), $validator->getParametersTexts());
390391
$attributeKey = $attribute->getKey();
391392
$ruleKey = $validator->getKey();
392393
$alias = $attribute->getAlias() ?: $this->resolveAttributeName($attribute);

src/Validator.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
class Validator
66
{
7+
use Traits\TranslationsTrait;
78

89
/** @var array */
910
protected $messages = [];
1011

12+
/** @var translations */
13+
protected $translations = [];
14+
1115
/** @var array */
1216
protected $validators = [];
1317

@@ -102,7 +106,10 @@ public function validate(array $inputs, array $rules, array $messages = []): Val
102106
public function make(array $inputs, array $rules, array $messages = []): Validation
103107
{
104108
$messages = array_merge($this->messages, $messages);
105-
return new Validation($this, $inputs, $rules, $messages);
109+
$validation = new Validation($this, $inputs, $rules, $messages);
110+
$validation->setTranslations($this->getTranslations());
111+
112+
return $validation;
106113
}
107114

108115
/**

0 commit comments

Comments
 (0)