From 84411a8814b12a2eced40a4c5d639ab7146f7607 Mon Sep 17 00:00:00 2001 From: Muhammad Syifa Date: Thu, 29 Nov 2018 21:40:16 +0700 Subject: [PATCH] Messages getter and setter using trait --- src/Traits/MessagesTrait.php | 54 ++++++++++++++++++++++++++++++++++++ src/Validation.php | 28 +------------------ src/Validator.php | 28 +------------------ 3 files changed, 56 insertions(+), 54 deletions(-) create mode 100644 src/Traits/MessagesTrait.php diff --git a/src/Traits/MessagesTrait.php b/src/Traits/MessagesTrait.php new file mode 100644 index 0000000..14ed28e --- /dev/null +++ b/src/Traits/MessagesTrait.php @@ -0,0 +1,54 @@ +messages[$key] = $message; + } + + /** + * Given $messages and set multiple messages + * + * @param array $messages + * @return void + */ + public function setMessages(array $messages) + { + $this->messages = array_merge($this->messages, $messages); + } + + /** + * Given message from given $key + * + * @param string $key + * @return string + */ + public function getMessage(string $key): string + { + return array_key_exists($key, $this->messages) ? $this->messages[$key] : $key; + } + + /** + * Get all $messages + * + * @return array + */ + public function getMessages(): array + { + return $this->messages; + } +} diff --git a/src/Validation.php b/src/Validation.php index 71f01b2..601ec69 100644 --- a/src/Validation.php +++ b/src/Validation.php @@ -9,7 +9,7 @@ class Validation { - use Traits\TranslationsTrait; + use Traits\TranslationsTrait, Traits\MessagesTrait; /** @var mixed */ protected $validator; @@ -20,9 +20,6 @@ class Validation /** @var array */ protected $attributes = []; - /** @var array */ - protected $messages = []; - /** @var array */ protected $aliases = []; @@ -523,29 +520,6 @@ protected function parseRule(string $rule): array return [$rulename, $params]; } - /** - * Set message - * - * @param mixed $key - * @param mixed $message - * @return void - */ - public function setMessage(string $key, string $message) - { - $this->messages[$key] = $message; - } - - /** - * Set multiple messages - * - * @param array $messages - * @return void - */ - public function setMessages(array $messages) - { - $this->messages = array_merge($this->messages, $messages); - } - /** * Given $attributeKey and $alias then assign alias * diff --git a/src/Validator.php b/src/Validator.php index ba25f62..943952b 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -4,10 +4,7 @@ class Validator { - use Traits\TranslationsTrait; - - /** @var array */ - protected $messages = []; + use Traits\TranslationsTrait, Traits\MessagesTrait; /** @var translations */ protected $translations = []; @@ -33,29 +30,6 @@ public function __construct(array $messages = []) $this->registerBaseValidators(); } - /** - * Given $key and $message to set message - * - * @param mixed $key - * @param mixed $message - * @return void - */ - public function setMessage(string $key, string $message) - { - $this->messages[$key] = $message; - } - - /** - * Given $messages and set multiple messages - * - * @param array $messages - * @return void - */ - public function setMessages(array $messages) - { - $this->messages = array_merge($this->messages, $messages); - } - /** * Register or override existing validator *