Completed
Push — fix-numbervalidator-comma-deci... ( 08054b...a7f0a3 )
by Alexander
40:41 queued 37:41
created

CookieCollection::getCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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