CookieCollection.php 6.38 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
 * @property integer $count the number of cookies in the collection
Qiang Xue committed
19 20 21 22
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
23
class CookieCollection extends Object implements \IteratorAggregate, \ArrayAccess, \Countable
Qiang Xue committed
24
{
Qiang Xue committed
25
	/**
Qiang Xue committed
26
	 * @var boolean whether this collection is read only.
Qiang Xue committed
27
	 */
Qiang Xue committed
28
	public $readOnly = false;
Qiang Xue committed
29

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

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

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

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

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

	/**
79 80 81 82
	 * 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
83
	 */
84
	public function get($name)
Qiang Xue committed
85
	{
86
		return isset($this->_cookies[$name]) ? $this->_cookies[$name] : null;
Qiang Xue committed
87 88 89
	}

	/**
90 91 92 93 94
	 * 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
95
	 */
Qiang Xue committed
96
	public function getValue($name, $defaultValue = null)
Qiang Xue committed
97
	{
98
		return isset($this->_cookies[$name]) ? $this->_cookies[$name]->value : $defaultValue;
Qiang Xue committed
99 100
	}

Qiang Xue committed
101 102 103 104 105 106 107 108 109 110
	/**
	 * 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
111
	/**
112 113 114
	 * 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
115
	 * @throws InvalidCallException if the cookie collection is read only
Qiang Xue committed
116
	 */
117
	public function add($cookie)
Qiang Xue committed
118
	{
Qiang Xue committed
119 120
		if ($this->readOnly) {
			throw new InvalidCallException('The cookie collection is read only.');
Qiang Xue committed
121
		}
Qiang Xue committed
122 123 124 125
		$this->_cookies[$cookie->name] = $cookie;
	}

	/**
Qiang Xue committed
126 127 128
	 * 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.
129
	 * @param Cookie|string $cookie the cookie object or the name of the cookie to be removed.
Qiang Xue committed
130 131
	 * @param boolean $removeFromBrowser whether to remove the cookie from browser
	 * @throws InvalidCallException if the cookie collection is read only
Qiang Xue committed
132
	 */
Qiang Xue committed
133
	public function remove($cookie, $removeFromBrowser = true)
Qiang Xue committed
134
	{
Qiang Xue committed
135 136
		if ($this->readOnly) {
			throw new InvalidCallException('The cookie collection is read only.');
137 138
		}
		if ($cookie instanceof Cookie) {
Qiang Xue committed
139 140 141 142 143 144 145 146 147 148 149
			$cookie->expire = 1;
			$cookie->value = '';
		} else {
			$cookie = new Cookie(array(
				'name' => $cookie,
				'expire' => 1,
			));
		}
		if ($removeFromBrowser) {
			$this->_cookies[$cookie->name] = $cookie;
		} else {
150 151
			unset($this->_cookies[$cookie->name]);
		}
Qiang Xue committed
152 153 154
	}

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

	/**
167 168 169 170
	 * 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
171 172 173 174 175 176 177
	 */
	public function toArray()
	{
		return $this->_cookies;
	}

	/**
178
	 * Returns whether there is a cookie with the specified name.
Qiang Xue committed
179
	 * This method is required by the SPL interface `ArrayAccess`.
180 181 182
	 * 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
183
	 */
184
	public function offsetExists($name)
Qiang Xue committed
185
	{
Qiang Xue committed
186
		return $this->has($name);
Qiang Xue committed
187 188 189
	}

	/**
190
	 * Returns the cookie with the specified name.
Qiang Xue committed
191
	 * This method is required by the SPL interface `ArrayAccess`.
192 193 194 195
	 * 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
196
	 */
197
	public function offsetGet($name)
Qiang Xue committed
198
	{
199
		return $this->get($name);
Qiang Xue committed
200 201 202
	}

	/**
203
	 * Adds the cookie to the collection.
Qiang Xue committed
204
	 * This method is required by the SPL interface `ArrayAccess`.
205 206 207 208
	 * 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
209
	 */
210
	public function offsetSet($name, $cookie)
Qiang Xue committed
211
	{
212
		$this->add($cookie);
Qiang Xue committed
213 214 215
	}

	/**
216
	 * Removes the named cookie.
Qiang Xue committed
217
	 * This method is required by the SPL interface `ArrayAccess`.
218 219 220
	 * 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
221
	 */
222
	public function offsetUnset($name)
Qiang Xue committed
223
	{
224
		$this->remove($name);
Qiang Xue committed
225 226
	}
}