messages.php 3.39 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.
	 *
Phill Sparks committed
15
	 * @param  array  $messages
16 17 18 19
	 * @return void
	 */
	public function __construct($messages = array())
	{
20
		$this->messages = (array) $messages;
21 22 23 24 25
	}

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

	/**
	 * Determine if messages exist for a given key.
	 *
55 56 57 58 59 60 61 62
	 * <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>
	 *
63 64 65
	 * @param  string  $key
	 * @return bool
	 */
66
	public function has($key = null)
67 68 69 70 71
	{
		return $this->first($key) !== '';
	}

	/**
72
	 * Get the first message from the container for a given key.
73
	 *
74
	 * <code>
75 76 77
	 *		// Echo the first message out of all messages.
	 *		echo $messages->first();
	 *
78 79
	 *		// Echo the first message for the e-mail attribute
	 *		echo $messages->first('email');
80 81
	 *
	 *		// Format the first message for the e-mail attribute
82
	 *		echo $messages->first('email', '<p>:message</p>');
83 84
	 * </code>
	 *
85 86 87 88
	 * @param  string  $key
	 * @param  string  $format
	 * @return string
	 */
89
	public function first($key = null, $format = ':message')
90
	{
91 92 93
		$messages = is_null($key) ? $this->all($format) : $this->get($key, $format);

		return (count($messages) > 0) ? $messages[0] : '';
94 95 96
	}

	/**
97
	 * Get all of the messages from the container for a given key.
98
	 *
99
	 * <code>
100 101
	 *		// Echo all of the messages for the e-mail attribute
	 *		echo $messages->get('email');
102 103
	 *
	 *		// Format all of the messages for the e-mail attribute
104
	 *		echo $messages->get('email', '<p>:message</p>');
105 106
	 * </code>
	 *
107 108 109 110
	 * @param  string  $key
	 * @param  string  $format
	 * @return array
	 */
111
	public function get($key, $format = ':message')
112
	{
113 114 115 116 117 118
		if (array_key_exists($key, $this->messages))
		{
			return $this->format($this->messages[$key], $format);
		}

		return array();
119 120 121
	}

	/**
122
	 * Get all of the messages for every key in the container.
123
	 *
124 125 126 127 128 129 130 131
	 * <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>
	 *
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	 * @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
	 */
154
	protected function format($messages, $format)
155
	{
Taylor Otwell committed
156 157
		$messages = (array) $messages;

158 159 160 161
		foreach ($messages as $key => &$message)
		{
			$message = str_replace(':message', $message, $format);
		}
162 163 164 165 166

		return $messages;
	}

}