DateFormatValueObject   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 11
c 0
b 0
f 0
dl 0
loc 38
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 3 1
A __construct() 0 5 1
A equals() 0 6 2
A getDateTime() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TimiTao\ValueObject\Beberlei\Standard;
6
7
use DateTime;
8
use DateTimeImmutable;
9
use Exception;
10
use TimiTao\ValueObject\Standard\DateTime\DateFormatValueObject as DateFormatValueObjectInterface;
11
12
abstract class DateFormatValueObject implements DateFormatValueObjectInterface
13
{
14
    private $value;
15
16
    private $format;
17
18
    /**
19
     * @throws Exception if value is invalid
20
     */
21
    public function __construct(DateTimeImmutable $value, string $format = DateTime::ATOM)
22
    {
23
        $this->guard($value, $format);
24
        $this->value = $value;
25
        $this->format = $format;
26
    }
27
28
    public function getValue(): string
29
    {
30
        return $this->value->format($this->format);
31
    }
32
33
    public function equals(DateFormatValueObjectInterface $other): bool
34
    {
35
        if (static::class !== get_class($other)) {
36
            return false;
37
        }
38
        return $this->getDateTime()->getTimestamp() === $other->getDateTime()->getTimestamp();
39
    }
40
41
    public function getDateTime(): DateTimeImmutable
42
    {
43
        return $this->value;
44
    }
45
46
    /**
47
     * @throws Exception if value is invalid
48
     */
49
    abstract protected function guard(DateTimeImmutable $value, string $format): void;
50
}
51