AttributeBag.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Attribute;
  11. /**
  12. * This class relates to session attribute storage.
  13. *
  14. * @implements \IteratorAggregate<string, mixed>
  15. */
  16. class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Countable
  17. {
  18. protected array $attributes = [];
  19. private string $name = 'attributes';
  20. private string $storageKey;
  21. /**
  22. * @param string $storageKey The key used to store attributes in the session
  23. */
  24. public function __construct(string $storageKey = '_sf2_attributes')
  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 &$attributes): void
  37. {
  38. $this->attributes = &$attributes;
  39. }
  40. public function getStorageKey(): string
  41. {
  42. return $this->storageKey;
  43. }
  44. public function has(string $name): bool
  45. {
  46. return \array_key_exists($name, $this->attributes);
  47. }
  48. public function get(string $name, mixed $default = null): mixed
  49. {
  50. return \array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
  51. }
  52. public function set(string $name, mixed $value): void
  53. {
  54. $this->attributes[$name] = $value;
  55. }
  56. public function all(): array
  57. {
  58. return $this->attributes;
  59. }
  60. public function replace(array $attributes): void
  61. {
  62. $this->attributes = [];
  63. foreach ($attributes as $key => $value) {
  64. $this->set($key, $value);
  65. }
  66. }
  67. public function remove(string $name): mixed
  68. {
  69. $retval = null;
  70. if (\array_key_exists($name, $this->attributes)) {
  71. $retval = $this->attributes[$name];
  72. unset($this->attributes[$name]);
  73. }
  74. return $retval;
  75. }
  76. public function clear(): mixed
  77. {
  78. $return = $this->attributes;
  79. $this->attributes = [];
  80. return $return;
  81. }
  82. /**
  83. * Returns an iterator for attributes.
  84. *
  85. * @return \ArrayIterator<string, mixed>
  86. */
  87. public function getIterator(): \ArrayIterator
  88. {
  89. return new \ArrayIterator($this->attributes);
  90. }
  91. /**
  92. * Returns the number of attributes.
  93. */
  94. public function count(): int
  95. {
  96. return \count($this->attributes);
  97. }
  98. }