ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Cookies.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Dflydev\FigCookies;
4 
6 
7 class Cookies
8 {
12  const COOKIE_HEADER = 'Cookie';
13 
17  private $cookies = [];
18 
22  public function __construct(array $cookies = [])
23  {
24  foreach ($cookies as $cookie) {
25  $this->cookies[$cookie->getName()] = $cookie;
26  }
27  }
28 
33  public function has($name)
34  {
35  return isset($this->cookies[$name]);
36  }
37 
42  public function get($name)
43  {
44  if (! $this->has($name)) {
45  return null;
46  }
47 
48  return $this->cookies[$name];
49  }
50 
54  public function getAll()
55  {
56  return array_values($this->cookies);
57  }
58 
63  public function with(Cookie $cookie)
64  {
65  $clone = clone($this);
66 
67  $clone->cookies[$cookie->getName()] = $cookie;
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->cookies[$name]);
85 
86  return $clone;
87  }
88 
96  {
97  $cookieString = implode('; ', $this->cookies);
98 
99  $request = $request->withHeader(static::COOKIE_HEADER, $cookieString);
100 
101  return $request;
102  }
103 
110  public static function fromCookieString($string)
111  {
112  return new static(Cookie::listFromCookieString($string));
113  }
114 
121  public static function fromRequest(RequestInterface $request)
122  {
123  $cookieString = $request->getHeaderLine(static::COOKIE_HEADER);
124 
125  return static::fromCookieString($cookieString);
126  }
127 }
static listFromCookieString($string)
Create a list of Cookies from a Cookie header value string.
Definition: Cookie.php:84
static fromCookieString($string)
Create Cookies from a Cookie header value string.
Definition: Cookies.php:110
foreach($paths as $path) $request
Definition: asyncclient.php:32
with(Cookie $cookie)
Definition: Cookies.php:63
withHeader($name, $value)
Return an instance with the provided value replacing the specified header.
__construct(array $cookies=[])
Definition: Cookies.php:22
const COOKIE_HEADER
The name of the Cookie header.
Definition: Cookies.php:12
Representation of an outgoing, client-side request.
static fromRequest(RequestInterface $request)
Create Cookies from a Request.
Definition: Cookies.php:121
renderIntoCookieHeader(RequestInterface $request)
Render Cookies into a Request.
Definition: Cookies.php:95
getHeaderLine($name)
Retrieves a comma-separated string of the values for a single header.