Skip to content

Commit 136afa8

Browse files
committed
Pull CookieEncoder out of CookieBuilder, move into Cookie namespace.
1 parent 3179edd commit 136afa8

File tree

4 files changed

+114
-67
lines changed

4 files changed

+114
-67
lines changed

src/CookieBuilder.php renamed to src/Cookie/CookieBuilder.php

Lines changed: 8 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
<?php
22

3-
namespace Corpus\HttpMessageUtils;
3+
namespace Corpus\HttpMessageUtils\Cookie;
44

55
use Psr\Http\Message\ResponseInterface;
66

77
/**
88
* Utility to build an HTTP Cookie header
9-
*
10-
* Inspired by and partly based on `hansott/psr7-cookies`
11-
* MIT License Copyright (c) 2017 Hans Ott [email protected]
129
*/
13-
class CookieBuilder {
10+
class CookieBuilder implements CookieInterface {
1411

1512
private string $name;
1613
private string $value;
@@ -21,6 +18,8 @@ class CookieBuilder {
2118
private bool $httpOnly;
2219
private string $sameSite;
2320

21+
private CookieEncoder $encoder;
22+
2423
/**
2524
* @param string $name The name of the cookie
2625
* @param int $expiration The number of seconds for which this cookie will be valid. `time() + $expiration`
@@ -50,6 +49,8 @@ public function __construct(
5049
$this->secure = $secure;
5150
$this->httpOnly = $httpOnly;
5251
$this->sameSite = $sameSite;
52+
53+
$this->encoder = new CookieEncoder;
5354
}
5455

5556
/**
@@ -76,15 +77,15 @@ public function apply( callable $callee = "\\setcookie" ) : bool {
7677
* `Set-Cookie` header representing this Cookie.
7778
*/
7879
public function responseWithHeaderAdded( ResponseInterface $response ) : ResponseInterface {
79-
return $response->withAddedHeader('Set-Cookie', $this->toHeaderValue());
80+
return $response->withAddedHeader('Set-Cookie', ($this->encoder)($this));
8081
}
8182

8283
/**
8384
* Given a \Psr\Http\Message\ResponseInterface returns a new instance of ResponseInterface replacing any
8485
* `Set-Cookie` headers with one representing this Cookie.
8586
*/
8687
public function responseWithHeader( ResponseInterface $response ) : ResponseInterface {
87-
return $response->withHeader('Set-Cookie', $this->toHeaderValue());
88+
return $response->withHeader('Set-Cookie', ($this->encoder)($this));
8889
}
8990

9091
/**
@@ -180,96 +181,36 @@ public function withSameSite( string $sameSite ) : self {
180181
return $that;
181182
}
182183

183-
/**
184-
* Get the cookie name.
185-
*/
186184
public function getName() : string {
187185
return $this->name;
188186
}
189187

190-
/**
191-
* Get the cookie value.
192-
*/
193188
public function getValue() : string {
194189
return $this->value;
195190
}
196191

197-
/**
198-
* Get the cookie expiration.
199-
*/
200192
public function getExpiration() : int {
201193
return $this->expiration;
202194
}
203195

204-
/**
205-
* Get the cookie path.
206-
*/
207196
public function getPath() : string {
208197
return $this->path;
209198
}
210199

211-
/**
212-
* Get the cookie domain.
213-
*/
214200
public function getDomain() : string {
215201
return $this->domain;
216202
}
217203

218-
/**
219-
* Get the cookie secure flag.
220-
*/
221204
public function isSecure() : bool {
222205
return $this->secure;
223206
}
224207

225-
/**
226-
* Get the cookie httpOnly flag.
227-
*/
228208
public function isHttpOnly() : bool {
229209
return $this->httpOnly;
230210
}
231211

232-
/**
233-
* Get the cookie SameSite value.
234-
*/
235212
public function getSameSite() : string {
236213
return $this->sameSite;
237214
}
238215

239-
/**
240-
* Get the cookie as a header value.
241-
*/
242-
public function toHeaderValue() : string {
243-
$headerValue = sprintf('%s=%s', $this->name, urlencode($this->value));
244-
245-
if( $this->expiration !== 0 ) {
246-
$headerValue .= sprintf(
247-
'; expires=%s',
248-
gmdate(DATE_RFC1123, time() + $this->expiration)
249-
);
250-
}
251-
252-
if( $this->path ) {
253-
$headerValue .= sprintf('; path=%s', $this->path);
254-
}
255-
256-
if( $this->domain ) {
257-
$headerValue .= sprintf('; domain=%s', $this->domain);
258-
}
259-
260-
if( $this->secure ) {
261-
$headerValue .= '; secure';
262-
}
263-
264-
if( $this->httpOnly ) {
265-
$headerValue .= '; httponly';
266-
}
267-
268-
if( $this->sameSite ) {
269-
$headerValue .= sprintf('; samesite=%s', $this->sameSite);
270-
}
271-
272-
return $headerValue;
273-
}
274-
275216
}

src/Cookie/CookieEncoder.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Corpus\HttpMessageUtils\Cookie;
4+
5+
/**
6+
* Utility to encode a Cookie into a `Set-Cookie` header
7+
*
8+
* Based on `hansott/psr7-cookies`
9+
* MIT License Copyright (c) 2017 Hans Ott [email protected]
10+
* @see https://github.com/hansott/psr7-cookies/blob/ec7bc4b3393677730b1e607c987328655d27dfbf/src/SetCookie.php#L160-L192
11+
*/
12+
class CookieEncoder implements CookieEncoderInterface {
13+
14+
public function __invoke( CookieInterface $cookie ) : string {
15+
$headerValue = sprintf('%s=%s', $cookie->getName(), urlencode($cookie->getValue()));
16+
17+
if( $cookie->getExpiration() !== 0 ) {
18+
$headerValue .= sprintf(
19+
'; expires=%s',
20+
gmdate(DATE_RFC1123, time() + $cookie->getExpiration())
21+
);
22+
}
23+
24+
if( $cookie->getPath() ) {
25+
$headerValue .= sprintf('; path=%s', $cookie->getPath());
26+
}
27+
28+
if( $cookie->getDomain() ) {
29+
$headerValue .= sprintf('; domain=%s', $cookie->getDomain());
30+
}
31+
32+
if( $cookie->isSecure() ) {
33+
$headerValue .= '; secure';
34+
}
35+
36+
if( $cookie->isHttpOnly() ) {
37+
$headerValue .= '; httponly';
38+
}
39+
40+
if( $cookie->getSameSite() ) {
41+
$headerValue .= sprintf('; samesite=%s', $cookie->getSameSite());
42+
}
43+
44+
return $headerValue;
45+
}
46+
47+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Corpus\HttpMessageUtils\Cookie;
4+
5+
interface CookieEncoderInterface {
6+
7+
/**
8+
* Encode the given Cookie to a header string
9+
*/
10+
public function __invoke( CookieInterface $cookie ) : string;
11+
12+
}

src/Cookie/CookieInterface.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Corpus\HttpMessageUtils\Cookie;
4+
5+
interface CookieInterface {
6+
7+
/**
8+
* Get the cookie name.
9+
*/
10+
public function getName() : string;
11+
12+
/**
13+
* Get the cookie value.
14+
*/
15+
public function getValue() : string;
16+
17+
/**
18+
* Get the cookie expiration.
19+
*/
20+
public function getExpiration() : int;
21+
22+
/**
23+
* Get the cookie path.
24+
*/
25+
public function getPath() : string;
26+
27+
/**
28+
* Get the cookie domain.
29+
*/
30+
public function getDomain() : string;
31+
32+
/**
33+
* Get the cookie SameSite value.
34+
*/
35+
public function getSameSite() : string;
36+
37+
/**
38+
* Get the cookie httpOnly flag.
39+
*/
40+
public function isHttpOnly() : bool;
41+
42+
/**
43+
* Get the cookie secure flag.
44+
*/
45+
public function isSecure() : bool;
46+
47+
}

0 commit comments

Comments
 (0)