| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 | <?php namespace System;class Messages {	/**	 * All of the messages.	 *	 * @var array	 */	public $messages;	/**	 * Create a new Messages instance.	 *	 * @return void	 */	public function __construct($messages = array())	{		$this->messages = $messages;	}	/**	 * Add a message to the collector.	 *	 * Duplicate messages will not be added.	 *	 * @param  string  $key	 * @param  string  $message	 * @return void	 */	public function add($key, $message)	{		if ( ! isset($this->messages[$key]) or array_search($message, $this->messages[$key]) === false)		{			$this->messages[$key][] = $message;		}	}	/**	 * Determine if messages exist for a given key.	 *	 * @param  string  $key	 * @return bool	 */	public function has($key)	{		return $this->first($key) !== '';	}	/**	 * Get the first message for a given key.	 *	 * @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.	 *	 * If no key is specified, all of the messages will be returned.	 *	 * @param  string  $key	 * @param  string  $format	 * @return array	 */	public function get($key = null, $format = ':message')	{		if (is_null($key))		{			return $this->all($format);		}		return (array_key_exists($key, $this->messages)) ? $this->format($this->messages[$key], $format) : array();	}	/**	 * Get all of the messages.	 *	 * @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	 */	private function format($messages, $format)	{		foreach ($messages as $key => &$message)		{			$message = str_replace(':message', $message, $format);		}		return $messages;	}}
 |