1: <?php namespace Illuminate\Support;
2:
3: use Countable;
4: use Illuminate\Support\Contracts\ArrayableInterface;
5: use Illuminate\Support\Contracts\JsonableInterface;
6: use Illuminate\Support\Contracts\MessageProviderInterface;
7:
8: class MessageBag implements ArrayableInterface, Countable, JsonableInterface, MessageProviderInterface {
9:
10: 11: 12: 13: 14:
15: protected $messages = array();
16:
17: 18: 19: 20: 21:
22: protected $format = ':message';
23:
24: 25: 26: 27: 28: 29:
30: public function __construct(array $messages = array())
31: {
32: foreach ($messages as $key => $value)
33: {
34: $this->messages[$key] = (array) $value;
35: }
36: }
37:
38: 39: 40: 41: 42: 43: 44:
45: public function add($key, $message)
46: {
47: if ($this->isUnique($key, $message))
48: {
49: $this->messages[$key][] = $message;
50: }
51:
52: return $this;
53: }
54:
55: 56: 57: 58: 59: 60:
61: public function merge(array $messages)
62: {
63: $this->messages = array_merge_recursive($this->messages, $messages);
64:
65: return $this;
66: }
67:
68: 69: 70: 71: 72: 73: 74:
75: protected function isUnique($key, $message)
76: {
77: $messages = (array) $this->messages;
78:
79: return ! isset($messages[$key]) or ! in_array($message, $messages[$key]);
80: }
81:
82: 83: 84: 85: 86: 87:
88: public function has($key = null)
89: {
90: return $this->first($key) !== '';
91: }
92:
93: 94: 95: 96: 97: 98: 99:
100: public function first($key = null, $format = null)
101: {
102: $messages = is_null($key) ? $this->all($format) : $this->get($key, $format);
103:
104: return (count($messages) > 0) ? $messages[0] : '';
105: }
106:
107: 108: 109: 110: 111: 112: 113:
114: public function get($key, $format = null)
115: {
116: $format = $this->checkFormat($format);
117:
118:
119:
120:
121: if (array_key_exists($key, $this->messages))
122: {
123: return $this->transform($this->messages[$key], $format, $key);
124: }
125:
126: return array();
127: }
128:
129: 130: 131: 132: 133: 134:
135: public function all($format = null)
136: {
137: $format = $this->checkFormat($format);
138:
139: $all = array();
140:
141: foreach ($this->messages as $key => $messages)
142: {
143: $all = array_merge($all, $this->transform($messages, $format, $key));
144: }
145:
146: return $all;
147: }
148:
149: 150: 151: 152: 153: 154: 155: 156:
157: protected function transform($messages, $format, $messageKey)
158: {
159: $messages = (array) $messages;
160:
161:
162:
163:
164: foreach ($messages as $key => &$message)
165: {
166: $replace = array(':message', ':key');
167:
168: $message = str_replace($replace, array($message, $messageKey), $format);
169: }
170:
171: return $messages;
172: }
173:
174: 175: 176: 177: 178: 179:
180: protected function checkFormat($format)
181: {
182: return ($format === null) ? $this->format : $format;
183: }
184:
185: 186: 187: 188: 189:
190: public function getMessages()
191: {
192: return $this->messages;
193: }
194:
195: 196: 197: 198: 199:
200: public function getMessageBag()
201: {
202: return $this;
203: }
204:
205: 206: 207: 208: 209:
210: public function getFormat()
211: {
212: return $this->format;
213: }
214:
215: 216: 217: 218: 219: 220:
221: public function setFormat($format = ':message')
222: {
223: $this->format = $format;
224:
225: return $this;
226: }
227:
228: 229: 230: 231: 232:
233: public function isEmpty()
234: {
235: return ! $this->any();
236: }
237:
238: 239: 240: 241: 242:
243: public function any()
244: {
245: return $this->count() > 0;
246: }
247:
248: 249: 250: 251: 252:
253: public function count()
254: {
255: return count($this->messages);
256: }
257:
258: 259: 260: 261: 262:
263: public function toArray()
264: {
265: return $this->getMessages();
266: }
267:
268: 269: 270: 271: 272: 273:
274: public function toJson($options = 0)
275: {
276: return json_encode($this->toArray(), $options);
277: }
278:
279: 280: 281: 282: 283:
284: public function __toString()
285: {
286: return $this->toJson();
287: }
288:
289: }
290: