Completed
Pull Request — master (#9)
by Veaceslav
04:31
created

DateRange::getDatePeriod()   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
 * This file is part of Zee Project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @see https://github.com/zee/
9
 */
10
11
namespace Zee\DateRange;
12
13
use DateInterval;
14
use DatePeriod;
15
use DateTimeImmutable;
16
use DateTimeInterface;
17
use JsonSerializable;
18
use Zee\DateRange\States\RangeState;
19
use Zee\DateRange\States\UndefinedRange;
20
21
/**
22
 * Implementation of date range value object.
23
 */
24
final class DateRange implements DateRangeInterface, JsonSerializable
25
{
26
    /**
27
     * @var RangeState
28
     */
29
    private $state;
30
31
    /**
32
     * @param DateTimeInterface|null $startTime
33
     * @param DateTimeInterface|null $endTime
34
     */
35 13
    public function __construct(DateTimeInterface $startTime = null, DateTimeInterface $endTime = null)
36
    {
37 13
        $state = new UndefinedRange();
38
39 13
        if (isset($startTime)) {
40 8
            $state = $state->setStartTime($startTime);
41
        }
42
43 13
        if (isset($endTime)) {
44 8
            $state = $state->setEndTime($endTime);
45
        }
46
47 11
        $this->state = $state;
48
    }
49
50
    /**
51
     * Returns string representation of range.
52
     *
53
     * {@inheritdoc}
54
     */
55 4
    public function __toString(): string
56
    {
57 4
        return (string) $this->state;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 1
    public function __debugInfo()
64
    {
65
        return [
66 1
            'startTime' => $this->state->hasStartTime()
67 1
                ? $this->state->getStartTime()
68
                : null,
69 1
            'endTime' => $this->state->hasEndTime()
70 1
                ? $this->state->getEndTime()
71
                : null,
72
        ];
73
    }
74
75
    /**
76
     * Returns value ready to be encoded as JSON.
77
     *
78
     * The ISO-8601 range format is used for times.
79
     *
80
     * {@inheritdoc}
81
     */
82 4
    public function jsonSerialize(): array
83
    {
84 4
        return $this->state->jsonSerialize();
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 4
    public function hasStartTime(): bool
91
    {
92 4
        return $this->state->hasStartTime();
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 4
    public function hasEndTime(): bool
99
    {
100 4
        return $this->state->hasEndTime();
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 8
    public function getStartTime(): DateTimeInterface
107
    {
108 8
        return $this->state->getStartTime();
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 8
    public function getEndTime(): DateTimeInterface
115
    {
116 8
        return $this->state->getEndTime();
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 4
    public function setStartTime(DateTimeInterface $time): DateRangeInterface
123
    {
124 4
        $clone = clone $this;
125 4
        $clone->state = $clone->state->setStartTime($time);
126
127 4
        return $clone;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 4
    public function setEndTime(DateTimeInterface $time): DateRangeInterface
134
    {
135 4
        $clone = clone $this;
136 4
        $clone->state = $clone->state->setEndTime($time);
137
138 4
        return $clone;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 4
    public function isStartedAt(DateTimeInterface $time): bool
145
    {
146 4
        return $this->hasStartTime() && $this->state->compareStartTime($time) === 0;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152 4
    public function isEndedAt(DateTimeInterface $time): bool
153
    {
154 4
        return $this->hasEndTime() && $this->state->compareEndTime($time) === 0;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 4
    public function isStarted(): bool
161
    {
162 4
        if ($this->hasStartTime()) {
163 2
            return $this->state->compareStartTime(new DateTimeImmutable()) <= 0;
164
        } else {
165 2
            return $this->hasEndTime();
166
        }
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172 4
    public function isEnded(): bool
173
    {
174 4
        return $this->hasEndTime() && $this->state->compareEndTime(new DateTimeImmutable()) < 0;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180 1
    public function getDateInterval(): DateInterval
181
    {
182 1
        return $this->getStartTime()->diff($this->getEndTime());
0 ignored issues
show
Bug introduced by
$this->getEndTime() of type DateTimeInterface is incompatible with the type DateInterval expected by parameter $datetime2 of DateTimeInterface::diff(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

182
        return $this->getStartTime()->diff(/** @scrutinizer ignore-type */ $this->getEndTime());
Loading history...
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188 1
    public function getDatePeriod(DateInterval $interval, int $option = 0): DatePeriod
189
    {
190 1
        return new DatePeriod($this->getStartTime(), $interval, $this->getEndTime(), $option);
191
    }
192
}
193