Skip to content

Commit

Permalink
Merge pull request #74 from rakit/messages-trait
Browse files Browse the repository at this point in the history
Messages getter and setter using trait
  • Loading branch information
emsifa authored Nov 29, 2018
2 parents 86a13f9 + 84411a8 commit 75dc5bd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 54 deletions.
54 changes: 54 additions & 0 deletions src/Traits/MessagesTrait.php
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;
}
}
28 changes: 1 addition & 27 deletions src/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class Validation
{
use Traits\TranslationsTrait;
use Traits\TranslationsTrait, Traits\MessagesTrait;

/** @var mixed */
protected $validator;
Expand All @@ -20,9 +20,6 @@ class Validation
/** @var array */
protected $attributes = [];

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

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

Expand Down Expand Up @@ -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
*
Expand Down
28 changes: 1 addition & 27 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

class Validator
{
use Traits\TranslationsTrait;

/** @var array */
protected $messages = [];
use Traits\TranslationsTrait, Traits\MessagesTrait;

/** @var translations */
protected $translations = [];
Expand All @@ -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
*
Expand Down

0 comments on commit 75dc5bd

Please sign in to comment.