1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File was created 12.04.2016 06:54 |
4
|
|
|
*/ |
5
|
|
|
namespace PeekAndPoke\Types; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @api |
9
|
|
|
* |
10
|
|
|
* @author Karsten J. Gerber <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class LocalTimeslot |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var LocalDate |
16
|
|
|
* |
17
|
|
|
* @PeekAndPoke\Component\Slumber\Annotation\Slumber\AsLocalDate() |
18
|
|
|
*/ |
19
|
|
|
private $from; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var LocalDate |
23
|
|
|
* |
24
|
|
|
* @PeekAndPoke\Component\Slumber\Annotation\Slumber\AsLocalDate() |
25
|
|
|
*/ |
26
|
|
|
private $to; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return LocalTimeslot |
30
|
|
|
*/ |
31
|
14 |
|
public static function createEmpty() |
32
|
|
|
{ |
33
|
14 |
|
return new static(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param LocalDate $from |
38
|
|
|
* @param LocalDate $to |
39
|
|
|
* |
40
|
|
|
* @return LocalTimeslot |
41
|
|
|
*/ |
42
|
10 |
|
public static function from(LocalDate $from, LocalDate $to) |
43
|
|
|
{ |
44
|
10 |
|
$ret = new static(); |
45
|
10 |
|
$ret->from = $from; |
46
|
10 |
|
$ret->to = $to; |
47
|
|
|
|
48
|
10 |
|
return $ret; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* LocalTimeslot cannot be constructed directly. |
53
|
|
|
* |
54
|
|
|
* @see from() |
55
|
|
|
* @see createEmpty() |
56
|
|
|
*/ |
57
|
24 |
|
protected function __construct() |
58
|
|
|
{ |
59
|
24 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
4 |
|
public function __toString() |
65
|
|
|
{ |
66
|
4 |
|
return ($this->from ? $this->from->format() : '?') . ' - ' . ($this->to ? $this->to->format() : '?'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
9 |
|
public function isValid() |
73
|
|
|
{ |
74
|
9 |
|
return $this->from !== null |
75
|
9 |
|
&& $this->to !== null |
76
|
9 |
|
&& $this->from->isBefore($this->to); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param LocalTimeslot $other |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
9 |
|
public function equals(LocalTimeslot $other = null) |
85
|
|
|
{ |
86
|
9 |
|
if ($this === $other) { |
87
|
1 |
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return |
91
|
|
|
// is the other there ? |
92
|
8 |
|
$other !== null |
93
|
|
|
// is the from the same ? |
94
|
|
|
&& ( |
95
|
7 |
|
($this->from === null && $other->from === null) || |
96
|
8 |
|
($this->from !== null && $this->from->isEqual($other->from)) |
97
|
|
|
) |
98
|
|
|
// is the to the same ? |
99
|
|
|
&& ( |
100
|
4 |
|
($this->to === null && $other->to === null) || |
101
|
8 |
|
($this->to !== null && $this->to->isEqual($other->to)) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return LocalDate |
107
|
|
|
*/ |
108
|
10 |
|
public function getFrom() |
109
|
|
|
{ |
110
|
10 |
|
return $this->from; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return LocalDate |
115
|
|
|
*/ |
116
|
10 |
|
public function getTo() |
117
|
|
|
{ |
118
|
10 |
|
return $this->to; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param LocalDate $from |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
12 |
|
public function setFrom(LocalDate $from = null) |
127
|
|
|
{ |
128
|
12 |
|
$this->from = $from; |
129
|
|
|
|
130
|
12 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param LocalDate $to |
135
|
|
|
* |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
12 |
|
public function setTo(LocalDate $to = null) |
139
|
|
|
{ |
140
|
12 |
|
$this->to = $to; |
141
|
|
|
|
142
|
12 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Creates a new instance and moves the timeslot by the given number of hours |
147
|
|
|
* |
148
|
|
|
* @param float $numHours |
149
|
|
|
* |
150
|
|
|
* @return LocalTimeslot |
151
|
|
|
*/ |
152
|
3 |
|
public function modifyByHours($numHours) |
153
|
|
|
{ |
154
|
3 |
|
return self::from( |
155
|
3 |
|
$this->from->modifyByHours($numHours), |
156
|
3 |
|
$this->to->modifyByHours($numHours) |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Creates a new instance and moves the timeslot by the given number of days |
162
|
|
|
* |
163
|
|
|
* @param int $numDays |
164
|
|
|
* |
165
|
|
|
* @return LocalTimeslot |
166
|
|
|
*/ |
167
|
3 |
|
public function modifyByDays($numDays) |
168
|
|
|
{ |
169
|
3 |
|
return self::from( |
170
|
3 |
|
$this->from->modifyByDays($numDays), |
171
|
3 |
|
$this->to->modifyByDays($numDays) |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Creates a new instance and moves the timeslot by the given number of days |
177
|
|
|
* Takes into consideration the daylight savings possible issues |
178
|
|
|
* |
179
|
|
|
* @param int $numDays |
180
|
|
|
* |
181
|
|
|
* @return LocalTimeslot |
182
|
|
|
*/ |
183
|
3 |
|
public function modifyByDaysDaylightSavingAware($numDays) |
184
|
|
|
{ |
185
|
3 |
|
return self::from( |
186
|
3 |
|
$this->from->modifyByDaysDaylightSavingAware($numDays), |
187
|
3 |
|
$this->to->modifyByDaysDaylightSavingAware($numDays) |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return int |
193
|
|
|
*/ |
194
|
2 |
|
public function getDurationInSecs() |
195
|
|
|
{ |
196
|
2 |
|
if ($this->from && $this->to) { |
197
|
1 |
|
return $this->to->getTimestamp() - $this->from->getTimestamp(); |
198
|
|
|
} |
199
|
|
|
|
200
|
1 |
|
return 0; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return float |
205
|
|
|
*/ |
206
|
2 |
|
public function getDurationInMins() |
207
|
|
|
{ |
208
|
2 |
|
return $this->getDurationInSecs() / 60; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|