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