Skip to content

Commit

Permalink
Add Request::cookie()
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Dec 21, 2012
1 parent fb829bd commit 13006d8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/Cake/Network/Http/Request.php
Expand Up @@ -36,9 +36,13 @@ class Request {
protected $_version = '1.1';

protected $_method;

protected $_content;

protected $_url;

protected $_cookies = [];

/**
* Headers to be sent.
*
Expand Down Expand Up @@ -146,4 +150,36 @@ public function content($content = null) {
return $this;
}

/**
* Get/Set cookie values.
*
* ### Getting a cookie
*
* `$request->cookie('session');`
*
* ### Setting one cookie
*
* `$request->cookie('session', '123456');`
*
* ### Setting multiple headers
*
* `$request->cookie(['test' => 'value', 'split' => 'banana']);`
*
* @param string $name The name of the cookie to get/set
* @param string|null $value Either the value or null when getting values.
* @return mixed Either $this or the cookie value.
*/
public function cookie($name, $value = null) {
if ($value === null && is_string($name)) {
return isset($this->_cookies[$name]) ? $this->_cookies[$name] : null;
}
if (is_string($name) && is_string($value)) {
$name = [$name => $value];
}
foreach ($name as $key => $val) {
$this->_cookies[$key] = $val;
}
return $this;
}

}
16 changes: 16 additions & 0 deletions lib/Cake/Test/TestCase/Network/Http/RequestTest.php
Expand Up @@ -97,4 +97,20 @@ public function testHeader() {
$this->assertNull($request->header('not set'));
}

/**
* test cookie method.
*
* @return void
*/
public function testCookie() {
$request = new Request();
$result = $request->cookie('session', '123456');
$this->assertSame($result, $request, 'Should return self');

$this->assertNull($request->cookie('not set'));

$result = $request->cookie('session');
$this->assertEquals('123456', $result);
}

}

0 comments on commit 13006d8

Please sign in to comment.