-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageBag.php
283 lines (241 loc) · 6.43 KB
/
MessageBag.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/**
* Qubus\Validation
*
* @link https://github.com/QubusPHP/validation
* @copyright 2020
* @author Joshua Parker <[email protected]>
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Qubus\Validation;
use Countable;
use JsonSerializable;
use function array_merge;
use function array_merge_recursive;
use function count;
use function in_array;
use function json_encode;
use function Qubus\Support\Helpers\array_key_exists__;
use function str_replace;
use const COUNT_RECURSIVE;
class MessageBag implements Countable, JsonSerializable
{
/**
* All the registered messages.
*
* @var array $messages
*/
protected array $messages = [];
/**
* Default format for message output.
*/
protected string $format = ':message';
/**
* Create a new message bag instance.
*
* @param array $messages
* @return void
*/
public function __construct(array $messages = [])
{
foreach ($messages as $key => $value) {
$this->messages[$key] = (array) $value;
}
}
/**
* Add a message to the bag.
*
* @param string $key
* @param string $message
* @return self
*/
public function add(string $key, string $message): self
{
if ($this->isUnique($key, $message)) {
$this->messages[$key][] = $message;
}
return $this;
}
/**
* Merge a new array of messages into the bag.
*
* @param array $messages
* @return self
*/
public function merge(array $messages): self
{
$this->messages = array_merge_recursive($this->messages, $messages);
return $this;
}
/**
* Determine if a key and message combination already exists.
*/
protected function isUnique(string $key, string $message): bool
{
$messages = (array) $this->messages;
return ! isset($messages[$key]) || ! in_array($message, $messages[$key], true);
}
/**
* Determine if messages exist for a given key.
*/
public function has(?string $key = null): bool
{
return $this->first($key) !== '';
}
/**
* Get the first message from the bag for a given key.
*/
public function first(?string $key = null, ?string $format = null): string
{
$messages = null === $key ? $this->all($format) : $this->get($key, $format);
return count($messages) > 0 ? $messages[0] : '';
}
/**
* Get all the messages from the bag for a given key.
*
* @param string|null $key
* @param string|null $format
* @return array
*/
public function get(?string $key = null, ?string $format = null): array
{
$format = $this->checkFormat($format);
// If the message exists in the container, we will transform it and return
// the message. Otherwise, we'll return an empty array since the entire
// methods is to return back an array of messages in the first place.
if (array_key_exists__($key, $this->messages)) {
return $this->transform($this->messages[$key], $format, $key);
}
return [];
}
/**
* Get all the messages for every key in the bag.
*
* @param string|null $format
* @return array
*/
public function all(?string $format = null): array
{
$format = $this->checkFormat($format);
$all = [];
foreach ($this->messages as $key => $messages) {
$all = array_merge($all, $this->transform($messages, $format, $key));
}
return $all;
}
/**
* Format an array of messages.
*
* @param array $messages
* @param string $format
* @param string $messageKey
* @return array
*/
protected function transform(array $messages, string $format, string $messageKey): array
{
$messages = (array) $messages;
// We will simply spin through the given messages and transform each one
// replacing the :message place holder with the real message allowing
// the messages to be easily formatted to each developer's desires.
foreach ($messages as $key => &$message) {
$replace = [':message', ':key'];
$message = str_replace($replace, [$message, $messageKey], $format);
}
return $messages;
}
/**
* Get the appropriate format based on the given format.
*/
protected function checkFormat(?string $format): ?string
{
return $format ?? $this->format;
}
/**
* Get the raw messages in the container.
*
* @return array
*/
public function getMessages(): array
{
return $this->messages;
}
/**
* Get the messages for the instance.
*/
public function getMessageBag(): MessageBag
{
return $this;
}
/**
* Get the default message format.
*/
public function getFormat(): string
{
return $this->format;
}
/**
* Set the default message format.
*
* @param string $format
* @return MessageBag
*/
public function setFormat(string $format = ':message'): MessageBag
{
$this->format = $format;
return $this;
}
/**
* Determine if the message bag has any messages.
*/
public function isEmpty(): bool
{
return ! $this->any();
}
/**
* Determine if the message bag has any messages.
*/
public function any(): bool
{
return $this->count() > 0;
}
/**
* Get the number of messages in the container.
*/
public function count(): int
{
return count($this->messages, COUNT_RECURSIVE) - count($this->messages);
}
/**
* Get the instance as an array.
*
* @return array
*/
public function toArray(): array
{
return $this->getMessages();
}
/**
* Convert the object into something JSON serializable.
*
* @return array
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
/**
* Convert the object to its JSON representation.
*/
public function toJson(int $options = 0): string
{
return json_encode($this->toArray(), $options);
}
/**
* Convert the message bag to its string representation.
*/
public function __toString(): string
{
return $this->toJson();
}
}