ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
SetCookie.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Dflydev\FigCookies;
4 
5 use DateTime;
7 
8 class SetCookie
9 {
10  private $name;
11  private $value;
12  private $expires = 0;
13  private $maxAge = 0;
14  private $path;
15  private $domain;
16  private $secure = false;
17  private $httpOnly = false;
18 
19  private function __construct($name, $value = null)
20  {
21  $this->name = $name;
22  $this->value = $value;
23  }
24 
25  public function getName()
26  {
27  return $this->name;
28  }
29 
30  public function getValue()
31  {
32  return $this->value;
33  }
34 
35  public function getExpires()
36  {
37  return $this->expires;
38  }
39 
40  public function getMaxAge()
41  {
42  return $this->maxAge;
43  }
44 
45  public function getPath()
46  {
47  return $this->path;
48  }
49 
50  public function getDomain()
51  {
52  return $this->domain;
53  }
54 
55  public function getSecure()
56  {
57  return $this->secure;
58  }
59 
60  public function getHttpOnly()
61  {
62  return $this->httpOnly;
63  }
64 
65  public function withValue($value = null)
66  {
67  $clone = clone($this);
68 
69  $clone->value = $value;
70 
71  return $clone;
72  }
73 
74  private function resolveExpires($expires = null)
75  {
76  if (is_null($expires)) {
77  return null;
78  }
79 
80  if ($expires instanceof DateTime || $expires instanceof DateTimeInterface) {
81  return $expires->getTimestamp();
82  }
83 
84  if (is_numeric($expires)) {
85  return $expires;
86  }
87 
88  return strtotime($expires);
89  }
90 
91  public function withExpires($expires = null)
92  {
94 
95  $clone = clone($this);
96 
97  $clone->expires = $expires;
98 
99  return $clone;
100  }
101 
102  public function rememberForever()
103  {
104  return $this->withExpires(new DateTime('+5 years'));
105  }
106 
107  public function expire()
108  {
109  return $this->withExpires(new DateTime('-5 years'));
110  }
111 
112  public function withMaxAge($maxAge = null)
113  {
114  $clone = clone($this);
115 
116  $clone->maxAge = $maxAge;
117 
118  return $clone;
119  }
120 
121  public function withPath($path = null)
122  {
123  $clone = clone($this);
124 
125  $clone->path = $path;
126 
127  return $clone;
128  }
129 
130  public function withDomain($domain = null)
131  {
132  $clone = clone($this);
133 
134  $clone->domain = $domain;
135 
136  return $clone;
137  }
138 
139  public function withSecure($secure = null)
140  {
141  $clone = clone($this);
142 
143  $clone->secure = $secure;
144 
145  return $clone;
146  }
147 
148  public function withHttpOnly($httpOnly = null)
149  {
150  $clone = clone($this);
151 
152  $clone->httpOnly = $httpOnly;
153 
154  return $clone;
155  }
156 
157  public function __toString()
158  {
159  $cookieStringParts = [
160  urlencode($this->name).'='.urlencode($this->value),
161  ];
162 
163  $cookieStringParts = $this->appendFormattedDomainPartIfSet($cookieStringParts);
164  $cookieStringParts = $this->appendFormattedPathPartIfSet($cookieStringParts);
165  $cookieStringParts = $this->appendFormattedExpiresPartIfSet($cookieStringParts);
166  $cookieStringParts = $this->appendFormattedMaxAgePartIfSet($cookieStringParts);
167  $cookieStringParts = $this->appendFormattedSecurePartIfSet($cookieStringParts);
168  $cookieStringParts = $this->appendFormattedHttpOnlyPartIfSet($cookieStringParts);
169 
170  return implode('; ', $cookieStringParts);
171  }
172 
173  public static function create($name, $value = null)
174  {
175  return new static($name, $value);
176  }
177 
178  public static function createRememberedForever($name, $value = null)
179  {
180  return static::create($name, $value)->rememberForever();
181  }
182 
183  public static function createExpired($name)
184  {
185  return static::create($name)->expire();
186  }
187 
188  public static function fromSetCookieString($string)
189  {
190  $rawAttributes = StringUtil::splitOnAttributeDelimiter($string);
191 
192  list ($cookieName, $cookieValue) = StringUtil::splitCookiePair(array_shift($rawAttributes));
193 
195  $setCookie = new static($cookieName);
196 
197  if (! is_null($cookieValue)) {
198  $setCookie = $setCookie->withValue($cookieValue);
199  }
200 
201  while ($rawAttribute = array_shift($rawAttributes)) {
202  $rawAttributePair = explode('=', $rawAttribute, 2);
203 
204  $attributeKey = $rawAttributePair[0];
205  $attributeValue = count($rawAttributePair) > 1 ? $rawAttributePair[1] : null;
206 
207  $attributeKey = strtolower($attributeKey);
208 
209  switch ($attributeKey) {
210  case 'expires':
211  $setCookie = $setCookie->withExpires($attributeValue);
212  break;
213  case 'max-age':
214  $setCookie = $setCookie->withMaxAge($attributeValue);
215  break;
216  case 'domain':
217  $setCookie = $setCookie->withDomain($attributeValue);
218  break;
219  case 'path':
220  $setCookie = $setCookie->withPath($attributeValue);
221  break;
222  case 'secure':
223  $setCookie = $setCookie->withSecure(true);
224  break;
225  case 'httponly':
226  $setCookie = $setCookie->withHttpOnly(true);
227  break;
228  }
229 
230  }
231 
232  return $setCookie;
233  }
234  private function appendFormattedDomainPartIfSet(array $cookieStringParts)
235  {
236  if ($this->domain) {
237  $cookieStringParts[] = sprintf("Domain=%s", $this->domain);
238  }
239 
240  return $cookieStringParts;
241  }
242 
243  private function appendFormattedPathPartIfSet(array $cookieStringParts)
244  {
245  if ($this->path) {
246  $cookieStringParts[] = sprintf("Path=%s", $this->path);
247  }
248 
249  return $cookieStringParts;
250  }
251 
252  private function appendFormattedExpiresPartIfSet(array $cookieStringParts)
253  {
254  if ($this->expires) {
255  $cookieStringParts[] = sprintf("Expires=%s", gmdate('D, d M Y H:i:s T', $this->expires));
256  }
257 
258  return $cookieStringParts;
259  }
260 
261  private function appendFormattedMaxAgePartIfSet(array $cookieStringParts)
262  {
263  if ($this->maxAge) {
264  $cookieStringParts[] = sprintf("Max-Age=%s", $this->maxAge);
265  }
266 
267  return $cookieStringParts;
268  }
269 
270  private function appendFormattedSecurePartIfSet(array $cookieStringParts)
271  {
272  if ($this->secure) {
273  $cookieStringParts[] = 'Secure';
274  }
275 
276  return $cookieStringParts;
277  }
278 
279  private function appendFormattedHttpOnlyPartIfSet(array $cookieStringParts)
280  {
281  if ($this->httpOnly) {
282  $cookieStringParts[] = 'HttpOnly';
283  }
284 
285  return $cookieStringParts;
286  }
287 }
appendFormattedMaxAgePartIfSet(array $cookieStringParts)
Definition: SetCookie.php:261
__construct($name, $value=null)
Definition: SetCookie.php:19
appendFormattedHttpOnlyPartIfSet(array $cookieStringParts)
Definition: SetCookie.php:279
static create($name, $value=null)
Definition: SetCookie.php:173
static createRememberedForever($name, $value=null)
Definition: SetCookie.php:178
withHttpOnly($httpOnly=null)
Definition: SetCookie.php:148
$cookieName
appendFormattedDomainPartIfSet(array $cookieStringParts)
Definition: SetCookie.php:234
appendFormattedPathPartIfSet(array $cookieStringParts)
Definition: SetCookie.php:243
static splitOnAttributeDelimiter($string)
Definition: StringUtil.php:7
appendFormattedExpiresPartIfSet(array $cookieStringParts)
Definition: SetCookie.php:252
resolveExpires($expires=null)
Definition: SetCookie.php:74
appendFormattedSecurePartIfSet(array $cookieStringParts)
Definition: SetCookie.php:270
Create styles array
The data for the language used.
static splitCookiePair($string)
Definition: StringUtil.php:12
withExpires($expires=null)
Definition: SetCookie.php:91