|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WITR\Schedule; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
|
|
7
|
|
|
class WeeklySchedule extends Collection |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
public static function mergeFromTimeSlots(Collection $timeSlots) |
|
11
|
|
|
{ |
|
12
|
|
|
$scheduledShows = new Collection(); |
|
13
|
|
|
$timeSlots = $timeSlots->reduce(function($scheduledShows, $timeslot) { |
|
14
|
|
|
$scheduledShow = $scheduledShows->last(); |
|
15
|
|
|
if (static::showContinuesIntoCurrentTimeslot($scheduledShow, $timeslot)) |
|
|
|
|
|
|
16
|
|
|
{ |
|
17
|
|
|
$scheduledShow->extendShowByHour(); |
|
18
|
|
|
} |
|
19
|
|
|
else |
|
20
|
|
|
{ |
|
21
|
|
|
$show = ScheduledShow::fromShowAndDJ($timeslot->showForTimeslot, $timeslot->djForTimeslot); |
|
22
|
|
|
$show->setId($timeslot->id); |
|
23
|
|
|
$show->startsAt($timeslot->hour); |
|
24
|
|
|
$show->airsDayOfWeek($timeslot->day); |
|
25
|
|
|
$scheduledShows->push($show); |
|
26
|
|
|
} |
|
27
|
|
|
return $scheduledShows; |
|
28
|
|
|
}, $scheduledShows); |
|
29
|
|
|
$weeklySchedule = new WeeklySchedule($timeSlots); |
|
30
|
|
|
|
|
31
|
|
|
return $weeklySchedule; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public static function fromTimeSlots(Collection $timeSlots) |
|
35
|
|
|
{ |
|
36
|
|
|
$timeSlots = $timeSlots->map(function($timeslot) { |
|
37
|
|
|
$show = ScheduledShow::fromShowAndDJ($timeslot->showForTimeslot, $timeslot->djForTimeslot); |
|
38
|
|
|
$show->setId($timeslot->id); |
|
39
|
|
|
$show->startsAt($timeslot->hour); |
|
40
|
|
|
$show->airsDayOfWeek($timeslot->day); |
|
41
|
|
|
return $show; |
|
42
|
|
|
}); |
|
43
|
|
|
|
|
44
|
|
|
return new WeeklySchedule($timeSlots); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private static function showContinuesIntoCurrentTimeslot($scheduledShow, $timeslot) |
|
48
|
|
|
{ |
|
49
|
|
|
return $scheduledShow != null |
|
50
|
|
|
&& $scheduledShow->djId() == $timeslot->djForTimeslot->id |
|
51
|
|
|
&& $scheduledShow->showId() == $timeslot->showForTimeslot->id |
|
52
|
|
|
&& $scheduledShow->airDayOfWeek() == $timeslot->day; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function scheduleFor($weekday) |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->filter(function($timeslot) use ($weekday) { |
|
58
|
|
|
return $timeslot->airDayOfWeek() == $weekday; |
|
59
|
|
|
}); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getShowsForSlideshow() |
|
63
|
|
|
{ |
|
64
|
|
|
$shows = new Collection(); |
|
65
|
|
|
$sortedShows = $this->getShowsInPlayOrder(); |
|
66
|
|
|
$shows->push($sortedShows->shift()); |
|
67
|
|
|
$nonPulseShows = $sortedShows->filter(function ($show) { |
|
68
|
|
|
return strtolower($show->show()) != 'the pulse of music'; |
|
69
|
|
|
}); |
|
70
|
|
|
|
|
71
|
|
|
$shows = $shows->merge($nonPulseShows->take(2)); |
|
72
|
|
|
|
|
73
|
|
|
do |
|
74
|
|
|
{ |
|
75
|
|
|
$random = $nonPulseShows->random(); |
|
76
|
|
|
$exists = $shows->filter(function($show) use($random) { |
|
77
|
|
|
return $show->showId() == $random->showId(); |
|
78
|
|
|
}); |
|
79
|
|
|
} while(!$exists->isEmpty()); |
|
80
|
|
|
|
|
81
|
|
|
$shows->push($random); |
|
82
|
|
|
|
|
83
|
|
|
return $shows; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getShowsInPlayOrder() |
|
87
|
|
|
{ |
|
88
|
|
|
$first = $this->first(function ($key, $show) { |
|
89
|
|
|
return $show->nowPlaying(); |
|
90
|
|
|
}); |
|
91
|
|
|
$firstKey = $this->search($first); |
|
92
|
|
|
$sorted = $this->slice($firstKey); |
|
93
|
|
|
$firstHalfSorted = $this->slice(0, $firstKey); |
|
94
|
|
|
return $sorted->merge($firstHalfSorted); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: