CookieCollection.php 7.9 KB

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