FlashBag.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Session\Flash;
  11. /**
  12. * FlashBag flash message container.
  13. *
  14. * @author Drak <drak@zikula.org>
  15. */
  16. class FlashBag implements FlashBagInterface
  17. {
  18. private string $name = 'flashes';
  19. private array $flashes = [];
  20. private string $storageKey;
  21. /**
  22. * @param string $storageKey The key used to store flashes in the session
  23. */
  24. public function __construct(string $storageKey = '_symfony_flashes')
  25. {
  26. $this->storageKey = $storageKey;
  27. }
  28. public function getName(): string
  29. {
  30. return $this->name;
  31. }
  32. public function setName(string $name): void
  33. {
  34. $this->name = $name;
  35. }
  36. public function initialize(array &$flashes): void
  37. {
  38. $this->flashes = &$flashes;
  39. }
  40. public function add(string $type, mixed $message): void
  41. {
  42. $this->flashes[$type][] = $message;
  43. }
  44. public function peek(string $type, array $default = []): array
  45. {
  46. return $this->has($type) ? $this->flashes[$type] : $default;
  47. }
  48. public function peekAll(): array
  49. {
  50. return $this->flashes;
  51. }
  52. public function get(string $type, array $default = []): array
  53. {
  54. if (!$this->has($type)) {
  55. return $default;
  56. }
  57. $return = $this->flashes[$type];
  58. unset($this->flashes[$type]);
  59. return $return;
  60. }
  61. public function all(): array
  62. {
  63. $return = $this->peekAll();
  64. $this->flashes = [];
  65. return $return;
  66. }
  67. public function set(string $type, string|array $messages): void
  68. {
  69. $this->flashes[$type] = (array) $messages;
  70. }
  71. public function setAll(array $messages): void
  72. {
  73. $this->flashes = $messages;
  74. }
  75. public function has(string $type): bool
  76. {
  77. return \array_key_exists($type, $this->flashes) && $this->flashes[$type];
  78. }
  79. public function keys(): array
  80. {
  81. return array_keys($this->flashes);
  82. }
  83. public function getStorageKey(): string
  84. {
  85. return $this->storageKey;
  86. }
  87. public function clear(): mixed
  88. {
  89. return $this->all();
  90. }
  91. }