* // Get the first message for the e-mail attribute
* $email = $messages->first('email');
*
* // Format the first message for the e-mail attribute
* $email = $messages->first('email', ':message
');
*
*
* @param string $key
* @param string $format
* @return string
*/
public function first($key, $format = ':message')
{
return (count($messages = $this->get($key, $format)) > 0) ? $messages[0] : '';
}
/**
* Get all of the messages for a key.
*
*
* // Get all of the messages for the e-mail attribute
* $email = $messages->get('email');
*
* // Format all of the messages for the e-mail attribute
* $email = $messages->get('email', ':message
');
*
*
* @param string $key
* @param string $format
* @return array
*/
public function get($key = null, $format = ':message')
{
if (is_null($key)) return $this->all($format);
if (array_key_exists($key, $this->messages))
{
return $this->format($this->messages[$key], $format);
}
return array();
}
/**
* Get all of the messages for every key.
*
*
* // Get all of the messages in the collector
* $all = $messages->all();
*
* // Format all of the messages in the collector
* $all = $messages->all(':message
');
*
*
* @param string $format
* @return array
*/
public function all($format = ':message')
{
$all = array();
foreach ($this->messages as $messages)
{
$all = array_merge($all, $this->format($messages, $format));
}
return $all;
}
/**
* Format an array of messages.
*
* @param array $messages
* @param string $format
* @return array
*/
protected function format($messages, $format)
{
foreach ($messages as $key => &$message)
{
$message = str_replace(':message', $message, $format);
}
return $messages;
}
}