1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Speicher210\BusinessHours\Day; |
4
|
|
|
|
5
|
|
|
use Speicher210\BusinessHours\Day\Time\Time; |
6
|
|
|
use Speicher210\BusinessHours\Day\Time\TimeBuilder; |
7
|
|
|
use Speicher210\BusinessHours\Day\Time\TimeInterval; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Build a DayInterface concrete implementation. |
11
|
|
|
*/ |
12
|
|
|
class DayBuilder |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Create a new Day. |
16
|
|
|
* |
17
|
|
|
* @param integer $dayOfWeek The day of week. |
18
|
|
|
* @param array $openingIntervals The opening intervals. |
19
|
|
|
* @return Day |
20
|
|
|
*/ |
21
|
|
|
public static function fromArray($dayOfWeek, array $openingIntervals) |
22
|
|
|
{ |
23
|
|
|
$intervals = array(); |
24
|
|
|
foreach ($openingIntervals as $interval) { |
25
|
|
|
$intervals[] = new TimeInterval( |
26
|
|
|
TimeBuilder::fromString($interval[0]), |
27
|
|
|
TimeBuilder::fromString($interval[1]) |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return new Day($dayOfWeek, $intervals); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create a DayInterface object from an array. |
36
|
|
|
* |
37
|
|
|
* @param array $data The day data. |
38
|
|
|
* @return DayInterface |
39
|
|
|
*/ |
40
|
|
|
public static function fromAssociativeArray(array $data) |
41
|
|
|
{ |
42
|
|
View Code Duplication |
if (!isset($data['openingIntervals']) || !is_array($data['openingIntervals']) || !isset($data['dayOfWeek'])) { |
|
|
|
|
43
|
|
|
throw new \InvalidArgumentException('Array is not valid.'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$openingIntervals = array(); |
47
|
|
|
foreach ($data['openingIntervals'] as $openingInterval) { |
48
|
|
|
$start = TimeBuilder::fromArray($openingInterval['start']); |
49
|
|
|
$end = TimeBuilder::fromArray($openingInterval['end']); |
50
|
|
|
if (self::isIntervalAllDay($start, $end)) { |
51
|
|
|
return new AllDay($data['dayOfWeek']); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$openingIntervals[] = new TimeInterval($start, $end); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return new Day($data['dayOfWeek'], $openingIntervals); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Check if an interval array is all day. |
62
|
|
|
* |
63
|
|
|
* @param Time $start The start time. |
64
|
|
|
* @param Time $end The end time. |
65
|
|
|
* @return boolean |
66
|
|
|
*/ |
67
|
|
|
private static function isIntervalAllDay(Time $start, Time $end) |
68
|
|
|
{ |
69
|
|
View Code Duplication |
if ($start->getHours() !== 0 || $start->getMinutes() !== 0 || $start->getSeconds() !== 0) { |
|
|
|
|
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
View Code Duplication |
if ($end->getHours() !== 24 || $end->getMinutes() !== 0 || $end->getSeconds() !== 0) { |
|
|
|
|
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.