forked from laravel/laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.php
113 lines (99 loc) · 2.03 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
<?php namespace System;
class Messages {
/**
* All of the messages.
*
* @var array
*/
public $messages;
/**
* Create a new Messages instance.
*
* @return void
*/
public function __construct($messages = array())
{
$this->messages = $messages;
}
/**
* Add a message to the collector.
*
* Duplicate messages will not be added.
*
* @param string $key
* @param string $message
* @return void
*/
public function add($key, $message)
{
if ( ! isset($this->messages[$key]) or array_search($message, $this->messages[$key]) === false)
{
$this->messages[$key][] = $message;
}
}
/**
* Determine if messages exist for a given key.
*
* @param string $key
* @return bool
*/
public function has($key)
{
return $this->first($key) !== '';
}
/**
* Get the first message for a given key.
*
* @param string $key
* @param string $format
* @return string
*/
public function first($key, $format = ':message')
{
return (count($messages = $this->get($key, $format)) > 0) ? $messages[0] : '';
}
/**
* Get all of the messages for a key.
*
* If no key is specified, all of the messages will be returned.
*
* @param string $key
* @param string $format
* @return array
*/
public function get($key = null, $format = ':message')
{
if (is_null($key)) return $this->all($format);
return (array_key_exists($key, $this->messages)) ? $this->format($this->messages[$key], $format) : array();
}
/**
* Get all of the messages.
*
* @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
*/
private function format($messages, $format)
{
foreach ($messages as $key => &$message)
{
$message = str_replace(':message', $message, $format);
}
return $messages;
}
}