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

	/**
	 * Create a new Messages instance.
	 *
15
	 * The Messages class provides a convenient wrapper around an array of strings.
16
	 *
17 18 19 20
	 * @return void
	 */
	public function __construct($messages = array())
	{
21
		$this->messages = (array) $messages;
22 23 24 25 26
	}

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

	/**
	 * 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.
	 *
67 68 69 70 71 72 73 74
	 * <code>
	 *		// 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', '<p>:message</p>');
	 * </code>
	 *
75 76 77 78 79 80 81 82 83 84 85 86
	 * @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.
	 *
87 88 89 90 91 92 93 94
	 * <code>
	 *		// 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', '<p>:message</p>');
	 * </code>
	 *
95 96 97 98
	 * @param  string  $key
	 * @param  string  $format
	 * @return array
	 */
99
	public function get($key, $format = ':message')
100
	{
101 102 103 104 105 106
		if (array_key_exists($key, $this->messages))
		{
			return $this->format($this->messages[$key], $format);
		}

		return array();
107 108 109
	}

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

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

		return $messages;
	}

}