allow global messages format

Signed-off-by: Dayle Rees <thepunkfan@gmail.com>
This commit is contained in:
Dayle Rees 2012-05-25 18:47:17 +01:00
parent 34919aff71
commit 3f0adcb5f3
1 changed files with 28 additions and 3 deletions

View File

@ -9,6 +9,13 @@ class Messages {
*/
public $messages;
/**
* Default format for message output.
*
* @var string
*/
public $format = ':message';
/**
* Create a new Messages instance.
*
@ -68,6 +75,21 @@ public function has($key = null)
return $this->first($key) !== '';
}
/**
* Set the default message format for output.
*
* <code>
* // Apply a new default format.
* $messages->set_format('email', '<p>this is my :message</p>');
* </code>
*
* @param string $format
*/
public function set_format($format = ':message')
{
$this->format = $format;
}
/**
* Get the first message from the container for a given key.
*
@ -86,8 +108,9 @@ public function has($key = null)
* @param string $format
* @return string
*/
public function first($key = null, $format = ':message')
public function first($key = null, $format = null)
{
$format = ($format === null) ? $this->format : $format;
$messages = is_null($key) ? $this->all($format) : $this->get($key, $format);
return (count($messages) > 0) ? $messages[0] : '';
@ -108,8 +131,9 @@ public function first($key = null, $format = ':message')
* @param string $format
* @return array
*/
public function get($key, $format = ':message')
public function get($key, $format = null)
{
$format = ($format === null) ? $this->format : $format;
if (array_key_exists($key, $this->messages))
{
return $this->format($this->messages[$key], $format);
@ -132,8 +156,9 @@ public function get($key, $format = ':message')
* @param string $format
* @return array
*/
public function all($format = ':message')
public function all($format = null)
{
$format = ($format === null) ? $this->format : $format;
$all = array();
foreach ($this->messages as $messages)