FormatDateParser::parseDate()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 4
nop 1
crap 3
1
<?php
2
3
namespace Alchemy\Rest\Request\DateParser;
4
5
use Alchemy\Rest\Request\DateParser;
6
7
class FormatDateParser implements DateParser
8
{
9
10
    const FORMAT = 'Y-m-d\TH:i:s\Z';
11
12
    private $format;
13
14
    private $timezone;
15
16 18
    public function __construct($timezoneName = 'UTC', $format = null)
17
    {
18 18
        $this->timezone = new \DateTimeZone($timezoneName);
19 18
        $this->format = $format ?: self::FORMAT;
20 18
    }
21
22
    /**
23
     * @param $value
24
     * @return null|\DateTime|\DateTimeInterface
25
     */
26 16
    public function parseDate($value)
27
    {
28 16
        if (class_exists('\DateTimeImmutable')) {
29 8
            $date = \DateTimeImmutable::createFromFormat($this->format, $value, $this->timezone);
30 8
        } else {
31 8
            $date = \DateTime::createFromFormat($this->format, $value, $this->timezone);
32
        }
33
34 16
        if ($date === false) {
35 12
            return null;
36
        }
37
38 4
        return $date;
39
    }
40
}
41