messages.php 3.94 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
	 *
	 * @var array
	 */
	public $messages;

	/**
13 14 15 16 17 18 19
	 * Default format for message output.
	 *
	 * @var string
	 */	
	public $format = ':message';

	/**
20 21
	 * Create a new Messages instance.
	 *
Phill Sparks committed
22
	 * @param  array  $messages
23 24 25 26
	 * @return void
	 */
	public function __construct($messages = array())
	{
27
		$this->messages = (array) $messages;
28 29 30 31 32
	}

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

	/**
	 * Determine if messages exist for a given key.
	 *
62 63 64 65 66 67 68 69
	 * <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>
	 *
70 71 72
	 * @param  string  $key
	 * @return bool
	 */
73
	public function has($key = null)
74 75 76 77 78
	{
		return $this->first($key) !== '';
	}

	/**
79 80 81 82
	 * Set the default message format for output.
	 *
	 * <code>
	 *		// Apply a new default format.
83
	 *		$messages->format('email', '<p>this is my :message</p>');
84 85 86 87
	 * </code>
	 *
	 * @param  string  $format
	 */
88
	public function format($format = ':message')
89 90 91 92 93
	{
		$this->format = $format;
	}

	/**
94
	 * Get the first message from the container for a given key.
95
	 *
96
	 * <code>
97 98 99
	 *		// Echo the first message out of all messages.
	 *		echo $messages->first();
	 *
100 101
	 *		// Echo the first message for the e-mail attribute
	 *		echo $messages->first('email');
102 103
	 *
	 *		// Format the first message for the e-mail attribute
104
	 *		echo $messages->first('email', '<p>:message</p>');
105 106
	 * </code>
	 *
107 108 109 110
	 * @param  string  $key
	 * @param  string  $format
	 * @return string
	 */
111
	public function first($key = null, $format = null)
112
	{
113
		$format = ($format === null) ? $this->format : $format;
114

115 116 117
		$messages = is_null($key) ? $this->all($format) : $this->get($key, $format);

		return (count($messages) > 0) ? $messages[0] : '';
118 119 120
	}

	/**
121
	 * Get all of the messages from the container for a given key.
122
	 *
123
	 * <code>
124 125
	 *		// Echo all of the messages for the e-mail attribute
	 *		echo $messages->get('email');
126 127
	 *
	 *		// Format all of the messages for the e-mail attribute
128
	 *		echo $messages->get('email', '<p>:message</p>');
129 130
	 * </code>
	 *
131 132 133 134
	 * @param  string  $key
	 * @param  string  $format
	 * @return array
	 */
135
	public function get($key, $format = null)
136
	{
137
		$format = ($format === null) ? $this->format : $format;
138

139 140
		if (array_key_exists($key, $this->messages))
		{
141
			return $this->transform($this->messages[$key], $format);
142 143 144
		}

		return array();
145 146 147
	}

	/**
148
	 * Get all of the messages for every key in the container.
149
	 *
150 151 152 153 154 155 156 157
	 * <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>
	 *
158 159 160
	 * @param  string  $format
	 * @return array
	 */
161
	public function all($format = null)
162
	{
163
		$format = ($format === null) ? $this->format : $format;
164

165 166 167 168
		$all = array();

		foreach ($this->messages as $messages)
		{
169
			$all = array_merge($all, $this->transform($messages, $format));
170 171 172 173 174 175 176 177 178 179 180 181
		}

		return $all;
	}

	/**
	 * Format an array of messages.
	 *
	 * @param  array   $messages
	 * @param  string  $format
	 * @return array
	 */
182
	protected function transform($messages, $format)
183
	{
Taylor Otwell committed
184 185
		$messages = (array) $messages;

186 187 188 189
		foreach ($messages as $key => &$message)
		{
			$message = str_replace(':message', $message, $format);
		}
190 191 192 193 194

		return $messages;
	}

}