Lines
100.00%
41 / 41
Functions and Methods
100.00%
8 / 8
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| DayoffList | 100.00% | 41 / 41 | 100.00% | 8 / 8 | 22 | 100.00% | 1 / 1 | |||
| hasEntityByDate | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | |||||
| getByDate | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | |||||
| getEntityByName | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | |||||
| withTimestampFromDateformat | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 2 | |||||
| testDatesInYear | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 3 | |||||
| testUniqueDates | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 3 | |||||
| withNew | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | |||||
| isNewerThan | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 3 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace BO\Zmsentities\Collection; | |
| 4 | ||
| 5 | /** | |
| 6 | * @extends Base<\BO\Zmsentities\Dayoff> | |
| 7 | */ | |
| 8 | class DayoffList extends Base | |
| 9 | { | |
| 10 | public const string ENTITY_CLASS = '\BO\Zmsentities\Dayoff'; | |
| 11 | ||
| 12 | public function hasEntityByDate(string|\DateTimeInterface $date): bool | |
| 13 | { | |
| 14 | return $this->getByDate($date) ? true : false; | |
| 15 | } | |
| 16 | ||
| 17 | public function getByDate(string|\DateTimeInterface $date): \BO\Zmsentities\Dayoff|false | |
| 18 | { | |
| 19 | $date = \BO\Zmsentities\Helper\DateTime::create($date)->format('Y-m-d'); | |
| 20 | foreach ($this as $entity) { | |
| 21 | if ($entity->getDateTime()->format('Y-m-d') == $date) { | |
| 22 | return $entity; | |
| 23 | } | |
| 24 | } | |
| 25 | return false; | |
| 26 | } | |
| 27 | ||
| 28 | public function getEntityByName(mixed $name): \BO\Zmsentities\Dayoff|null | |
| 29 | { | |
| 30 | $result = null; | |
| 31 | foreach ($this as $entity) { | |
| 32 | if ($entity->name == $name) { | |
| 33 | $result = $entity; | |
| 34 | } | |
| 35 | } | |
| 36 | return $result; | |
| 37 | } | |
| 38 | ||
| 39 | public function withTimestampFromDateformat(string $fromFormat = 'd.m.Y'): self | |
| 40 | { | |
| 41 | $collection = new self(); | |
| 42 | foreach ($this as $data) { | |
| 43 | $entity = new \BO\Zmsentities\Dayoff($data); // if source is an array | |
| 44 | $entity = clone $entity; | |
| 45 | $entity->setTimestampFromDateformat($fromFormat); | |
| 46 | $collection->addEntity($entity); | |
| 47 | } | |
| 48 | return $collection; | |
| 49 | } | |
| 50 | ||
| 51 | /** | |
| 52 | * @return true | |
| 53 | */ | |
| 54 | public function testDatesInYear(mixed $year): bool | |
| 55 | { | |
| 56 | foreach ($this as $data) { | |
| 57 | $entity = new \BO\Zmsentities\Dayoff($data); // if source is an array | |
| 58 | $entityYear = (new \DateTimeImmutable())->setTimestamp($entity->date)->format('Y'); | |
| 59 | if ($entityYear != $year) { | |
| 60 | throw new \BO\Zmsentities\Exception\DayoffWrongYear(); | |
| 61 | } | |
| 62 | } | |
| 63 | return true; | |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * @return true | |
| 68 | */ | |
| 69 | public function testUniqueDates(): bool | |
| 70 | { | |
| 71 | $seen = []; | |
| 72 | foreach ($this as $data) { | |
| 73 | $entity = new \BO\Zmsentities\Dayoff($data); | |
| 74 | $day = $entity->getDateTime()->format('Y-m-d'); | |
| 75 | if (isset($seen[$day])) { | |
| 76 | throw new \BO\Zmsentities\Exception\DayoffDuplicateDate(); | |
| 77 | } | |
| 78 | $seen[$day] = true; | |
| 79 | } | |
| 80 | return true; | |
| 81 | } | |
| 82 | ||
| 83 | public function withNew(DayoffList $dayoffList): self | |
| 84 | { | |
| 85 | $list = new self(); | |
| 86 | foreach ($dayoffList as $entity) { | |
| 87 | if (! $this->hasEntityByDate($entity->getDateTime())) { | |
| 88 | $list->addEntity($entity); | |
| 89 | } | |
| 90 | } | |
| 91 | return $list; | |
| 92 | } | |
| 93 | ||
| 94 | /** | |
| 95 | * Check if dayoff is newer than given time | |
| 96 | * | |
| 97 | * @return bool | |
| 98 | */ | |
| 99 | public function isNewerThan(\DateTimeInterface $dateTime, mixed $filterByAvailability = null, mixed $now = null) | |
| 100 | { | |
| 101 | foreach ($this as $dayoff) { | |
| 102 | if ($dayoff->isNewerThan($dateTime, $filterByAvailability, $now)) { | |
| 103 | return true; | |
| 104 | } | |
| 105 | } | |
| 106 | return false; | |
| 107 | } | |
| 108 | } |