Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
72 / 72
100.00% covered (success)
100.00%
13 / 13
CRAP
100.00% covered (success)
100.00%
1 / 1
DayList
100.00% covered (success)
100.00%
72 / 72
100.00% covered (success)
100.00%
13 / 13
32
100.00% covered (success)
100.00%
1 / 1
 getDay
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
8
 getDayByDateTime
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDayByDay
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasDay
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 withAssociatedDays
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 setStatusByType
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 withAddedDayList
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 setSortByDate
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 setSort
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 hasDayWithAppointments
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getFirstBookableDay
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 withDaysInDateRange
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 withDaysFromPeriod
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace BO\Zmsentities\Collection;
4
5use BO\Zmsentities\Day;
6
7class DayList extends Base implements JsonUnindexed
8{
9    const ENTITY_CLASS = '\BO\Zmsentities\Day';
10
11    /**
12     * ATTENTION: Performance critical, keep highly optimized
13     *
14     */
15    public function getDay($year, $month, $dayNumber, $createDay = true)
16    {
17        $dateHash = Day::getCalculatedDayHash($dayNumber, $month, $year);
18        if ($this->offsetExists($dateHash)) {
19            return $this[$dateHash];
20        }
21        foreach ($this as $key => $day) {
22            if (!$day instanceof Day) {
23                $day = new Day($day);
24                $this[$key] = $day;
25            }
26            if ($day->day == $dayNumber && $day->month == $month && $day->year == $year) {
27                unset($this[$key]);
28                $this[$dateHash] = $day;
29                return $day;
30            }
31        }
32        if ($createDay) {
33            $day  = new \BO\Zmsentities\Day([
34                'year' => $year,
35                'month' => $month,
36                'day' => $dayNumber
37            ]);
38            $this[$dateHash] = $day;
39            return $day;
40        }
41        return null;
42    }
43
44    public function getDayByDateTime(\DateTimeInterface $datetime)
45    {
46        return $this->getDay($datetime->format('Y'), $datetime->format('m'), $datetime->format('d'));
47    }
48
49    public function getDayByDay(\BO\Zmsentities\Day $day)
50    {
51        return $this->getDay($day->year, $day->month, $day->day);
52    }
53
54
55    public function hasDay($year, $month, $dayNumber)
56    {
57        $day = $this->getDay($year, $month, $dayNumber, false);
58        return ($day === null) ? false : true;
59    }
60
61    public function withAssociatedDays($currentDate)
62    {
63        $dayList = new self();
64        $lastDay = $currentDate->format('t');
65        for ($dayNumber = 1; $dayNumber <= $lastDay; $dayNumber++) {
66            $day = str_pad($dayNumber, 2, '0', STR_PAD_LEFT);
67            $entity = $this->getDay($currentDate->format('Y'), $currentDate->format('m'), $day);
68            $dayList->addEntity($entity);
69        }
70        return $dayList->sortByCustomKey('day');
71    }
72
73    public function setStatusByType($slotType, \DateTimeInterface $dateTime)
74    {
75        foreach ($this as $day) {
76            $day->getWithStatus($slotType, $dateTime);
77        }
78        return $this;
79    }
80
81    public function withAddedDayList(DayList $dayList)
82    {
83        $merged = new DayList();
84        foreach ($dayList as $day) {
85            // @codeCoverageIgnoreStart
86            if (!$day instanceof Day) {
87                $day = new Day($day);
88            }
89            // @codeCoverageIgnoreEnd
90            $merged->addEntity($day->withAddedDay($this->getDayByDay($day)));
91        }
92        return $merged;
93    }
94
95    public function setSortByDate()
96    {
97        $this->uasort(function ($dayA, $dayB) {
98            return (
99                intval($dayA['year'] . $dayA['month'] . $dayA['day']) -
100                intval($dayB['year'] . $dayB['month'] . $dayB['day'])
101            );
102        });
103        return $this;
104    }
105
106    public function setSort($property = 'day')
107    {
108        $this->uasort(function ($dayA, $dayB) use ($property) {
109            return strnatcmp($dayA[$property], $dayB[$property]);
110        });
111        return $this;
112    }
113
114    public function hasDayWithAppointments()
115    {
116        foreach ($this as $hash => $day) {
117            $hash = null;
118            $day = new Day($day);
119            if ($day->hasAppointments()) {
120                return true;
121            }
122        }
123        return false;
124    }
125
126    public function getFirstBookableDay()
127    {
128        foreach ($this as $day) {
129            $day = new Day($day);
130            if ($day->isBookable()) {
131                return $day->toDateTime();
132            }
133        }
134        return null;
135    }
136
137    public function withDaysInDateRange(\DateTimeInterface $startDate, \DateTimeInterface $endDate)
138    {
139        $list = new self();
140        foreach ($this as $day) {
141            if (
142                $day->toDateTime() >= $startDate->modify('00:00:00') &&
143                $day->toDateTime() <= $endDate->modify('23:59:59')
144            ) {
145                $list->addEntity($day);
146            }
147        }
148        return $list;
149    }
150
151    public function withDaysFromPeriod(\DateTimeInterface $startDate, \DateTimeInterface $endDate)
152    {
153        $list = new self();
154        do {
155            $day = (new Day())->setDateTime($startDate);
156            $list->addEntity($day);
157            $startDate = $startDate->modify('+1 day');
158        } while ($startDate <= $endDate);
159        return $list;
160    }
161}