1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Speicher210\BusinessHours; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Build a DayInterface concrete implementation. |
7
|
|
|
*/ |
8
|
|
|
class DayBuilder |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Create a DayInterface object from an array. |
12
|
|
|
* |
13
|
|
|
* @param array $data The day data. |
14
|
|
|
* @return DayInterface |
15
|
|
|
*/ |
16
|
|
|
public static function fromArray(array $data) |
17
|
|
|
{ |
18
|
|
|
if (!isset($data['openingIntervals']) || !is_array($data['openingIntervals']) || !isset($data['dayOfWeek'])) { |
19
|
|
|
throw new \InvalidArgumentException('Array is not valid.'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
if (count($data['openingIntervals']) === 1 && self::isIntervalArrayAllDay(reset($data['openingIntervals']))) { |
23
|
|
|
return new AllDay($data['dayOfWeek']); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$openingIntervals = array(); |
27
|
|
|
foreach ($data['openingIntervals'] as $openingInterval) { |
28
|
|
|
$start = new Time( |
29
|
|
|
$openingInterval['start']['hours'], |
30
|
|
|
isset($openingInterval['start']['minutes']) ? $openingInterval['start']['minutes'] : 0, |
31
|
|
|
isset($openingInterval['start']['seconds']) ? $openingInterval['start']['seconds'] : 0 |
32
|
|
|
); |
33
|
|
|
$end = new Time( |
34
|
|
|
$openingInterval['end']['hours'], |
35
|
|
|
isset($openingInterval['end']['minutes']) ? $openingInterval['end']['minutes'] : 0, |
36
|
|
|
isset($openingInterval['end']['seconds']) ? $openingInterval['end']['seconds'] : 0 |
37
|
|
|
); |
38
|
|
|
$openingIntervals[] = array($start, $end); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return new Day($data['dayOfWeek'], $openingIntervals); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Check if an interval array is all day. |
46
|
|
|
* |
47
|
|
|
* @param array $data The interval as an array to check. |
48
|
|
|
* @return boolean |
49
|
|
|
*/ |
50
|
|
|
private static function isIntervalArrayAllDay(array $data) |
51
|
|
|
{ |
52
|
|
|
if ($data['start']['hours'] != 0 || $data['end']['hours'] != 24) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
if (isset($data['start']['minutes']) && $data['start']['minutes'] != 0) { |
|
|
|
|
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
if (isset($data['end']['minutes']) && $data['end']['minutes'] != 0) { |
|
|
|
|
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
View Code Duplication |
if (isset($data['start']['seconds']) && $data['start']['seconds'] != 0) { |
|
|
|
|
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
if (isset($data['end']['seconds']) && $data['end']['seconds'] != 0) { |
|
|
|
|
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: