AttributeBag.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. private $name = 'attributes';
  19. private $storageKey;
  20. protected $attributes = [];
  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. /**
  29. * {@inheritdoc}
  30. */
  31. public function getName()
  32. {
  33. return $this->name;
  34. }
  35. public function setName(string $name)
  36. {
  37. $this->name = $name;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function initialize(array &$attributes)
  43. {
  44. $this->attributes = &$attributes;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function getStorageKey()
  50. {
  51. return $this->storageKey;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function has(string $name)
  57. {
  58. return \array_key_exists($name, $this->attributes);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function get(string $name, $default = null)
  64. {
  65. return \array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function set(string $name, $value)
  71. {
  72. $this->attributes[$name] = $value;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function all()
  78. {
  79. return $this->attributes;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function replace(array $attributes)
  85. {
  86. $this->attributes = [];
  87. foreach ($attributes as $key => $value) {
  88. $this->set($key, $value);
  89. }
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function remove(string $name)
  95. {
  96. $retval = null;
  97. if (\array_key_exists($name, $this->attributes)) {
  98. $retval = $this->attributes[$name];
  99. unset($this->attributes[$name]);
  100. }
  101. return $retval;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function clear()
  107. {
  108. $return = $this->attributes;
  109. $this->attributes = [];
  110. return $return;
  111. }
  112. /**
  113. * Returns an iterator for attributes.
  114. *
  115. * @return \ArrayIterator<string, mixed>
  116. */
  117. #[\ReturnTypeWillChange]
  118. public function getIterator()
  119. {
  120. return new \ArrayIterator($this->attributes);
  121. }
  122. /**
  123. * Returns the number of attributes.
  124. *
  125. * @return int
  126. */
  127. #[\ReturnTypeWillChange]
  128. public function count()
  129. {
  130. return \count($this->attributes);
  131. }
  132. }