ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
SetCookies.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Dflydev\FigCookies;
4 
6 
7 class SetCookies
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 
124  public static function fromResponse(ResponseInterface $response)
125  {
126  return new static(array_map(function ($setCookieString) {
127  return SetCookie::fromSetCookieString($setCookieString);
128  }, $response->getHeader(static::SET_COOKIE_HEADER)));
129  }
130 }
with(SetCookie $setCookie)
Definition: SetCookies.php:63
withoutHeader($name)
Return an instance without the specified header.
renderIntoSetCookieHeader(ResponseInterface $response)
Render SetCookies into a Response.
Definition: SetCookies.php:95
static fromResponse(ResponseInterface $response)
Create SetCookies from a Response.
Definition: SetCookies.php:124
static fromSetCookieStrings($setCookieStrings)
Create SetCookies from a collection of SetCookie header value strings.
Definition: SetCookies.php:111
const SET_COOKIE_HEADER
The name of the Set-Cookie header.
Definition: SetCookies.php:12
if($format !==null) $name
Definition: metadata.php:146
getHeader($name)
Retrieves a message header value by the given case-insensitive name.
Representation of an outgoing, server-side response.
withAddedHeader($name, $value)
Return an instance with the specified header appended with the given value.
__construct(array $setCookies=[])
Definition: SetCookies.php:22
Create styles array
The data for the language used.
$response