Files
spa-api/laravel/messages.php

194 lines
3.9 KiB
PHP
Raw Normal View History

<?php namespace Laravel;
class Messages {
/**
* All of the registered messages.
*
* @var array
*/
public $messages;
/**
* Default format for message output.
*
* @var string
*/
public $format = ':message';
/**
* Create a new Messages instance.
*
2012-02-04 21:30:52 +00:00
* @param array $messages
* @return void
*/
public function __construct($messages = array())
{
2011-11-16 22:46:54 -06:00
$this->messages = (array) $messages;
}
/**
* Add a message to the collector.
*
2011-11-22 18:00:17 -06:00
* <code>
* // Add a message for the e-mail attribute
* $messages->add('email', 'The e-mail address is invalid.');
* </code>
*
* @param string $key
* @param string $message
* @return void
*/
public function add($key, $message)
{
2011-10-26 21:21:31 -05:00
if ($this->unique($key, $message)) $this->messages[$key][] = $message;
}
/**
* Determine if a key and message combination already exists.
*
* @param string $key
* @param string $message
* @return bool
*/
protected function unique($key, $message)
{
2011-11-22 18:00:17 -06:00
return ! isset($this->messages[$key]) or ! in_array($message, $this->messages[$key]);
}
/**
* Determine if messages exist for a given key.
*
* <code>
* // Is there a message for the e-mail attribute
* return $messages->has('email');
*
* // Is there a message for the any attribute
* echo $messages->has();
* </code>
*
* @param string $key
* @return bool
*/
public function has($key = null)
{
return $this->first($key) !== '';
}
/**
* Set the default message format for output.
*
* <code>
* // Apply a new default format.
2012-05-30 08:41:03 -05:00
* $messages->format('email', '<p>this is my :message</p>');
* </code>
*
* @param string $format
*/
2012-05-30 08:41:03 -05:00
public function format($format = ':message')
{
$this->format = $format;
}
/**
2012-01-16 13:59:24 -06:00
* Get the first message from the container for a given key.
*
2011-09-28 22:42:28 -05:00
* <code>
* // Echo the first message out of all messages.
* echo $messages->first();
*
2012-01-16 13:59:24 -06:00
* // Echo the first message for the e-mail attribute
* echo $messages->first('email');
2011-09-28 22:42:28 -05:00
*
* // Format the first message for the e-mail attribute
2012-01-16 13:59:24 -06:00
* echo $messages->first('email', '<p>:message</p>');
2011-09-28 22:42:28 -05:00
* </code>
*
* @param string $key
* @param string $format
* @return string
*/
public function first($key = null, $format = null)
{
$format = ($format === null) ? $this->format : $format;
2012-05-30 08:41:03 -05:00
$messages = is_null($key) ? $this->all($format) : $this->get($key, $format);
return (count($messages) > 0) ? $messages[0] : '';
}
/**
2012-01-16 13:59:24 -06:00
* Get all of the messages from the container for a given key.
*
2011-09-28 22:42:28 -05:00
* <code>
2012-01-16 13:59:24 -06:00
* // Echo all of the messages for the e-mail attribute
* echo $messages->get('email');
2011-09-28 22:42:28 -05:00
*
* // Format all of the messages for the e-mail attribute
2012-01-16 13:59:24 -06:00
* echo $messages->get('email', '<p>:message</p>');
2011-09-28 22:42:28 -05:00
* </code>
*
* @param string $key
* @param string $format
* @return array
*/
public function get($key, $format = null)
{
$format = ($format === null) ? $this->format : $format;
2012-05-30 08:41:03 -05:00
2011-10-15 14:04:11 -05:00
if (array_key_exists($key, $this->messages))
{
2012-05-30 08:41:03 -05:00
return $this->transform($this->messages[$key], $format);
2011-10-15 14:04:11 -05:00
}
return array();
}
/**
2012-01-16 13:59:24 -06:00
* Get all of the messages for every key in the container.
*
2011-09-28 22:42:28 -05:00
* <code>
* // Get all of the messages in the collector
* $all = $messages->all();
*
* // Format all of the messages in the collector
* $all = $messages->all('<p>:message</p>');
* </code>
*
* @param string $format
* @return array
*/
public function all($format = null)
{
$format = ($format === null) ? $this->format : $format;
2012-05-30 08:41:03 -05:00
$all = array();
foreach ($this->messages as $messages)
{
2012-05-30 08:41:03 -05:00
$all = array_merge($all, $this->transform($messages, $format));
}
return $all;
}
/**
* Format an array of messages.
*
* @param array $messages
* @param string $format
* @return array
*/
2012-05-30 08:41:03 -05:00
protected function transform($messages, $format)
{
2011-11-15 23:15:58 -06:00
$messages = (array) $messages;
foreach ($messages as $key => &$message)
{
$message = str_replace(':message', $message, $format);
}
return $messages;
}
}