|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Timegridio\Concierge\Timetable; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
|
|
7
|
|
|
class Timetable |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Timetable matrix. |
|
11
|
|
|
* |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $timetable = null; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* DateTime keyword for the begining of the timetable. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $from = 'today'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Number of days to build the timetable forward. |
|
25
|
|
|
* |
|
26
|
|
|
* @var int |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $future = 1; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Starting time for each day. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $startAt = '09:00:00'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Finishing time for each day. |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $finishAt = '18:00:00'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Interval between slots in minutes. |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $interval = 30; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Services. |
|
53
|
|
|
* |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $services = []; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Dimensions format of the timetable matrix. |
|
60
|
|
|
* |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $dimensions = ['date', 'service', 'time']; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Setter for beginning date. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $relative |
|
69
|
|
|
* |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
|
|
public function from($relative) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->from = $relative; |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Setter for number of days forward. |
|
81
|
|
|
* |
|
82
|
|
|
* @param int $days |
|
83
|
|
|
* |
|
84
|
|
|
* @return $this |
|
85
|
|
|
*/ |
|
86
|
|
|
public function future($days) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->future = $days; |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Setter for startAt time. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $time |
|
97
|
|
|
* |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
|
|
public function startAt($time) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->startAt = $time; |
|
103
|
|
|
|
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Setter for finishAt time. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $time |
|
111
|
|
|
* |
|
112
|
|
|
* @return $this |
|
113
|
|
|
*/ |
|
114
|
|
|
public function finishAt($time) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->finishAt = $time; |
|
117
|
|
|
|
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Setter for interval. |
|
123
|
|
|
* |
|
124
|
|
|
* @param int $minutes |
|
|
|
|
|
|
125
|
|
|
* |
|
126
|
|
|
* @return $this |
|
127
|
|
|
*/ |
|
128
|
|
|
public function interval($interval = 30) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->interval = $interval; |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Setter for services. |
|
137
|
|
|
* |
|
138
|
|
|
* @param array $services |
|
139
|
|
|
* |
|
140
|
|
|
* @return $this |
|
141
|
|
|
*/ |
|
142
|
|
|
public function services($services) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->services = $services; |
|
145
|
|
|
|
|
146
|
|
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Initialize Timetable. |
|
151
|
|
|
* |
|
152
|
|
|
* @return $this |
|
153
|
|
|
*/ |
|
154
|
|
|
public function init() |
|
155
|
|
|
{ |
|
156
|
|
|
$this->timetable = []; |
|
157
|
|
|
|
|
158
|
|
|
$dimensions['service'] = $this->inflateServices(); |
|
|
|
|
|
|
159
|
|
|
$dimensions['date'] = $this->inflateDates(); |
|
160
|
|
|
$dimensions['time'] = $this->inflateTimes(); |
|
161
|
|
|
|
|
162
|
|
|
foreach ($dimensions['service'] as $service) { |
|
163
|
|
|
foreach ($dimensions['date'] as $date) { |
|
164
|
|
|
foreach ($dimensions['time'] as $time) { |
|
165
|
|
|
$this->capacity($date, $time, $service, 0); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function inflateServices() |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->services; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function inflateDates() |
|
179
|
|
|
{ |
|
180
|
|
|
$starting = $this->from; |
|
181
|
|
|
|
|
182
|
|
|
for ($i = 0; $i < $this->future; $i++) { |
|
183
|
|
|
$date = date('Y-m-d', strtotime("$starting +$i days")); |
|
184
|
|
|
$dates[$date] = $date; |
|
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return $dates; |
|
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
public function inflateTimes() |
|
191
|
|
|
{ |
|
192
|
|
|
$interval = $this->interval; |
|
193
|
|
|
|
|
194
|
|
|
$start = strtotime('today '.$this->startAt); |
|
195
|
|
|
$finish = strtotime('today '.$this->finishAt); |
|
196
|
|
|
|
|
197
|
|
|
$times = []; |
|
198
|
|
|
for ($i = $start; $i < $finish; $i += $interval * 60) { |
|
199
|
|
|
$time = date('H:i:s', $i); |
|
200
|
|
|
$times[$time] = $time; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return $times; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Get the Timetable matrix. |
|
208
|
|
|
* |
|
209
|
|
|
* @return array |
|
210
|
|
|
*/ |
|
211
|
|
|
public function get() |
|
212
|
|
|
{ |
|
213
|
|
|
if ($this->timetable === null) { |
|
214
|
|
|
$this->init(); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return $this->timetable; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Set the capacity for a slot. |
|
222
|
|
|
* |
|
223
|
|
|
* @param string $date |
|
224
|
|
|
* @param string $time |
|
225
|
|
|
* @param string $service |
|
226
|
|
|
* @param int $capacity |
|
227
|
|
|
* |
|
228
|
|
|
* @return int |
|
229
|
|
|
*/ |
|
230
|
|
|
public function capacity($date, $time, $service, $capacity = null) |
|
231
|
|
|
{ |
|
232
|
|
|
$path = $this->dimensions(compact('date', 'service', 'time')); |
|
233
|
|
|
|
|
234
|
|
|
return $capacity === null |
|
235
|
|
|
? $this->array_get($this->timetable, $path) |
|
236
|
|
|
: $this->array_set($this->timetable, $path, $capacity); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Get a concrete Timetable path. |
|
241
|
|
|
* |
|
242
|
|
|
* @param array $segments |
|
243
|
|
|
* |
|
244
|
|
|
* @return string |
|
245
|
|
|
*/ |
|
246
|
|
|
private function dimensions(array $segments) |
|
247
|
|
|
{ |
|
248
|
|
|
$translatedDimensions = $this->dimensions; |
|
249
|
|
|
|
|
250
|
|
|
$this->array_substitute($translatedDimensions, $segments); |
|
251
|
|
|
|
|
252
|
|
|
return implode('.', $translatedDimensions); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Setter for the dimensions format. |
|
257
|
|
|
* |
|
258
|
|
|
* @param string $dimensions |
|
259
|
|
|
* |
|
260
|
|
|
* @return $this |
|
261
|
|
|
*/ |
|
262
|
|
|
public function format($dimensions) |
|
263
|
|
|
{ |
|
264
|
|
|
if (is_array($dimensions)) { |
|
265
|
|
|
$this->dimensions = $dimensions; |
|
|
|
|
|
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
if (is_string($dimensions)) { |
|
269
|
|
|
$this->dimensions = explode('.', $dimensions); |
|
|
|
|
|
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
return $this; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
///////////// |
|
276
|
|
|
// Helpers // |
|
277
|
|
|
///////////// |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Helper method for Arr::set |
|
281
|
|
|
* |
|
282
|
|
|
* @param array &$array |
|
283
|
|
|
* @param mixed $key |
|
284
|
|
|
* @param mixed $value |
|
285
|
|
|
* |
|
286
|
|
|
* @return mixed |
|
287
|
|
|
*/ |
|
288
|
|
|
private function array_set(&$array, $key, $value) |
|
289
|
|
|
{ |
|
290
|
|
|
return Arr::set($array, $key, $value); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Helper method for Arr::get |
|
295
|
|
|
* |
|
296
|
|
|
* @param array &$array |
|
297
|
|
|
* @param mixed $key |
|
298
|
|
|
* @param mixed $default |
|
299
|
|
|
* |
|
300
|
|
|
* @return mixed |
|
301
|
|
|
*/ |
|
302
|
|
|
private function array_get($array, $key, $default = null) |
|
303
|
|
|
{ |
|
304
|
|
|
return Arr::get($array, $key, $default); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
private function array_substitute(&$array1, $array2) |
|
308
|
|
|
{ |
|
309
|
|
|
foreach ($array1 as $key => $value) { |
|
310
|
|
|
$array1[$key] = $array2[$value]; |
|
311
|
|
|
} |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.