-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from rakit/messages-trait
Messages getter and setter using trait
- Loading branch information
Showing
3 changed files
with
56 additions
and
54 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 MessagesTrait | ||
{ | ||
|
||
/** @var array */ | ||
protected $messages = []; | ||
|
||
/** | ||
* 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); | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
} |
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