AttributeBag.php 2.8 KB

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