Completed
Push — master ( 38af03...989956 )
by Frederik
02:11
created

Date   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 38
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A decode() 0 14 3
A fromFormat() 0 6 1
1
<?php
2
namespace Genkgo\Camt\Decoder;
3
4
class Date
5
{
6
    /**
7
     * @var
8
     */
9
    private $format;
10
11
    /**
12
     * @param $date
13
     * @return \DateTimeImmutable
14
     */
15
    public function decode($date)
16
    {
17
        if ($this->format === null) {
18
            $result = new \DateTimeImmutable($date);
19
        } else {
20
            $result = \DateTimeImmutable::createFromFormat($this->format, $date);
21
        }
22
23
        if ($result === false) {
24
            throw new \InvalidArgumentException("Cannot decode date {$date}");
25
        }
26
27
        return $result;
28
    }
29
30
    /**
31
     * @param $format
32
     * @return Date
33
     */
34
    public static function fromFormat($format)
35
    {
36
        $decoder = new self();
37
        $decoder->format = $format;
38
        return $decoder;
39
    }
40
41
}