Skip to content

Commit 4c53882

Browse files
committed
feat: Response::setCookie() can get Cookie object as the first parameter
1 parent fca1d0a commit 4c53882

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

system/HTTP/ResponseTrait.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -536,15 +536,15 @@ public function redirect(string $uri, string $method = 'auto', ?int $code = null
536536
* Accepts an arbitrary number of binds (up to 7) or an associative
537537
* array in the first parameter containing all the values.
538538
*
539-
* @param array|string $name Cookie name or array containing binds
540-
* @param string $value Cookie value
541-
* @param string $expire Cookie expiration time in seconds
542-
* @param string $domain Cookie domain (e.g.: '.yourdomain.com')
543-
* @param string $path Cookie path (default: '/')
544-
* @param string $prefix Cookie name prefix
545-
* @param bool $secure Whether to only transfer cookies via SSL
546-
* @param bool $httponly Whether only make the cookie accessible via HTTP (no javascript)
547-
* @param string|null $samesite
539+
* @param array|Cookie|string $name Cookie name / array containing binds / Cookie object
540+
* @param string $value Cookie value
541+
* @param string $expire Cookie expiration time in seconds
542+
* @param string $domain Cookie domain (e.g.: '.yourdomain.com')
543+
* @param string $path Cookie path (default: '/')
544+
* @param string $prefix Cookie name prefix
545+
* @param bool $secure Whether to only transfer cookies via SSL
546+
* @param bool $httponly Whether only make the cookie accessible via HTTP (no javascript)
547+
* @param string|null $samesite
548548
*
549549
* @return $this
550550
*/
@@ -559,6 +559,12 @@ public function setCookie(
559559
$httponly = false,
560560
$samesite = null
561561
) {
562+
if ($name instanceof Cookie) {
563+
$this->cookieStore = $this->cookieStore->put($name);
564+
565+
return $this;
566+
}
567+
562568
if (is_array($name)) {
563569
// always leave 'name' in last place, as the loop will break otherwise, due to $$item
564570
foreach (['samesite', 'value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name'] as $item) {

tests/system/HTTP/ResponseCookieTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ public function testCookiesAll()
6565
$this->assertTrue($response->hasCookie('bee'));
6666
}
6767

68+
public function testSetCookieObject()
69+
{
70+
$cookie = new Cookie('foo', 'bar');
71+
$response = new Response(new App());
72+
73+
$response->setCookie($cookie);
74+
75+
$this->assertCount(1, $response->getCookies());
76+
$this->assertTrue($response->hasCookie('foo'));
77+
}
78+
6879
public function testCookieGet()
6980
{
7081
$response = new Response(new App());

0 commit comments

Comments
 (0)