Skip to content

Commit d45dba7

Browse files
committed
Implements a cookie-date parsing utility
@see php-http/client-common#46
1 parent 13df8c4 commit d45dba7

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
## Unreleased
55

6+
###
7+
8+
- CookieUtil::parseDate to create a date from cookie date string
9+
610
## 1.5.0 - 2017-02-14
711

812
### Added

spec/CookieUtilSpec.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace spec\Http\Message;
4+
5+
use PhpSpec\ObjectBehavior;
6+
7+
class CookieUtilSpec extends ObjectBehavior
8+
{
9+
/**
10+
* @dataProvider getCookieStrings
11+
*/
12+
function it_parses_cookie_date_string($cookieDateString)
13+
{
14+
$this->beConstructedThrough('parseDate', [$cookieDateString]);
15+
$this->shouldHaveType(\DateTime::class);
16+
$this->getTimestamp()->shouldReturn(1596185377);
17+
}
18+
19+
/**
20+
* @dataProvider getInvalidCookieDateStrings
21+
*/
22+
function it_returns_false_if_cookie_date_string_is_invalid($cookieDateString)
23+
{
24+
$this->beConstructedThrough('parseDate', [$cookieDateString]);
25+
$this->shouldBe(false);
26+
}
27+
28+
/**
29+
* Provides examples for valid cookie date string.
30+
*
31+
* @return array
32+
*/
33+
public function getCookieStrings()
34+
{
35+
return [
36+
['Friday, 31 Jul 20 08:49:37 GMT'],
37+
['Friday, 31-Jul-20 08:49:37 GMT'],
38+
['Fri, 31-Jul-2020 08:49:37 GMT'],
39+
['Fri, 31 Jul 2020 08:49:37 GMT'],
40+
['Fri, 31-07-2020 08:49:37 GMT'],
41+
['Fri, 31-07-20 08:49:37 GMT'],
42+
['Friday, 31-Jul-20 08:49:37 GMT'],
43+
['Fri Jul 31 08:49:37 2020'],
44+
['Fri Jul 31 08:49:37 2020'],
45+
['Friday July 31st 2020, 08:49:37 GMT'],
46+
];
47+
}
48+
49+
/**
50+
* Provides examples for invalid cookie date string.
51+
*
52+
* @return array
53+
*/
54+
public function getInvalidCookieDateStrings()
55+
{
56+
return [
57+
['Flursday July 31st 2020, 08:49:37 GMT'],
58+
];
59+
}
60+
}

src/CookieUtil.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Http\Message;
4+
5+
final class CookieUtil
6+
{
7+
/**
8+
* Handles dates as defined by RFC 2616 section 3.3.1, and also some other
9+
* non-standard, but common formats.
10+
*
11+
* @var array
12+
*/
13+
private static $dateFormats = array(
14+
'D, d M y H:i:s T',
15+
'D, d M Y H:i:s T',
16+
'D, d-M-y H:i:s T',
17+
'D, d-M-Y H:i:s T',
18+
'D, d-m-y H:i:s T',
19+
'D, d-m-Y H:i:s T',
20+
'D M j G:i:s Y',
21+
'D M d H:i:s Y T',
22+
);
23+
24+
/**
25+
* @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/BrowserKit/Cookie.php
26+
*
27+
* @param string $dateValue
28+
* @return \DateTime|false
29+
*/
30+
public static function parseDate($dateValue)
31+
{
32+
foreach (self::$dateFormats as $dateFormat) {
33+
if (false !== $date = \DateTime::createFromFormat($dateFormat, $dateValue, new \DateTimeZone('GMT'))) {
34+
return $date;
35+
}
36+
}
37+
// attempt a fallback for unusual formatting
38+
return date_create($dateValue, new \DateTimeZone('GMT'));
39+
}
40+
}

0 commit comments

Comments
 (0)