AttributeBag.php 2.6 KB

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