messages.php 3.02 KB
Newer Older
1
<?php namespace Laravel;
2 3 4 5

class Messages {

	/**
6
	 * All of the registered messages.
7 8 9 10 11 12 13 14 15 16 17 18
	 *
	 * @var array
	 */
	public $messages;

	/**
	 * Create a new Messages instance.
	 *
	 * @return void
	 */
	public function __construct($messages = array())
	{
19
		$this->messages = (array) $messages;
20 21 22 23 24
	}

	/**
	 * Add a message to the collector.
	 *
25 26 27 28 29
	 * <code>
	 *		// Add a message for the e-mail attribute
	 *		$messages->add('email', 'The e-mail address is invalid.');
	 * </code>
	 *
30 31 32 33 34 35
	 * @param  string  $key
	 * @param  string  $message
	 * @return void
	 */
	public function add($key, $message)
	{
36 37 38 39 40 41 42 43 44 45 46 47
		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)
	{
48
		return ! isset($this->messages[$key]) or ! in_array($message, $this->messages[$key]);
49 50 51 52 53 54 55 56 57 58 59 60 61 62
	}

	/**
	 * Determine if messages exist for a given key.
	 *
	 * @param  string  $key
	 * @return bool
	 */
	public function has($key)
	{
		return $this->first($key) !== '';
	}

	/**
63
	 * Get the first message from the container for a given key.
64
	 *
65
	 * <code>
66 67
	 *		// Echo the first message for the e-mail attribute
	 *		echo $messages->first('email');
68 69
	 *
	 *		// Format the first message for the e-mail attribute
70
	 *		echo $messages->first('email', '<p>:message</p>');
71 72
	 * </code>
	 *
73 74 75 76 77 78 79 80 81 82
	 * @param  string  $key
	 * @param  string  $format
	 * @return string
	 */
	public function first($key, $format = ':message')
	{
		return (count($messages = $this->get($key, $format)) > 0) ? $messages[0] : '';
	}

	/**
83
	 * Get all of the messages from the container for a given key.
84
	 *
85
	 * <code>
86 87
	 *		// Echo all of the messages for the e-mail attribute
	 *		echo $messages->get('email');
88 89
	 *
	 *		// Format all of the messages for the e-mail attribute
90
	 *		echo $messages->get('email', '<p>:message</p>');
91 92
	 * </code>
	 *
93 94 95 96
	 * @param  string  $key
	 * @param  string  $format
	 * @return array
	 */
97
	public function get($key, $format = ':message')
98
	{
99 100 101 102 103 104
		if (array_key_exists($key, $this->messages))
		{
			return $this->format($this->messages[$key], $format);
		}

		return array();
105 106 107
	}

	/**
108
	 * Get all of the messages for every key in the container.
109
	 *
110 111 112 113 114 115 116 117
	 * <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>
	 *
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	 * @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
	 */
140
	protected function format($messages, $format)
141
	{
Taylor Otwell committed
142 143
		$messages = (array) $messages;

144 145 146 147
		foreach ($messages as $key => &$message)
		{
			$message = str_replace(':message', $message, $format);
		}
148 149 150 151 152

		return $messages;
	}

}