Skip to content

Commit

Permalink
Added trait TranslationsTrait and use it in Validator and Validation …
Browse files Browse the repository at this point in the history
…classes
  • Loading branch information
emsifa committed Nov 27, 2018
1 parent 550c6c4 commit bb8c375
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
54 changes: 54 additions & 0 deletions src/Traits/TranslationsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Rakit\Validation\Traits;

trait TranslationsTrait
{

/** @var array */
protected $translations = [];

/**
* Given $key and $translation to set translation
*
* @param mixed $key
* @param mixed $translation
* @return void
*/
public function setTranslation(string $key, string $translation)
{
$this->translations[$key] = $translation;
}

/**
* Given $translations and set multiple translations
*
* @param array $translations
* @return void
*/
public function setTranslations(array $translations)
{
$this->translations = array_merge($this->translations, $translations);
}

/**
* Given translation from given $key
*
* @param string $key
* @return string
*/
public function getTranslation(string $key): string
{
return array_key_exists($key, $this->translations) ? $this->translations[$key] : $key;
}

/**
* Get all $translations
*
* @return array
*/
public function getTranslations(): array
{
return $this->translations;
}
}
3 changes: 2 additions & 1 deletion src/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

class Validation
{
use Traits\TranslationsTrait;

/** @var mixed */
protected $validator;
Expand Down Expand Up @@ -386,7 +387,7 @@ protected function resolveAttributeName(Attribute $attribute): string
protected function resolveMessage(Attribute $attribute, $value, Rule $validator): string
{
$primaryAttribute = $attribute->getPrimaryAttribute();
$params = $validator->getParameters();
$params = array_merge($validator->getParameters(), $validator->getParametersTexts());
$attributeKey = $attribute->getKey();
$ruleKey = $validator->getKey();
$alias = $attribute->getAlias() ?: $this->resolveAttributeName($attribute);
Expand Down
9 changes: 8 additions & 1 deletion src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

class Validator
{
use Traits\TranslationsTrait;

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

/** @var translations */
protected $translations = [];

/** @var array */
protected $validators = [];

Expand Down Expand Up @@ -102,7 +106,10 @@ public function validate(array $inputs, array $rules, array $messages = []): Val
public function make(array $inputs, array $rules, array $messages = []): Validation
{
$messages = array_merge($this->messages, $messages);
return new Validation($this, $inputs, $rules, $messages);
$validation = new Validation($this, $inputs, $rules, $messages);
$validation->setTranslations($this->getTranslations());

return $validation;
}

/**
Expand Down

0 comments on commit bb8c375

Please sign in to comment.