Skip to content
This repository was archived by the owner on Dec 31, 2022. It is now read-only.

Commit 590cfcc

Browse files
committed
Issue #6
1 parent 2d4df38 commit 590cfcc

3 files changed

Lines changed: 191 additions & 0 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,20 @@ $header = new HeaderSetCookie('qwerty', '219ffwef9w0f', new \DateTime('+1 day'),
543543
$message = $header->addToMessage($message);
544544
```
545545

546+
#### Sunset
547+
548+
> Useful link: https://tools.ietf.org/id/draft-wilde-sunset-header-03.html
549+
550+
```php
551+
use Sunrise\Http\Header\HeaderSunset;
552+
use Sunrise\Http\Message\ResponseFactory;
553+
554+
$message = (new ResponseFactory)->createResponse();
555+
556+
$header = new HeaderSunset(new \DateTime('2038-01-19 03:14:07'));
557+
$message = $header->setToMessage($message);
558+
```
559+
546560
#### Trailer
547561

548562
> Useful link: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Trailer

src/HeaderSunset.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* It's free open-source software released under the MIT License.
5+
*
6+
* @author Anatoly Fenric <anatoly@fenric.ru>
7+
* @copyright Copyright (c) 2018, Anatoly Fenric
8+
* @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE
9+
* @link https://github.com/sunrise-php/http-header-kit
10+
*/
11+
12+
namespace Sunrise\Http\Header;
13+
14+
/**
15+
* HeaderSunset
16+
*
17+
* @link https://tools.ietf.org/id/draft-wilde-sunset-header-03.html
18+
* @link https://github.com/sunrise-php/http-header-kit/issues/1#issuecomment-457043527
19+
*/
20+
class HeaderSunset extends AbstractHeader implements HeaderInterface
21+
{
22+
23+
/**
24+
* Timestamp for the header field-value
25+
*
26+
* @var \DateTimeInterface
27+
*/
28+
protected $timestamp;
29+
30+
/**
31+
* Constructor of the class
32+
*
33+
* @param \DateTimeInterface $timestamp
34+
*/
35+
public function __construct(\DateTimeInterface $timestamp)
36+
{
37+
$this->setTimestamp($timestamp);
38+
}
39+
40+
/**
41+
* Sets timestamp for the header field-value
42+
*
43+
* @param \DateTimeInterface $timestamp
44+
*
45+
* @return self
46+
*/
47+
public function setTimestamp(\DateTimeInterface $timestamp) : self
48+
{
49+
$this->timestamp = $timestamp;
50+
51+
return $this;
52+
}
53+
54+
/**
55+
* Gets timestamp for the header field-value
56+
*
57+
* @return \DateTimeInterface
58+
*/
59+
public function getTimestamp() : \DateTimeInterface
60+
{
61+
return $this->timestamp;
62+
}
63+
64+
/**
65+
* {@inheritDoc}
66+
*/
67+
public function getFieldName() : string
68+
{
69+
return 'Sunset';
70+
}
71+
72+
/**
73+
* {@inheritDoc}
74+
*/
75+
public function getFieldValue() : string
76+
{
77+
$this->getTimestamp()->setTimezone(new \DateTimeZone('GMT'));
78+
79+
return $this->getTimestamp()->format(\DateTime::RFC822);
80+
}
81+
}

tests/HeaderSunsetTest.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace Sunrise\Http\Header\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Sunrise\Http\Header\HeaderSunset;
7+
use Sunrise\Http\Header\HeaderInterface;
8+
9+
class HeaderSunsetTest extends TestCase
10+
{
11+
public function testConstructor()
12+
{
13+
$now = new \DateTime('now');
14+
15+
$header = new HeaderSunset($now);
16+
17+
$this->assertInstanceOf(HeaderInterface::class, $header);
18+
}
19+
20+
public function testSetTimestamp()
21+
{
22+
$now = new \DateTime('now');
23+
24+
$header = new HeaderSunset($now);
25+
26+
$tomorrow = new \DateTime('+1 day');
27+
28+
$this->assertInstanceOf(HeaderInterface::class, $header->setTimestamp($tomorrow));
29+
30+
$this->assertEquals($tomorrow, $header->getTimestamp());
31+
}
32+
33+
public function testGetTimestamp()
34+
{
35+
$now = new \DateTime('now');
36+
37+
$header = new HeaderSunset($now);
38+
39+
$this->assertEquals($now, $header->getTimestamp());
40+
}
41+
42+
public function testGetFieldName()
43+
{
44+
$now = new \DateTime('now');
45+
46+
$header = new HeaderSunset($now);
47+
48+
$this->assertEquals('Sunset', $header->getFieldName());
49+
}
50+
51+
public function testGetFieldValue()
52+
{
53+
$now = new \DateTime('now', new \DateTimeZone('Europe/Moscow'));
54+
55+
$header = new HeaderSunset($now);
56+
57+
$expected = new \DateTime('now', new \DateTimeZone('UTC'));
58+
59+
$this->assertEquals($expected->format(\DateTime::RFC822), $header->getFieldValue());
60+
}
61+
62+
public function testToString()
63+
{
64+
$now = new \DateTime('now', new \DateTimeZone('UTC'));
65+
66+
$header = new HeaderSunset($now);
67+
68+
$this->assertEquals(\sprintf('Sunset: %s', $now->format(\DateTime::RFC822)), (string) $header);
69+
}
70+
71+
public function testSetToMessage()
72+
{
73+
$now = new \DateTime('now');
74+
$header = new HeaderSunset($now);
75+
76+
$message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse();
77+
$message = $message->withHeader($header->getFieldName(), 'foo bar baz');
78+
79+
$message = $header->setToMessage($message);
80+
81+
$this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName()));
82+
}
83+
84+
public function testAddToMessage()
85+
{
86+
$now = new \DateTime('now');
87+
$header = new HeaderSunset($now);
88+
89+
$message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse();
90+
$message = $message->withHeader($header->getFieldName(), 'foo bar baz');
91+
92+
$message = $header->addToMessage($message);
93+
94+
$this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName()));
95+
}
96+
}

0 commit comments

Comments
 (0)