Overview

Namespaces

  • Composer
    • Autoload
  • Illuminate
    • Support
      • Contracts
      • Facades
  • Laravella
    • Cart
      • Facades
  • None
  • PHP

Classes

  • ClassLoader
  • Collection
  • Fluent
  • Manager
  • MessageBag
  • NamespacedItemResolver
  • Pluralizer
  • SerializableClosure
  • ServiceProvider
  • Str
  • Overview
  • Namespace
  • Class
  • Tree
  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:      * All of the registered messages.
 12:      *
 13:      * @var array
 14:      */
 15:     protected $messages = array();
 16: 
 17:     /**
 18:      * Default format for message output.
 19:      *
 20:      * @var string
 21:      */
 22:     protected $format = ':message';
 23: 
 24:     /**
 25:      * Create a new message bag instance.
 26:      *
 27:      * @param  array  $messages
 28:      * @return void
 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:      * Add a message to the bag.
 40:      *
 41:      * @param  string  $key
 42:      * @param  string  $message
 43:      * @return \Illuminate\Support\MessageBag
 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:      * Merge a new array of messages into the bag.
 57:      *
 58:      * @param  array  $messages
 59:      * @return \Illuminate\Support\MessageBag
 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:      * Determine if a key and message combination already exists.
 70:      *
 71:      * @param  string  $key
 72:      * @param  string  $message
 73:      * @return bool
 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:      * Determine if messages exist for a given key.
 84:      *
 85:      * @param  string  $key
 86:      * @return bool
 87:      */
 88:     public function has($key = null)
 89:     {
 90:         return $this->first($key) !== '';
 91:     }
 92: 
 93:     /**
 94:      * Get the first message from the bag for a given key.
 95:      *
 96:      * @param  string  $key
 97:      * @param  string  $format
 98:      * @return string
 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:      * Get all of the messages from the bag for a given key.
109:      *
110:      * @param  string  $key
111:      * @param  string  $format
112:      * @return array
113:      */
114:     public function get($key, $format = null)
115:     {
116:         $format = $this->checkFormat($format);
117: 
118:         // If the message exists in the container, we will transform it and return
119:         // the message. Otherwise, we'll return an empty array since the entire
120:         // methods is to return back an array of messages in the first place.
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:      * Get all of the messages for every key in the bag.
131:      *
132:      * @param  string  $format
133:      * @return array
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:      * Format an array of messages.
151:      *
152:      * @param  array   $messages
153:      * @param  string  $format
154:      * @param  string  $messageKey
155:      * @return array
156:      */
157:     protected function transform($messages, $format, $messageKey)
158:     {
159:         $messages = (array) $messages;
160: 
161:         // We will simply spin through the given messages and transform each one
162:         // replacing the :message place holder with the real message allowing
163:         // the messages to be easily formatted to each developer's desires.
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:      * Get the appropriate format based on the given format.
176:      *
177:      * @param  string  $format
178:      * @return string
179:      */
180:     protected function checkFormat($format)
181:     {
182:         return ($format === null) ? $this->format : $format;
183:     }
184: 
185:     /**
186:      * Get the raw messages in the container.
187:      *
188:      * @return array
189:      */
190:     public function getMessages()
191:     {
192:         return $this->messages;
193:     }
194: 
195:     /**
196:      * Get the messages for the instance.
197:      *
198:      * @return \Illuminate\Support\MessageBag
199:      */
200:     public function getMessageBag()
201:     {
202:         return $this;
203:     }
204: 
205:     /**
206:      * Get the default message format.
207:      *
208:      * @return string
209:      */
210:     public function getFormat()
211:     {
212:         return $this->format;
213:     }
214: 
215:     /**
216:      * Set the default message format.
217:      *
218:      * @param  string  $format
219:      * @return \Illuminate\Support\MessageBag
220:      */
221:     public function setFormat($format = ':message')
222:     {
223:         $this->format = $format;
224: 
225:         return $this;
226:     }
227: 
228:     /**
229:      * Determine if the message bag has any messages.
230:      *
231:      * @return bool
232:      */
233:     public function isEmpty()
234:     {
235:         return ! $this->any();
236:     }
237: 
238:     /**
239:      * Determine if the message bag has any messages.
240:      *
241:      * @return bool
242:      */
243:     public function any()
244:     {
245:         return $this->count() > 0;
246:     }
247: 
248:     /**
249:      * Get the number of messages in the container.
250:      *
251:      * @return int
252:      */
253:     public function count()
254:     {
255:         return count($this->messages);
256:     }
257: 
258:     /**
259:      * Get the instance as an array.
260:      *
261:      * @return array
262:      */
263:     public function toArray()
264:     {
265:         return $this->getMessages();
266:     }
267: 
268:     /**
269:      * Convert the object to its JSON representation.
270:      *
271:      * @param  int  $options
272:      * @return string
273:      */
274:     public function toJson($options = 0)
275:     {
276:         return json_encode($this->toArray(), $options);
277:     }
278: 
279:     /**
280:      * Convert the message bag to its string representation.
281:      *
282:      * @return string
283:      */
284:     public function __toString()
285:     {
286:         return $this->toJson();
287:     }
288: 
289: }
290: 
cart API documentation generated by ApiGen 2.8.0