1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HelePartnerSyncApi\Method; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use HelePartnerSyncApi\Validator; |
7
|
|
|
use HelePartnerSyncApi\ValidatorException; |
8
|
|
|
|
9
|
|
|
class GetSlots extends Method |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @return string |
14
|
|
|
*/ |
15
|
|
|
public function getName() |
16
|
|
|
{ |
17
|
|
|
return 'getSlots'; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param array $data |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
protected function parseRequestData($data) |
25
|
|
|
{ |
26
|
|
|
Validator::checkStructure($data, array( |
27
|
|
|
'date' => Validator::TYPE_DATE_TIME_STRING, |
28
|
|
|
'parameters' => Validator::TYPE_ARRAY, |
29
|
|
|
)); |
30
|
|
|
|
31
|
|
|
return array( |
32
|
|
|
new DateTime($data['date']), |
33
|
|
|
$data['parameters'], |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param mixed $data |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
protected function constructResponseData($data) |
42
|
|
|
{ |
43
|
|
|
Validator::checkStructure($data, array( |
44
|
|
|
array( |
45
|
|
|
'startDateTime' => Validator::TYPE_DATE_TIME, |
46
|
|
|
'endDateTime' => Validator::TYPE_DATE_TIME, |
47
|
|
|
'capacity' => Validator::TYPE_INT, |
48
|
|
|
), |
49
|
|
|
)); |
50
|
|
|
|
51
|
|
|
$result = array(); |
52
|
|
|
|
53
|
|
|
foreach ($data as $index => $slot) { |
54
|
|
|
$whichSlot = sprintf('(slot #%d)', $index + 1); |
55
|
|
|
$startDateTime = $slot['startDateTime']->format(DateTime::W3C); |
56
|
|
|
$endDateTime = $slot['endDateTime']->format(DateTime::W3C); |
57
|
|
|
$capacity = $slot['capacity']; |
58
|
|
|
|
59
|
|
|
if ($startDateTime >= $endDateTime) { |
60
|
|
|
throw new ValidatorException(sprintf('Slot startDateTime (%s) must be before endDateTime (%s) %s', $startDateTime, $endDateTime, $whichSlot)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($capacity < 0) { |
64
|
|
|
throw new ValidatorException(sprintf('Slot capacity (%s) must be non-negative %s', $capacity, $whichSlot)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$result[] = array( |
68
|
|
|
'startDateTime' => $startDateTime, |
69
|
|
|
'endDateTime' => $endDateTime, |
70
|
|
|
'capacity' => $capacity, |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $result; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|