Completed
Push — master ( 35fe2c...cfa2d2 )
by Veaceslav
03:15
created

DateRange::setEndTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
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 DateTimeInterface;
14
use JsonSerializable;
15
use Zee\DateRange\States\RangeState;
16
use Zee\DateRange\States\UndefinedRange;
17
18
/**
19
 * Implementation of date range value object.
20
 */
21
final class DateRange implements DateRangeInterface, JsonSerializable
22
{
23
    /**
24
     * @var RangeState
25
     */
26
    private $state;
27
28
    /**
29
     * @param DateTimeInterface|null $startTime
30
     * @param DateTimeInterface|null $endTime
31
     */
32 11
    public function __construct(DateTimeInterface $startTime = null, DateTimeInterface $endTime = null)
33
    {
34 11
        $state = new UndefinedRange();
35
36 11
        if (isset($startTime)) {
37 6
            $state = $state->setStartTime($startTime);
38
        }
39
40 11
        if (isset($endTime)) {
41 6
            $state = $state->setEndTime($endTime);
42
        }
43
44 9
        $this->state = $state;
45
    }
46
47
    /**
48
     * Returns string representation of range.
49
     *
50
     * {@inheritdoc}
51
     */
52 4
    public function __toString(): string
53
    {
54 4
        return (string) $this->state;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 1
    public function __debugInfo()
61
    {
62
        return [
63 1
            'startTime' => $this->state->hasStartTime()
64 1
                ? $this->state->getStartTime()
65
                : null,
66 1
            'endTime' => $this->state->hasEndTime()
67 1
                ? $this->state->getEndTime()
68
                : null,
69
        ];
70
    }
71
72
    /**
73
     * Returns value ready to be encoded as JSON.
74
     *
75
     * The ISO-8601 range format is used for times.
76
     *
77
     * {@inheritdoc}
78
     */
79 4
    public function jsonSerialize(): array
80
    {
81 4
        return $this->state->jsonSerialize();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 4
    public function hasStartTime(): bool
88
    {
89 4
        return $this->state->hasStartTime();
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 4
    public function hasEndTime(): bool
96
    {
97 4
        return $this->state->hasEndTime();
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 6
    public function getStartTime(): DateTimeInterface
104
    {
105 6
        return $this->state->getStartTime();
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 6
    public function getEndTime(): DateTimeInterface
112
    {
113 6
        return $this->state->getEndTime();
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 4
    public function setStartTime(DateTimeInterface $time): DateRangeInterface
120
    {
121 4
        $clone = clone $this;
122 4
        $clone->state = $clone->state->setStartTime($time);
123
124 4
        return $clone;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 4
    public function setEndTime(DateTimeInterface $time): DateRangeInterface
131
    {
132 4
        $clone = clone $this;
133 4
        $clone->state = $clone->state->setEndTime($time);
134
135 4
        return $clone;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 4
    public function isStartAt(DateTimeInterface $time): bool
142
    {
143 4
        return $this->state->isStartAt($time);
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 4
    public function isEndAt(DateTimeInterface $time): bool
150
    {
151 4
        return $this->state->isEndAt($time);
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157 4
    public function isStarted(): bool
158
    {
159 4
        return $this->state->isStarted();
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165 4
    public function isEnded(): bool
166
    {
167 4
        return $this->state->isEnded();
168
    }
169
}
170