|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
-
-
- namespace yii\web;
-
- use Yii;
- use ArrayIterator;
- use yii\base\InvalidCallException;
- use yii\base\Object;
-
-
- class CookieCollection extends Object implements \IteratorAggregate, \ArrayAccess, \Countable
- {
-
-
- public $readOnly = false;
-
-
-
- private $_cookies = [];
-
-
-
-
- public function __construct($cookies = [], $config = [])
- {
- $this->_cookies = $cookies;
- parent::__construct($config);
- }
-
-
-
- public function getIterator()
- {
- return new ArrayIterator($this->_cookies);
- }
-
-
-
- public function count()
- {
- return $this->getCount();
- }
-
-
-
- public function getCount()
- {
- return count($this->_cookies);
- }
-
-
-
- public function get($name)
- {
- return isset($this->_cookies[$name]) ? $this->_cookies[$name] : null;
- }
-
-
-
- public function getValue($name, $defaultValue = null)
- {
- return isset($this->_cookies[$name]) ? $this->_cookies[$name]->value : $defaultValue;
- }
-
-
-
- public function has($name)
- {
- return isset($this->_cookies[$name]) && $this->_cookies[$name]->value !== ''
- && ($this->_cookies[$name]->expire === null || $this->_cookies[$name]->expire >= time());
- }
-
-
-
- public function add($cookie)
- {
- if ($this->readOnly) {
- throw new InvalidCallException('The cookie collection is read only.');
- }
- $this->_cookies[$cookie->name] = $cookie;
- }
-
-
-
- public function remove($cookie, $removeFromBrowser = true)
- {
- if ($this->readOnly) {
- throw new InvalidCallException('The cookie collection is read only.');
- }
- if ($cookie instanceof Cookie) {
- $cookie->expire = 1;
- $cookie->value = '';
- } else {
- $cookie = new Cookie([
- 'name' => $cookie,
- 'expire' => 1,
- ]);
- }
- if ($removeFromBrowser) {
- $this->_cookies[$cookie->name] = $cookie;
- } else {
- unset($this->_cookies[$cookie->name]);
- }
- }
-
-
-
- public function removeAll()
- {
- if ($this->readOnly) {
- throw new InvalidCallException('The cookie collection is read only.');
- }
- $this->_cookies = [];
- }
-
-
-
- public function toArray()
- {
- return $this->_cookies;
- }
-
-
-
- public function fromArray(array $array)
- {
- $this->_cookies = $array;
- }
-
-
-
- public function offsetExists($name)
- {
- return $this->has($name);
- }
-
-
-
- public function offsetGet($name)
- {
- return $this->get($name);
- }
-
-
-
- public function offsetSet($name, $cookie)
- {
- $this->add($cookie);
- }
-
-
-
- public function offsetUnset($name)
- {
- $this->remove($name);
- }
- }
|