Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.92% |
63 / 65 |
|
94.74% |
18 / 19 |
CRAP | |
0.00% |
0 / 1 |
| Appointment | |
96.92% |
63 / 65 |
|
94.74% |
18 / 19 |
30 | |
0.00% |
0 / 1 |
| getDefaults | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| toDate | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| toTime | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| hasTime | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| setTime | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| setDateTime | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addDate | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addScope | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getScope | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| addSlotCount | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getSlotCount | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAvailability | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| toDateTime | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getStartTime | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getEndTime | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getEndTimeWithCustomSlotTime | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setDateByString | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| isMatching | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| __toString | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BO\Zmsentities; |
| 4 | |
| 5 | use BO\Zmsentities\Helper\Property; |
| 6 | |
| 7 | /** |
| 8 | * Schema-backed entity (ArrayObject::ARRAY_AS_PROPS); document dynamic keys for Psalm. |
| 9 | * |
| 10 | * @property int $date |
| 11 | */ |
| 12 | class Appointment extends Schema\Entity |
| 13 | { |
| 14 | public const string PRIMARY = 'id'; |
| 15 | |
| 16 | public static $schema = "appointment.json"; |
| 17 | |
| 18 | /** |
| 19 | * @return (Availability|Scope|int)[] |
| 20 | * |
| 21 | */ |
| 22 | #[\Override] |
| 23 | public function getDefaults() |
| 24 | { |
| 25 | return [ |
| 26 | 'date' => 0, |
| 27 | 'scope' => new Scope(), |
| 28 | 'availability' => new Availability(), |
| 29 | 'slotCount' => 0, |
| 30 | ]; |
| 31 | } |
| 32 | |
| 33 | public function toDate($lang = 'de') |
| 34 | { |
| 35 | // Mittwoch 18. November 2015 |
| 36 | return ($lang == 'en') ? date('l F d, Y', $this->date) : Helper\DateTime::getFormatedDates( |
| 37 | $this->toDateTime(), |
| 38 | 'EEEE dd. MMMM yyyy' |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | public function toTime($lang = 'de'): string |
| 43 | { |
| 44 | $suffix = ($lang == 'en') ? ' o\'clock' : ' Uhr'; |
| 45 | return date('H:i', $this->date) . $suffix; |
| 46 | } |
| 47 | |
| 48 | public function hasTime(): bool |
| 49 | { |
| 50 | if ($this->date == 0) { |
| 51 | return false; |
| 52 | } |
| 53 | $time = $this->toDateTime(); |
| 54 | return ('00:00' != $time->format('H:i')); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Modify time for appointment |
| 59 | */ |
| 60 | public function setTime($timeString): static |
| 61 | { |
| 62 | $dateTime = $this->toDateTime(); |
| 63 | $this->date = $dateTime->modify($timeString)->getTimestamp(); |
| 64 | return $this; |
| 65 | } |
| 66 | |
| 67 | public function setDateTime(\DateTimeInterface $dateTime): static |
| 68 | { |
| 69 | $this->date = $dateTime->getTimestamp(); |
| 70 | return $this; |
| 71 | } |
| 72 | |
| 73 | public function addDate(int $date): static |
| 74 | { |
| 75 | $this->date = $date; |
| 76 | return $this; |
| 77 | } |
| 78 | |
| 79 | public function addScope($scopeId): static |
| 80 | { |
| 81 | $this->getScope()->id = $scopeId; |
| 82 | return $this; |
| 83 | } |
| 84 | |
| 85 | public function getScope(): Scope |
| 86 | { |
| 87 | $this->scope = ($this->toProperty()->scope->isAvailable()) ? new Scope($this['scope']) : new Scope(); |
| 88 | return $this->scope; |
| 89 | } |
| 90 | |
| 91 | public function addSlotCount(int|null $slotCount = null): static |
| 92 | { |
| 93 | if ($slotCount) { |
| 94 | $this->slotCount = $slotCount; |
| 95 | } else { |
| 96 | $this->slotCount += 1; |
| 97 | } |
| 98 | |
| 99 | return $this; |
| 100 | } |
| 101 | |
| 102 | public function getSlotCount() |
| 103 | { |
| 104 | return $this->slotCount; |
| 105 | } |
| 106 | |
| 107 | public function getAvailability(): Availability |
| 108 | { |
| 109 | $data = array(); |
| 110 | if (Property::__keyExists('availability', $this)) { |
| 111 | $data = $this['availability']; |
| 112 | } |
| 113 | return new Availability($data); |
| 114 | } |
| 115 | |
| 116 | public function toDateTime($timezone = 'Europe/Berlin'): \DateTimeImmutable |
| 117 | { |
| 118 | $date = (new \DateTimeImmutable())->setTimestamp($this->date); |
| 119 | //$date = \DateTimeImmutable::createFromFormat("U", $this->date); |
| 120 | if ($date) { |
| 121 | $date = $date->setTimeZone(new \DateTimeZone($timezone)); |
| 122 | } |
| 123 | return $date; |
| 124 | } |
| 125 | |
| 126 | public function getStartTime() |
| 127 | { |
| 128 | $time = $this->toDateTime(); |
| 129 | return $time; |
| 130 | } |
| 131 | |
| 132 | public function getEndTime() |
| 133 | { |
| 134 | $time = $this->getStartTime(); |
| 135 | $availability = $this->getAvailability(); |
| 136 | return ($availability->slotTimeInMinutes) |
| 137 | ? $time->modify('+' . ($availability->slotTimeInMinutes * $this->slotCount) . ' minutes') |
| 138 | : $time; |
| 139 | } |
| 140 | |
| 141 | public function getEndTimeWithCustomSlotTime($slotTimeInMinutes) |
| 142 | { |
| 143 | $time = $this->getStartTime(); |
| 144 | return $time->modify('+' . ($slotTimeInMinutes * $this->slotCount) . ' minutes'); |
| 145 | } |
| 146 | |
| 147 | public function setDateByString(string $dateString, $format = 'Y-m-d H:i'): static |
| 148 | { |
| 149 | $appointmentDateTime = \DateTimeImmutable::createFromFormat($format, $dateString); |
| 150 | if ($appointmentDateTime) { |
| 151 | $this->date = $appointmentDateTime->format('U'); |
| 152 | } else { |
| 153 | throw new Exception\DateStringWrongFormat( |
| 154 | "String " . htmlspecialchars($dateString) . " not format " . htmlspecialchars($format) |
| 155 | ); |
| 156 | } |
| 157 | return $this; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Does two appointments match, the matching appointment might have a lower slot count |
| 162 | */ |
| 163 | public function isMatching(self $appointment): bool |
| 164 | { |
| 165 | if ( |
| 166 | $appointment['scope']['id'] == $this['scope']['id'] |
| 167 | && $appointment['date'] == $this['date'] |
| 168 | ) { |
| 169 | return true; |
| 170 | } |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | public function __toString() |
| 175 | { |
| 176 | return "appointment#" |
| 177 | . $this->toDateTime()->format('c') |
| 178 | . " " . $this['slotCount'] . "slots" |
| 179 | . " scope" . $this['scope']['id'] |
| 180 | ; |
| 181 | } |
| 182 | } |