CookieCollection.php 6.51 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7 8 9
 * @license http://www.yiiframework.com/license/
 */

namespace yii\web;

Qiang Xue committed
10
use Yii;
11
use ArrayIterator;
Qiang Xue committed
12 13
use yii\base\InvalidCallException;
use yii\base\Object;
Qiang Xue committed
14 15

/**
16
 * CookieCollection maintains the cookies available in the current request.
Qiang Xue committed
17
 *
18 19 20
 * @property integer $count The number of cookies in the collection. This property is read-only.
 * @property ArrayIterator $iterator An iterator for traversing the cookies in the collection. This property
 * is read-only.
Qiang Xue committed
21 22 23 24
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
25
class CookieCollection extends Object implements \IteratorAggregate, \ArrayAccess, \Countable
Qiang Xue committed
26
{
Qiang Xue committed
27
	/**
Qiang Xue committed
28
	 * @var boolean whether this collection is read only.
Qiang Xue committed
29
	 */
Qiang Xue committed
30
	public $readOnly = false;
Qiang Xue committed
31

Qiang Xue committed
32
	/**
33
	 * @var Cookie[] the cookies in this collection (indexed by the cookie names)
Qiang Xue committed
34
	 */
Alexander Makarov committed
35
	private $_cookies = [];
Qiang Xue committed
36 37 38

	/**
	 * Constructor.
Qiang Xue committed
39 40
	 * @param array $cookies the cookies that this collection initially contains. This should be
	 * an array of name-value pairs.s
Qiang Xue committed
41 42
	 * @param array $config name-value pairs that will be used to initialize the object properties
	 */
Alexander Makarov committed
43
	public function __construct($cookies = [], $config = [])
Qiang Xue committed
44
	{
Qiang Xue committed
45
		$this->_cookies = $cookies;
Qiang Xue committed
46 47 48 49
		parent::__construct($config);
	}

	/**
50
	 * Returns an iterator for traversing the cookies in the collection.
Qiang Xue committed
51
	 * This method is required by the SPL interface `IteratorAggregate`.
52
	 * It will be implicitly called when you use `foreach` to traverse the collection.
53
	 * @return ArrayIterator an iterator for traversing the cookies in the collection.
Qiang Xue committed
54 55 56
	 */
	public function getIterator()
	{
57
		return new ArrayIterator($this->_cookies);
Qiang Xue committed
58 59 60
	}

	/**
61
	 * Returns the number of cookies in the collection.
Qiang Xue committed
62
	 * This method is required by the SPL `Countable` interface.
63 64
	 * It will be implicitly called when you use `count($collection)`.
	 * @return integer the number of cookies in the collection.
Qiang Xue committed
65 66 67 68 69 70 71
	 */
	public function count()
	{
		return $this->getCount();
	}

	/**
72 73
	 * Returns the number of cookies in the collection.
	 * @return integer the number of cookies in the collection.
Qiang Xue committed
74 75 76 77 78 79 80
	 */
	public function getCount()
	{
		return count($this->_cookies);
	}

	/**
81 82 83 84
	 * Returns the cookie with the specified name.
	 * @param string $name the cookie name
	 * @return Cookie the cookie with the specified name. Null if the named cookie does not exist.
	 * @see getValue()
Qiang Xue committed
85
	 */
86
	public function get($name)
Qiang Xue committed
87
	{
88
		return isset($this->_cookies[$name]) ? $this->_cookies[$name] : null;
Qiang Xue committed
89 90 91
	}

	/**
92 93 94 95 96
	 * Returns the value of the named cookie.
	 * @param string $name the cookie name
	 * @param mixed $defaultValue the value that should be returned when the named cookie does not exist.
	 * @return mixed the value of the named cookie.
	 * @see get()
Qiang Xue committed
97
	 */
Qiang Xue committed
98
	public function getValue($name, $defaultValue = null)
Qiang Xue committed
99
	{
100
		return isset($this->_cookies[$name]) ? $this->_cookies[$name]->value : $defaultValue;
Qiang Xue committed
101 102
	}

Qiang Xue committed
103 104 105 106 107 108 109 110 111 112
	/**
	 * Returns whether there is a cookie with the specified name.
	 * @param string $name the cookie name
	 * @return boolean whether the named cookie exists
	 */
	public function has($name)
	{
		return isset($this->_cookies[$name]);
	}

Qiang Xue committed
113
	/**
114 115 116
	 * Adds a cookie to the collection.
	 * If there is already a cookie with the same name in the collection, it will be removed first.
	 * @param Cookie $cookie the cookie to be added
Qiang Xue committed
117
	 * @throws InvalidCallException if the cookie collection is read only
Qiang Xue committed
118
	 */
119
	public function add($cookie)
Qiang Xue committed
120
	{
Qiang Xue committed
121 122
		if ($this->readOnly) {
			throw new InvalidCallException('The cookie collection is read only.');
Qiang Xue committed
123
		}
Qiang Xue committed
124 125 126 127
		$this->_cookies[$cookie->name] = $cookie;
	}

	/**
Qiang Xue committed
128 129 130
	 * Removes a cookie.
	 * If `$removeFromBrowser` is true, the cookie will be removed from the browser.
	 * In this case, a cookie with outdated expiry will be added to the collection.
131
	 * @param Cookie|string $cookie the cookie object or the name of the cookie to be removed.
Qiang Xue committed
132 133
	 * @param boolean $removeFromBrowser whether to remove the cookie from browser
	 * @throws InvalidCallException if the cookie collection is read only
Qiang Xue committed
134
	 */
Qiang Xue committed
135
	public function remove($cookie, $removeFromBrowser = true)
Qiang Xue committed
136
	{
Qiang Xue committed
137 138
		if ($this->readOnly) {
			throw new InvalidCallException('The cookie collection is read only.');
139 140
		}
		if ($cookie instanceof Cookie) {
Qiang Xue committed
141 142 143
			$cookie->expire = 1;
			$cookie->value = '';
		} else {
Alexander Makarov committed
144
			$cookie = new Cookie([
Qiang Xue committed
145 146
				'name' => $cookie,
				'expire' => 1,
Alexander Makarov committed
147
			]);
Qiang Xue committed
148 149 150 151
		}
		if ($removeFromBrowser) {
			$this->_cookies[$cookie->name] = $cookie;
		} else {
152 153
			unset($this->_cookies[$cookie->name]);
		}
Qiang Xue committed
154 155 156
	}

	/**
157
	 * Removes all cookies.
Qiang Xue committed
158
	 * @throws InvalidCallException if the cookie collection is read only
Qiang Xue committed
159
	 */
160
	public function removeAll()
Qiang Xue committed
161
	{
Qiang Xue committed
162 163
		if ($this->readOnly) {
			throw new InvalidCallException('The cookie collection is read only.');
Qiang Xue committed
164
		}
Alexander Makarov committed
165
		$this->_cookies = [];
Qiang Xue committed
166 167 168
	}

	/**
169 170 171 172
	 * Returns the collection as a PHP array.
	 * @return array the array representation of the collection.
	 * The array keys are cookie names, and the array values are the corresponding
	 * cookie objects.
Qiang Xue committed
173 174 175 176 177 178 179
	 */
	public function toArray()
	{
		return $this->_cookies;
	}

	/**
180
	 * Returns whether there is a cookie with the specified name.
Qiang Xue committed
181
	 * This method is required by the SPL interface `ArrayAccess`.
182 183 184
	 * It is implicitly called when you use something like `isset($collection[$name])`.
	 * @param string $name the cookie name
	 * @return boolean whether the named cookie exists
Qiang Xue committed
185
	 */
186
	public function offsetExists($name)
Qiang Xue committed
187
	{
Qiang Xue committed
188
		return $this->has($name);
Qiang Xue committed
189 190 191
	}

	/**
192
	 * Returns the cookie with the specified name.
Qiang Xue committed
193
	 * This method is required by the SPL interface `ArrayAccess`.
194 195 196 197
	 * It is implicitly called when you use something like `$cookie = $collection[$name];`.
	 * This is equivalent to [[get()]].
	 * @param string $name the cookie name
	 * @return Cookie the cookie with the specified name, null if the named cookie does not exist.
Qiang Xue committed
198
	 */
199
	public function offsetGet($name)
Qiang Xue committed
200
	{
201
		return $this->get($name);
Qiang Xue committed
202 203 204
	}

	/**
205
	 * Adds the cookie to the collection.
Qiang Xue committed
206
	 * This method is required by the SPL interface `ArrayAccess`.
207 208 209 210
	 * It is implicitly called when you use something like `$collection[$name] = $cookie;`.
	 * This is equivalent to [[add()]].
	 * @param string $name the cookie name
	 * @param Cookie $cookie the cookie to be added
Qiang Xue committed
211
	 */
212
	public function offsetSet($name, $cookie)
Qiang Xue committed
213
	{
214
		$this->add($cookie);
Qiang Xue committed
215 216 217
	}

	/**
218
	 * Removes the named cookie.
Qiang Xue committed
219
	 * This method is required by the SPL interface `ArrayAccess`.
220 221 222
	 * It is implicitly called when you use something like `unset($collection[$name])`.
	 * This is equivalent to [[remove()]].
	 * @param string $name the cookie name
Qiang Xue committed
223
	 */
224
	public function offsetUnset($name)
Qiang Xue committed
225
	{
226
		$this->remove($name);
Qiang Xue committed
227 228
	}
}