AttributeBag.php 2.9 KB

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