give
/
vendor
/
vendor-prefixed
/
symfony
/
http-foundation
/
Session
/
Flash
/
AutoExpireFlashBag.php
AutoExpireFlashBag.php in GiveWP – Donation Plugin and Fundraising Platform 4.15.1, at vendor/vendor-prefixed/symfony/http-foundation/Session/Flash/AutoExpireFlashBag.php
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <[email protected]> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Give\Vendors\Symfony\Component\HttpFoundation\Session\Flash; |
| 13 | |
| 14 | /** |
| 15 | * AutoExpireFlashBag flash message container. |
| 16 | * |
| 17 | * @author Drak <[email protected]> |
| 18 | */ |
| 19 | class AutoExpireFlashBag implements FlashBagInterface |
| 20 | { |
| 21 | private $name = 'flashes'; |
| 22 | private $flashes = ['display' => [], 'new' => []]; |
| 23 | private $storageKey; |
| 24 | |
| 25 | /** |
| 26 | * @param string $storageKey The key used to store flashes in the session |
| 27 | */ |
| 28 | public function __construct(string $storageKey = '_symfony_flashes') |
| 29 | { |
| 30 | $this->storageKey = $storageKey; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * {@inheritdoc} |
| 35 | */ |
| 36 | public function getName() |
| 37 | { |
| 38 | return $this->name; |
| 39 | } |
| 40 | |
| 41 | public function setName(string $name) |
| 42 | { |
| 43 | $this->name = $name; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * {@inheritdoc} |
| 48 | */ |
| 49 | public function initialize(array &$flashes) |
| 50 | { |
| 51 | $this->flashes = &$flashes; |
| 52 | |
| 53 | // The logic: messages from the last request will be stored in new, so we move them to previous |
| 54 | // This request we will show what is in 'display'. What is placed into 'new' this time round will |
| 55 | // be moved to display next time round. |
| 56 | $this->flashes['display'] = \array_key_exists('new', $this->flashes) ? $this->flashes['new'] : []; |
| 57 | $this->flashes['new'] = []; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * {@inheritdoc} |
| 62 | */ |
| 63 | public function add(string $type, $message) |
| 64 | { |
| 65 | $this->flashes['new'][$type][] = $message; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * {@inheritdoc} |
| 70 | */ |
| 71 | public function peek(string $type, array $default = []) |
| 72 | { |
| 73 | return $this->has($type) ? $this->flashes['display'][$type] : $default; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * {@inheritdoc} |
| 78 | */ |
| 79 | public function peekAll() |
| 80 | { |
| 81 | return \array_key_exists('display', $this->flashes) ? $this->flashes['display'] : []; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * {@inheritdoc} |
| 86 | */ |
| 87 | public function get(string $type, array $default = []) |
| 88 | { |
| 89 | $return = $default; |
| 90 | |
| 91 | if (!$this->has($type)) { |
| 92 | return $return; |
| 93 | } |
| 94 | |
| 95 | if (isset($this->flashes['display'][$type])) { |
| 96 | $return = $this->flashes['display'][$type]; |
| 97 | unset($this->flashes['display'][$type]); |
| 98 | } |
| 99 | |
| 100 | return $return; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * {@inheritdoc} |
| 105 | */ |
| 106 | public function all() |
| 107 | { |
| 108 | $return = $this->flashes['display']; |
| 109 | $this->flashes['display'] = []; |
| 110 | |
| 111 | return $return; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * {@inheritdoc} |
| 116 | */ |
| 117 | public function setAll(array $messages) |
| 118 | { |
| 119 | $this->flashes['new'] = $messages; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * {@inheritdoc} |
| 124 | */ |
| 125 | public function set(string $type, $messages) |
| 126 | { |
| 127 | $this->flashes['new'][$type] = (array) $messages; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * {@inheritdoc} |
| 132 | */ |
| 133 | public function has(string $type) |
| 134 | { |
| 135 | return \array_key_exists($type, $this->flashes['display']) && $this->flashes['display'][$type]; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * {@inheritdoc} |
| 140 | */ |
| 141 | public function keys() |
| 142 | { |
| 143 | return array_keys($this->flashes['display']); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * {@inheritdoc} |
| 148 | */ |
| 149 | public function getStorageKey() |
| 150 | { |
| 151 | return $this->storageKey; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * {@inheritdoc} |
| 156 | */ |
| 157 | public function clear() |
| 158 | { |
| 159 | return $this->all(); |
| 160 | } |
| 161 | } |
| 162 |