Passed
Push — master ( 29ab56...8d88d2 )
by Andy
01:17
created

DateTime::isSameOrAfter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Palmtree\Chrono;
4
5
use Palmtree\Chrono\Option\Comparision;
6
7
abstract class DateTime
8
{
9
    /** @var \DateTime */
10
    protected $dateTime;
11
12 20
    public function __construct(string $time = 'now', $timezone = null)
13
    {
14 20
        if (is_string($timezone)) {
15 1
            $timezone = new \DateTimeZone($timezone);
16
        }
17
18 20
        $this->dateTime = new \DateTime($time, $timezone);
19 20
    }
20
21
    abstract protected function getFormatFromTimePrecision(?string $precision);
22
23
    abstract protected function getDateInterval(int $value, string $period): \DateInterval;
24
25 18
    public function format(string $format): string
26
    {
27 18
        return $this->dateTime->format($format);
28
    }
29
30 7
    public function isSame(DateTime $date, ?string $precision = null): bool
31
    {
32 7
        return $this->compareTo($date, Comparision::EQUAL_TO, $precision);
33
    }
34
35 3
    public function isBefore(DateTime $date, ?string $precision = null): bool
36
    {
37 3
        return $this->compareTo($date, Comparision::LESS_THAN, $precision);
38
    }
39
40 1
    public function isSameOrBefore(DateTime $date, ?string $precision = null): bool
41
    {
42 1
        return $this->compareTo($date, Comparision::LESS_THAN_OR_EQUAL_TO, $precision);
43
    }
44
45 3
    public function isAfter(DateTime $date, ?string $precision = null): bool
46
    {
47 3
        return $this->compareTo($date, Comparision::GREATER_THAN, $precision);
48
    }
49
50 1
    public function isSameOrAfter(DateTime $date, ?string $precision = null): bool
51
    {
52 1
        return $this->compareTo($date, Comparision::GREATER_THAN_OR_EQUAL_TO, $precision);
53
    }
54
55 2
    public function add(int $value, string $period): self
56
    {
57 2
        $this->dateTime->add($this->getDateInterval($value, $period));
58
59 2
        return $this;
60
    }
61
62 2
    public function subtract(int $value, string $period): self
63
    {
64 2
        $this->dateTime->sub($this->getDateInterval($value, $period));
65
66 2
        return $this;
67
    }
68
69 1
    public function toDateTime(): \DateTime
70
    {
71 1
        return clone $this->dateTime;
72
    }
73
74 2
    public static function min(...$dates): ?DateTime
75
    {
76
        return \array_reduce($dates, function (?DateTime $carry, DateTime $dateTime) {
77 2
            if (!$carry || $dateTime->isBefore($carry)) {
78 2
                $carry = $dateTime;
79
            }
80
81 2
            return $carry;
82 2
        });
83
    }
84
85 2
    public static function max(...$dates): ?DateTime
86
    {
87
        return \array_reduce($dates, function (?DateTime $carry, DateTime $dateTime) {
88 2
            if (!$carry || $dateTime->isAfter($carry)) {
89 2
                $carry = $dateTime;
90
            }
91
92 2
            return $carry;
93 2
        });
94
    }
95
96 15
    private function compareTo(DateTime $date, string $operator, ?string $precision = null): bool
97
    {
98 15
        $format = $this->getFormatFromTimePrecision($precision);
99
100 14
        $operandLeft  = (int)$this->format($format);
101 14
        $operandRight = (int)$date->format($format);
102
103
        switch ($operator) {
104 14
            case Comparision::EQUAL_TO:
105
            default:
106 6
                $result = $operandLeft === $operandRight;
107 6
                break;
108 8
            case Comparision::LESS_THAN:
109 3
                $result = $operandLeft < $operandRight;
110 3
                break;
111 5
            case Comparision::GREATER_THAN:
112 3
                $result = $operandLeft > $operandRight;
113 3
                break;
114 2
            case Comparision::LESS_THAN_OR_EQUAL_TO:
115 1
                $result = $operandLeft <= $operandRight;
116 1
                break;
117 1
            case Comparision::GREATER_THAN_OR_EQUAL_TO:
118 1
                $result = $operandLeft >= $operandRight;
119 1
                break;
120
        }
121
122 14
        return $result;
123
    }
124
}
125