DepartureDateTime::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TK\API\ValueObject;
5
6
use DateTimeImmutable;
7
use TK\API\Exception\InvalidArgumentException;
8
9
final class DepartureDateTime implements ValueObjectInterface
10
{
11
    private $date;
12
    private $windowAfter;
13
    private $windowBefore;
14
15
    private $dateFormat = 'Y-m-d';
16
17
    public function __construct(DateTimeImmutable $date, string $windowAfter, string $windowBefore)
18
    {
19
        $this->setDate($date);
20
        $this->setWindowBefore($windowBefore);
21
        $this->setWindowAfter($windowAfter);
22
    }
23
24
    private function setDate(DateTimeImmutable $date) : void
25
    {
26
        $this->date = $date;
27
    }
28
29
    private function setWindowBefore(string $windowBefore) : void
30
    {
31
        if (!preg_match('/^P[0-3]{1}D$/', $windowBefore)) {
32
            throw new InvalidArgumentException(
33
                'Invalid DepartureDateTime.WindowBefore value provided. Format should be like this P{int:[0,1,2,3]}D'
34
            );
35
        }
36
        $this->windowBefore = $windowBefore;
37
    }
38
39
    private function setWindowAfter(string $windowAfter) : void
40
    {
41
        if (!preg_match('/^P[0-3]{1}D$/', $windowAfter)) {
42
            throw new InvalidArgumentException(
43
                'Invalid DepartureDateTime.WindowAfter value provided. Format should be like this P{int:[0,1,2,3]}D'
44
            );
45
        }
46
        $this->windowAfter = $windowAfter;
47
    }
48
49
    public function withDateFormat(string $dateFormat) : DepartureDateTime
50
    {
51
        $this->dateFormat = $dateFormat;
52
        return $this;
53
    }
54
55
    public function getValue() : array
56
    {
57
        return [
58
            'Date' => strtoupper($this->date->format($this->dateFormat)),
59
            'WindowBefore' => $this->windowBefore,
60
            'WindowAfter' => $this->windowAfter
61
        ];
62
    }
63
}
64