2727class Cookie implements \ArrayAccess
2828{
2929 /** @var array */
30- protected $ cookie = [
31- 'name ' => null ,
32- 'value ' => null ,
33- 'path ' => null ,
34- 'domain ' => null ,
35- 'expiry ' => null ,
36- 'secure ' => null ,
37- 'httpOnly ' => null ,
38- ];
30+ protected $ cookie = [];
3931
4032 /**
4133 * @param string $name The name of the cookie; may not be null or an empty string.
@@ -51,11 +43,17 @@ public function __construct($name, $value)
5143 }
5244
5345 /**
54- * @param array $cookieArray
46+ * @param array $cookieArray The cookie fields; must contain name and value.
5547 * @return Cookie
5648 */
5749 public static function createFromArray (array $ cookieArray )
5850 {
51+ if (!isset ($ cookieArray ['name ' ])) {
52+ throw new InvalidArgumentException ('Cookie name should be set ' );
53+ }
54+ if (!isset ($ cookieArray ['value ' ])) {
55+ throw new InvalidArgumentException ('Cookie value should be set ' );
56+ }
5957 $ cookie = new self ($ cookieArray ['name ' ], $ cookieArray ['value ' ]);
6058
6159 if (isset ($ cookieArray ['path ' ])) {
@@ -82,15 +80,15 @@ public static function createFromArray(array $cookieArray)
8280 */
8381 public function getName ()
8482 {
85- return $ this ->cookie [ 'name ' ] ;
83+ return $ this ->offsetGet ( 'name ' ) ;
8684 }
8785
8886 /**
8987 * @return string
9088 */
9189 public function getValue ()
9290 {
93- return $ this ->cookie [ 'value ' ] ;
91+ return $ this ->offsetGet ( 'value ' ) ;
9492 }
9593
9694 /**
@@ -100,15 +98,15 @@ public function getValue()
10098 */
10199 public function setPath ($ path )
102100 {
103- $ this ->cookie [ 'path ' ] = $ path ;
101+ $ this ->offsetSet ( 'path ' , $ path) ;
104102 }
105103
106104 /**
107105 * @return string|null
108106 */
109107 public function getPath ()
110108 {
111- return $ this ->cookie [ 'path ' ] ;
109+ return $ this ->offsetGet ( 'path ' ) ;
112110 }
113111
114112 /**
@@ -122,15 +120,15 @@ public function setDomain($domain)
122120 throw new InvalidArgumentException (sprintf ('Cookie domain "%s" should not contain a port ' , $ domain ));
123121 }
124122
125- $ this ->cookie [ 'domain ' ] = $ domain ;
123+ $ this ->offsetSet ( 'domain ' , $ domain) ;
126124 }
127125
128126 /**
129127 * @return string|null
130128 */
131129 public function getDomain ()
132130 {
133- return $ this ->cookie [ 'domain ' ] ;
131+ return $ this ->offsetGet ( 'domain ' ) ;
134132 }
135133
136134 /**
@@ -140,15 +138,15 @@ public function getDomain()
140138 */
141139 public function setExpiry ($ expiry )
142140 {
143- $ this ->cookie [ 'expiry ' ] = (int ) $ expiry ;
141+ $ this ->offsetSet ( 'expiry ' , (int ) $ expiry) ;
144142 }
145143
146144 /**
147145 * @return int|null
148146 */
149147 public function getExpiry ()
150148 {
151- return $ this ->cookie [ 'expiry ' ] ;
149+ return $ this ->offsetGet ( 'expiry ' ) ;
152150 }
153151
154152 /**
@@ -158,15 +156,15 @@ public function getExpiry()
158156 */
159157 public function setSecure ($ secure )
160158 {
161- $ this ->cookie [ 'secure ' ] = $ secure ;
159+ $ this ->offsetSet ( 'secure ' , $ secure) ;
162160 }
163161
164162 /**
165163 * @return bool|null
166164 */
167165 public function isSecure ()
168166 {
169- return $ this ->cookie [ 'secure ' ] ;
167+ return $ this ->offsetGet ( 'secure ' ) ;
170168 }
171169
172170 /**
@@ -176,15 +174,15 @@ public function isSecure()
176174 */
177175 public function setHttpOnly ($ httpOnly )
178176 {
179- $ this ->cookie [ 'httpOnly ' ] = $ httpOnly ;
177+ $ this ->offsetSet ( 'httpOnly ' , $ httpOnly) ;
180178 }
181179
182180 /**
183181 * @return bool|null
184182 */
185183 public function isHttpOnly ()
186184 {
187- return $ this ->cookie [ 'httpOnly ' ] ;
185+ return $ this ->offsetGet ( 'httpOnly ' ) ;
188186 }
189187
190188 /**
@@ -202,12 +200,16 @@ public function offsetExists($offset)
202200
203201 public function offsetGet ($ offset )
204202 {
205- return $ this ->cookie [$ offset ];
203+ return $ this ->offsetExists ( $ offset ) ? $ this -> cookie [$ offset ] : null ;
206204 }
207205
208206 public function offsetSet ($ offset , $ value )
209207 {
210- $ this ->cookie [$ offset ] = $ value ;
208+ if ($ value === null ) {
209+ unset($ this ->cookie [$ offset ]);
210+ } else {
211+ $ this ->cookie [$ offset ] = $ value ;
212+ }
211213 }
212214
213215 public function offsetUnset ($ offset )
0 commit comments