AttributeBag.php 2.7 KB

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