Completed
Pull Request — master (#76)
by adev
03:40
created

CookieUtil::parseDate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 3
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 = [
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
     *
29
     * @return \DateTime|false
30
     */
31 13
    public static function parseDate($dateValue)
32
    {
33 13
        foreach (self::$dateFormats as $dateFormat) {
34 13
            if (false !== $date = \DateTime::createFromFormat($dateFormat, $dateValue, new \DateTimeZone('GMT'))) {
35 10
                return $date;
36
            }
37 11
        }
38
        // attempt a fallback for unusual formatting
39 3
        return date_create($dateValue, new \DateTimeZone('GMT'));
40
    }
41
}
42