FormatDateParser   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 34
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A parseDate() 0 14 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