CookieCollection.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\web;
  8. use Yii;
  9. use ArrayIterator;
  10. use yii\base\InvalidCallException;
  11. use yii\base\Object;
  12. /**
  13. * CookieCollection maintains the cookies available in the current request.
  14. *
  15. * @property integer $count The number of cookies in the collection. This property is read-only.
  16. * @property ArrayIterator $iterator An iterator for traversing the cookies in the collection. This property
  17. * is read-only.
  18. *
  19. * @author Qiang Xue <[email protected]>
  20. * @since 2.0
  21. */
  22. class CookieCollection extends Object implements \IteratorAggregate, \ArrayAccess, \Countable
  23. {
  24. /**
  25. * @var boolean whether this collection is read only.
  26. */
  27. public $readOnly = false;
  28. /**
  29. * @var Cookie[] the cookies in this collection (indexed by the cookie names)
  30. */
  31. private $_cookies = [];
  32. /**
  33. * Constructor.
  34. * @param array $cookies the cookies that this collection initially contains. This should be
  35. * an array of name-value pairs.s
  36. * @param array $config name-value pairs that will be used to initialize the object properties
  37. */
  38. public function __construct($cookies = [], $config = [])
  39. {
  40. $this->_cookies = $cookies;
  41. parent::__construct($config);
  42. }
  43. /**
  44. * Returns an iterator for traversing the cookies in the collection.
  45. * This method is required by the SPL interface `IteratorAggregate`.
  46. * It will be implicitly called when you use `foreach` to traverse the collection.
  47. * @return ArrayIterator an iterator for traversing the cookies in the collection.
  48. */
  49. public function getIterator()
  50. {
  51. return new ArrayIterator($this->_cookies);
  52. }
  53. /**
  54. * Returns the number of cookies in the collection.
  55. * This method is required by the SPL `Countable` interface.
  56. * It will be implicitly called when you use `count($collection)`.
  57. * @return integer the number of cookies in the collection.
  58. */
  59. public function count()
  60. {
  61. return $this->getCount();
  62. }
  63. /**
  64. * Returns the number of cookies in the collection.
  65. * @return integer the number of cookies in the collection.
  66. */
  67. public function getCount()
  68. {
  69. return count($this->_cookies);
  70. }
  71. /**
  72. * Returns the cookie with the specified name.
  73. * @param string $name the cookie name
  74. * @return Cookie the cookie with the specified name. Null if the named cookie does not exist.
  75. * @see getValue()
  76. */
  77. public function get($name)
  78. {
  79. return isset($this->_cookies[$name]) ? $this->_cookies[$name] : null;
  80. }
  81. /**
  82. * Returns the value of the named cookie.
  83. * @param string $name the cookie name
  84. * @param mixed $defaultValue the value that should be returned when the named cookie does not exist.
  85. * @return mixed the value of the named cookie.
  86. * @see get()
  87. */
  88. public function getValue($name, $defaultValue = null)
  89. {
  90. return isset($this->_cookies[$name]) ? $this->_cookies[$name]->value : $defaultValue;
  91. }
  92. /**
  93. * Returns whether there is a cookie with the specified name.
  94. * @param string $name the cookie name
  95. * @return boolean whether the named cookie exists
  96. */
  97. public function has($name)
  98. {
  99. return isset($this->_cookies[$name]);
  100. }
  101. /**
  102. * Adds a cookie to the collection.
  103. * If there is already a cookie with the same name in the collection, it will be removed first.
  104. * @param Cookie $cookie the cookie to be added
  105. * @throws InvalidCallException if the cookie collection is read only
  106. */
  107. public function add($cookie)
  108. {
  109. if ($this->readOnly) {
  110. throw new InvalidCallException('The cookie collection is read only.');
  111. }
  112. $this->_cookies[$cookie->name] = $cookie;
  113. }
  114. /**
  115. * Removes a cookie.
  116. * If `$removeFromBrowser` is true, the cookie will be removed from the browser.
  117. * In this case, a cookie with outdated expiry will be added to the collection.
  118. * @param Cookie|string $cookie the cookie object or the name of the cookie to be removed.
  119. * @param boolean $removeFromBrowser whether to remove the cookie from browser
  120. * @throws InvalidCallException if the cookie collection is read only
  121. */
  122. public function remove($cookie, $removeFromBrowser = true)
  123. {
  124. if ($this->readOnly) {
  125. throw new InvalidCallException('The cookie collection is read only.');
  126. }
  127. if ($cookie instanceof Cookie) {
  128. $cookie->expire = 1;
  129. $cookie->value = '';
  130. } else {
  131. $cookie = new Cookie([
  132. 'name' => $cookie,
  133. 'expire' => 1,
  134. ]);
  135. }
  136. if ($removeFromBrowser) {
  137. $this->_cookies[$cookie->name] = $cookie;
  138. } else {
  139. unset($this->_cookies[$cookie->name]);
  140. }
  141. }
  142. /**
  143. * Removes all cookies.
  144. * @throws InvalidCallException if the cookie collection is read only
  145. */
  146. public function removeAll()
  147. {
  148. if ($this->readOnly) {
  149. throw new InvalidCallException('The cookie collection is read only.');
  150. }
  151. $this->_cookies = [];
  152. }
  153. /**
  154. * Returns the collection as a PHP array.
  155. * @return array the array representation of the collection.
  156. * The array keys are cookie names, and the array values are the corresponding
  157. * cookie objects.
  158. */
  159. public function toArray()
  160. {
  161. return $this->_cookies;
  162. }
  163. /**
  164. * Returns whether there is a cookie with the specified name.
  165. * This method is required by the SPL interface `ArrayAccess`.
  166. * It is implicitly called when you use something like `isset($collection[$name])`.
  167. * @param string $name the cookie name
  168. * @return boolean whether the named cookie exists
  169. */
  170. public function offsetExists($name)
  171. {
  172. return $this->has($name);
  173. }
  174. /**
  175. * Returns the cookie with the specified name.
  176. * This method is required by the SPL interface `ArrayAccess`.
  177. * It is implicitly called when you use something like `$cookie = $collection[$name];`.
  178. * This is equivalent to [[get()]].
  179. * @param string $name the cookie name
  180. * @return Cookie the cookie with the specified name, null if the named cookie does not exist.
  181. */
  182. public function offsetGet($name)
  183. {
  184. return $this->get($name);
  185. }
  186. /**
  187. * Adds the cookie to the collection.
  188. * This method is required by the SPL interface `ArrayAccess`.
  189. * It is implicitly called when you use something like `$collection[$name] = $cookie;`.
  190. * This is equivalent to [[add()]].
  191. * @param string $name the cookie name
  192. * @param Cookie $cookie the cookie to be added
  193. */
  194. public function offsetSet($name, $cookie)
  195. {
  196. $this->add($cookie);
  197. }
  198. /**
  199. * Removes the named cookie.
  200. * This method is required by the SPL interface `ArrayAccess`.
  201. * It is implicitly called when you use something like `unset($collection[$name])`.
  202. * This is equivalent to [[remove()]].
  203. * @param string $name the cookie name
  204. */
  205. public function offsetUnset($name)
  206. {
  207. $this->remove($name);
  208. }
  209. }