Commit c2ad6a81 authored by Taylor Otwell's avatar Taylor Otwell

Merge pull request #714 from daylerees/feature/error-format

Allow custom global message format.
parents db620621 3f0adcb5
...@@ -9,6 +9,13 @@ class Messages { ...@@ -9,6 +9,13 @@ class Messages {
*/ */
public $messages; public $messages;
/**
* Default format for message output.
*
* @var string
*/
public $format = ':message';
/** /**
* Create a new Messages instance. * Create a new Messages instance.
* *
...@@ -68,6 +75,21 @@ class Messages { ...@@ -68,6 +75,21 @@ class Messages {
return $this->first($key) !== ''; 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. * Get the first message from the container for a given key.
* *
...@@ -86,8 +108,9 @@ class Messages { ...@@ -86,8 +108,9 @@ class Messages {
* @param string $format * @param string $format
* @return string * @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); $messages = is_null($key) ? $this->all($format) : $this->get($key, $format);
return (count($messages) > 0) ? $messages[0] : ''; return (count($messages) > 0) ? $messages[0] : '';
...@@ -108,8 +131,9 @@ class Messages { ...@@ -108,8 +131,9 @@ class Messages {
* @param string $format * @param string $format
* @return array * @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)) if (array_key_exists($key, $this->messages))
{ {
return $this->format($this->messages[$key], $format); return $this->format($this->messages[$key], $format);
...@@ -132,8 +156,9 @@ class Messages { ...@@ -132,8 +156,9 @@ class Messages {
* @param string $format * @param string $format
* @return array * @return array
*/ */
public function all($format = ':message') public function all($format = null)
{ {
$format = ($format === null) ? $this->format : $format;
$all = array(); $all = array();
foreach ($this->messages as $messages) foreach ($this->messages as $messages)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment