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