|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Webfactor\Laravel\OpeningHours\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Webfactor\Laravel\OpeningHours\Entities\OpeningHours; |
|
6
|
|
|
use Webfactor\Laravel\OpeningHours\Entities\OpeningHoursForDay; |
|
7
|
|
|
use Webfactor\Laravel\OpeningHours\Entities\TimeRange; |
|
8
|
|
|
use Webfactor\Laravel\OpeningHours\Exceptions\Exception; |
|
9
|
|
|
use Webfactor\Laravel\OpeningHours\Models\DayOpenTimeRange; |
|
10
|
|
|
|
|
11
|
|
|
trait OpeningHoursAttribute |
|
12
|
|
|
{ |
|
13
|
6 |
|
public function getOpeningHoursAttribute() |
|
|
|
|
|
|
14
|
|
|
{ |
|
15
|
6 |
|
if (!key_exists('opening_hours', $this->attributes) || !$this->attributes['opening_hours']) { |
|
16
|
4 |
|
$hours = $this->dayOpenTimeRanges |
|
|
|
|
|
|
17
|
|
|
->groupBy('day')->map(function ($day) { |
|
18
|
2 |
|
return $day->map(function (DayOpenTimeRange $range) { |
|
19
|
2 |
|
return $range->start.'-'.$range->end; |
|
20
|
2 |
|
}); |
|
21
|
4 |
|
}); |
|
22
|
|
|
|
|
23
|
4 |
|
$this->attributes['opening_hours'] = OpeningHours::create($hours->toArray()); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
6 |
|
return $this->attributes['opening_hours']; |
|
27
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
14 |
|
public function setOpeningHoursAttribute($data) |
|
31
|
|
|
{ |
|
32
|
|
|
// clear previous open times |
|
33
|
14 |
|
$this->attributes['opening_hours'] = null; |
|
34
|
14 |
|
$this->dayOpenTimeRanges()->delete(); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
14 |
|
if ($data == null) { |
|
37
|
2 |
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
12 |
|
if ($data instanceof OpeningHours) { |
|
41
|
1 |
|
$this->applyOpeningHours($data); |
|
42
|
1 |
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
11 |
|
if (is_array($data) || is_object($data)) { |
|
46
|
11 |
|
$this->applyOpeningHours(OpeningHours::create((array) $data)); |
|
47
|
11 |
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
throw new Exception("Invalid argument `{$data}` applied to opening hours attribute."); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
12 |
|
private function applyOpeningHours(OpeningHours $openingHours) |
|
54
|
|
|
{ |
|
55
|
12 |
|
$entries = $this->parseOpeningHoursForDB($openingHours); |
|
56
|
12 |
|
$this->dayOpenTimeRanges()->saveMany($entries); |
|
|
|
|
|
|
57
|
12 |
|
$this->attributes['opening_hours'] = $openingHours; |
|
58
|
12 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function parseOpeningHoursForDB(OpeningHours $openingHours) |
|
61
|
|
|
{ |
|
62
|
|
|
$ranges = $openingHours->flatMap(function (OpeningHoursForDay $openingHoursForDay, string $day) { |
|
63
|
12 |
|
return $openingHoursForDay->map(function (TimeRange $timeRange) use ($day) { |
|
64
|
|
|
return [ |
|
65
|
12 |
|
'day' => $day, |
|
66
|
12 |
|
'start' => $timeRange->start(), |
|
67
|
12 |
|
'end' => $timeRange->end() |
|
68
|
|
|
]; |
|
69
|
12 |
|
}); |
|
70
|
12 |
|
}); |
|
71
|
|
|
|
|
72
|
12 |
|
return collect($ranges)->map(function ($range) { |
|
73
|
12 |
|
return new DayOpenTimeRange($range); |
|
74
|
12 |
|
}); |
|
75
|
|
|
} |
|
76
|
|
|
} |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.