Completed
Push — feature/pilot_information ( 13ff1e...cc067b )
by Laurent
01:46
created

DateTrait::fromString()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace FlightLog\Domain\Pilot\ValueObject;
5
6
7
trait DateTrait
8
{
9
    /**
10
     * @var \DateTimeImmutable|null
11
     */
12
    private $value;
13
14
    private function __construct(\DateTimeImmutable $value = null)
15
    {
16
        $this->value = $value;
17
    }
18
19
    /**
20
     * @param \DateTimeImmutable $value
21
     *
22
     * @return DateTrait|static
0 ignored issues
show
Comprehensibility Bug introduced by
The return type DateTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
23
     */
24
    public static function create(\DateTimeImmutable $value): self
25
    {
26
        return new self($value);
27
    }
28
29
    /**
30
     * @return DateTrait|static
0 ignored issues
show
Comprehensibility Bug introduced by
The return type DateTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
31
     */
32
    public static function zero(): self
33
    {
34
        return new self(null);
35
    }
36
37
    /**
38
     * @param string|null $date
39
     *
40
     * @return DateTrait|static
0 ignored issues
show
Comprehensibility Bug introduced by
The return type DateTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
41
     */
42
    public static function fromString(string $date = null): self
43
    {
44
        if (null === $date || empty($date)) {
45
            return self::zero();
46
        }
47
48
        return new self(\DateTimeImmutable::createFromFormat('Y-m-d', $date));
49
    }
50
51
    public function asString()
52
    {
53
        if (null === $this->value) {
54
            return null;
55
        }
56
57
        return $this->value->format('Y-m-d');
58
    }
59
60
}