| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Support; |
| 4 |
|
| 5 |
use Illuminate\Contracts\Support\Arrayable; |
| 6 |
use Illuminate\Contracts\Support\Jsonable; |
| 7 |
use Illuminate\Contracts\Support\MessageBag as MessageBagContract; |
| 8 |
use Illuminate\Contracts\Support\MessageProvider; |
| 9 |
use JsonSerializable; |
| 10 |
use Stringable; |
| 11 |
|
| 12 |
class MessageBag implements Jsonable, JsonSerializable, MessageBagContract, MessageProvider, Stringable |
| 13 |
{ |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
protected $messages = []; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
protected $format = ':message'; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function __construct(array $messages = []) |
| 34 |
{ |
| 35 |
foreach ($messages as $key => $value) { |
| 36 |
$value = $value instanceof Arrayable ? $value->toArray() : (array) $value; |
| 37 |
|
| 38 |
$this->messages[$key] = array_unique($value); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function keys() |
| 48 |
{ |
| 49 |
return array_keys($this->messages); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public function add($key, $message) |
| 60 |
{ |
| 61 |
if ($this->isUnique($key, $message)) { |
| 62 |
$this->messages[$key][] = $message; |
| 63 |
} |
| 64 |
|
| 65 |
return $this; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function addIf($boolean, $key, $message) |
| 77 |
{ |
| 78 |
return $boolean ? $this->add($key, $message) : $this; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
protected function isUnique($key, $message) |
| 89 |
{ |
| 90 |
$messages = (array) $this->messages; |
| 91 |
|
| 92 |
return ! isset($messages[$key]) || ! in_array($message, $messages[$key]); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function merge($messages) |
| 102 |
{ |
| 103 |
if ($messages instanceof MessageProvider) { |
| 104 |
$messages = $messages->getMessageBag()->getMessages(); |
| 105 |
} |
| 106 |
|
| 107 |
$this->messages = array_merge_recursive($this->messages, $messages); |
| 108 |
|
| 109 |
return $this; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public function has($key) |
| 119 |
{ |
| 120 |
if ($this->isEmpty()) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
if (is_null($key)) { |
| 125 |
return $this->any(); |
| 126 |
} |
| 127 |
|
| 128 |
$keys = is_array($key) ? $key : func_get_args(); |
| 129 |
|
| 130 |
foreach ($keys as $key) { |
| 131 |
if ($this->first($key) === '') { |
| 132 |
return false; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function hasAny($keys = []) |
| 146 |
{ |
| 147 |
if ($this->isEmpty()) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
$keys = is_array($keys) ? $keys : func_get_args(); |
| 152 |
|
| 153 |
foreach ($keys as $key) { |
| 154 |
if ($this->has($key)) { |
| 155 |
return true; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public function missing($key) |
| 169 |
{ |
| 170 |
$keys = is_array($key) ? $key : func_get_args(); |
| 171 |
|
| 172 |
return ! $this->hasAny($keys); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function first($key = null, $format = null) |
| 183 |
{ |
| 184 |
$messages = is_null($key) ? $this->all($format) : $this->get($key, $format); |
| 185 |
|
| 186 |
$firstMessage = Arr::first($messages, null, ''); |
| 187 |
|
| 188 |
return is_array($firstMessage) ? Arr::first($firstMessage) : $firstMessage; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public function get($key, $format = null) |
| 199 |
{ |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
if (array_key_exists($key, $this->messages)) { |
| 204 |
return $this->transform( |
| 205 |
$this->messages[$key], $this->checkFormat($format), $key |
| 206 |
); |
| 207 |
} |
| 208 |
|
| 209 |
if (str_contains($key, '*')) { |
| 210 |
return $this->getMessagesForWildcardKey($key, $format); |
| 211 |
} |
| 212 |
|
| 213 |
return []; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
protected function getMessagesForWildcardKey($key, $format) |
| 224 |
{ |
| 225 |
return (new Collection($this->messages)) |
| 226 |
->filter(fn ($messages, $messageKey) => Str::is($key, $messageKey)) |
| 227 |
->map(function ($messages, $messageKey) use ($format) { |
| 228 |
return $this->transform($messages, $this->checkFormat($format), $messageKey); |
| 229 |
}) |
| 230 |
->all(); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public function all($format = null) |
| 240 |
{ |
| 241 |
$format = $this->checkFormat($format); |
| 242 |
|
| 243 |
$all = []; |
| 244 |
|
| 245 |
foreach ($this->messages as $key => $messages) { |
| 246 |
array_push($all, ...$this->transform($messages, $format, $key)); |
| 247 |
} |
| 248 |
|
| 249 |
return $all; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public function unique($format = null) |
| 259 |
{ |
| 260 |
return array_unique($this->all($format)); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public function forget($key) |
| 270 |
{ |
| 271 |
unset($this->messages[$key]); |
| 272 |
|
| 273 |
return $this; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
protected function transform($messages, $format, $messageKey) |
| 285 |
{ |
| 286 |
if ($format == ':message') { |
| 287 |
return (array) $messages; |
| 288 |
} |
| 289 |
|
| 290 |
return (new Collection((array) $messages)) |
| 291 |
->map(function ($message) use ($format, $messageKey) { |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
return str_replace([':message', ':key'], [$message, $messageKey], $format); |
| 296 |
})->all(); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
protected function checkFormat($format) |
| 306 |
{ |
| 307 |
return $format ?: $this->format; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public function messages() |
| 316 |
{ |
| 317 |
return $this->messages; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
public function getMessages() |
| 326 |
{ |
| 327 |
return $this->messages(); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public function getMessageBag() |
| 336 |
{ |
| 337 |
return $this; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public function getFormat() |
| 346 |
{ |
| 347 |
return $this->format; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
public function setFormat($format = ':message') |
| 357 |
{ |
| 358 |
$this->format = $format; |
| 359 |
|
| 360 |
return $this; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
public function isEmpty() |
| 369 |
{ |
| 370 |
return ! $this->any(); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
public function isNotEmpty() |
| 379 |
{ |
| 380 |
return $this->any(); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
public function any() |
| 389 |
{ |
| 390 |
return $this->count() > 0; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
public function count(): int |
| 399 |
{ |
| 400 |
return count($this->messages, COUNT_RECURSIVE) - count($this->messages); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
public function toArray() |
| 409 |
{ |
| 410 |
return $this->getMessages(); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
public function jsonSerialize(): array |
| 419 |
{ |
| 420 |
return $this->toArray(); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
public function toJson($options = 0) |
| 430 |
{ |
| 431 |
return json_encode($this->jsonSerialize(), $options); |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
public function toPrettyJson(int $options = 0) |
| 441 |
{ |
| 442 |
return $this->toJson(JSON_PRETTY_PRINT | $options); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
public function __toString() |
| 451 |
{ |
| 452 |
return $this->toJson(); |
| 453 |
} |
| 454 |
} |