1: <?php namespace Laravel;
2:
3: class Messages {
4:
5: /**
6: * All of the registered messages.
7: *
8: * @var array
9: */
10: public $messages;
11:
12: /**
13: * Default format for message output.
14: *
15: * @var string
16: */
17: public $format = ':message';
18:
19: /**
20: * Create a new Messages instance.
21: *
22: * @param array $messages
23: * @return void
24: */
25: public function __construct($messages = array())
26: {
27: $this->messages = (array) $messages;
28: }
29:
30: /**
31: * Add a message to the collector.
32: *
33: * <code>
34: * // Add a message for the e-mail attribute
35: * $messages->add('email', 'The e-mail address is invalid.');
36: * </code>
37: *
38: * @param string $key
39: * @param string $message
40: * @return void
41: */
42: public function add($key, $message)
43: {
44: if ($this->unique($key, $message)) $this->messages[$key][] = $message;
45: }
46:
47: /**
48: * Determine if a key and message combination already exists.
49: *
50: * @param string $key
51: * @param string $message
52: * @return bool
53: */
54: protected function unique($key, $message)
55: {
56: return ! isset($this->messages[$key]) or ! in_array($message, $this->messages[$key]);
57: }
58:
59: /**
60: * Determine if messages exist for a given key.
61: *
62: * <code>
63: * // Is there a message for the e-mail attribute
64: * return $messages->has('email');
65: *
66: * // Is there a message for the any attribute
67: * echo $messages->has();
68: * </code>
69: *
70: * @param string $key
71: * @return bool
72: */
73: public function has($key = null)
74: {
75: return $this->first($key) !== '';
76: }
77:
78: /**
79: * Set the default message format for output.
80: *
81: * <code>
82: * // Apply a new default format.
83: * $messages->format('email', '<p>this is my :message</p>');
84: * </code>
85: *
86: * @param string $format
87: */
88: public function format($format = ':message')
89: {
90: $this->format = $format;
91: }
92:
93: /**
94: * Get the first message from the container for a given key.
95: *
96: * <code>
97: * // Echo the first message out of all messages.
98: * echo $messages->first();
99: *
100: * // Echo the first message for the e-mail attribute
101: * echo $messages->first('email');
102: *
103: * // Format the first message for the e-mail attribute
104: * echo $messages->first('email', '<p>:message</p>');
105: * </code>
106: *
107: * @param string $key
108: * @param string $format
109: * @return string
110: */
111: public function first($key = null, $format = null)
112: {
113: $format = ($format === null) ? $this->format : $format;
114:
115: $messages = is_null($key) ? $this->all($format) : $this->get($key, $format);
116:
117: 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: * // Echo all of the messages for the e-mail attribute
125: * 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: * </code>
130: *
131: * @param string $key
132: * @param string $format
133: * @return array
134: */
135: public function get($key, $format = null)
136: {
137: $format = ($format === null) ? $this->format : $format;
138:
139: if (array_key_exists($key, $this->messages))
140: {
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: * <code>
151: * // Get all of the messages in the collector
152: * $all = $messages->all();
153: *
154: * // Format all of the messages in the collector
155: * $all = $messages->all('<p>:message</p>');
156: * </code>
157: *
158: * @param string $format
159: * @return array
160: */
161: public function all($format = null)
162: {
163: $format = ($format === null) ? $this->format : $format;
164:
165: $all = array();
166:
167: foreach ($this->messages as $messages)
168: {
169: $all = array_merge($all, $this->transform($messages, $format));
170: }
171:
172: return $all;
173: }
174:
175: /**
176: * Format an array of messages.
177: *
178: * @param array $messages
179: * @param string $format
180: * @return array
181: */
182: protected function transform($messages, $format)
183: {
184: $messages = (array) $messages;
185:
186: foreach ($messages as $key => &$message)
187: {
188: $message = str_replace(':message', $message, $format);
189: }
190:
191: return $messages;
192: }
193:
194: }