ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
SetCookies.php
Go to the documentation of this file.
1<?php
2
3namespace Dflydev\FigCookies;
4
6
8{
12 const SET_COOKIE_HEADER = 'Set-Cookie';
13
17 private $setCookies = [];
18
22 public function __construct(array $setCookies = [])
23 {
24 foreach ($setCookies as $setCookie) {
25 $this->setCookies[$setCookie->getName()] = $setCookie;
26 }
27 }
28
33 public function has($name)
34 {
35 return isset($this->setCookies[$name]);
36 }
37
42 public function get($name)
43 {
44 if (! $this->has($name)) {
45 return null;
46 }
47
48 return $this->setCookies[$name];
49 }
50
54 public function getAll()
55 {
56 return array_values($this->setCookies);
57 }
58
63 public function with(SetCookie $setCookie)
64 {
65 $clone = clone($this);
66
67 $clone->setCookies[$setCookie->getName()] = $setCookie;
68
69 return $clone;
70 }
71
76 public function without($name)
77 {
78 $clone = clone($this);
79
80 if (! $clone->has($name)) {
81 return $clone;
82 }
83
84 unset($clone->setCookies[$name]);
85
86 return $clone;
87 }
88
96 {
97 $response = $response->withoutHeader(static::SET_COOKIE_HEADER);
98 foreach ($this->setCookies as $setCookie) {
99 $response = $response->withAddedHeader(static::SET_COOKIE_HEADER, (string) $setCookie);
100 }
101
102 return $response;
103 }
104
111 public static function fromSetCookieStrings($setCookieStrings)
112 {
113 return new static(array_map(function ($setCookieString) {
114 return SetCookie::fromSetCookieString($setCookieString);
115 }, $setCookieStrings));
116 }
117
125 {
126 return new static(array_map(function ($setCookieString) {
127 return SetCookie::fromSetCookieString($setCookieString);
128 }, $response->getHeader(static::SET_COOKIE_HEADER)));
129 }
130}
An exception for terminatinating execution or to throw for unit testing.
const SET_COOKIE_HEADER
The name of the Set-Cookie header.
Definition: SetCookies.php:12
with(SetCookie $setCookie)
Definition: SetCookies.php:63
static fromResponse(ResponseInterface $response)
Create SetCookies from a Response.
Definition: SetCookies.php:124
__construct(array $setCookies=[])
Definition: SetCookies.php:22
renderIntoSetCookieHeader(ResponseInterface $response)
Render SetCookies into a Response.
Definition: SetCookies.php:95
static fromSetCookieStrings($setCookieStrings)
Create SetCookies from a collection of SetCookie header value strings.
Definition: SetCookies.php:111
Representation of an outgoing, server-side response.
$response