-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added trait TranslationsTrait and use it in Validator and Validation …
…classes
- Loading branch information
Showing
3 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters