Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
DayoffList
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
7 / 7
19
100.00% covered (success)
100.00%
1 / 1
 hasEntityByDate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getByDate
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getEntityByName
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 withTimestampFromDateformat
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 testDatesInYear
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 withNew
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 isNewerThan
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace BO\Zmsentities\Collection;
4
5/**
6 * @extends Base<\BO\Zmsentities\Dayoff>
7 */
8class DayoffList extends Base
9{
10    public const string ENTITY_CLASS = '\BO\Zmsentities\Dayoff';
11
12    public function hasEntityByDate($date): bool
13    {
14        return $this->getByDate($date) ? true : false;
15    }
16
17    public function getByDate($date): \BO\Zmsentities\Dayoff|false
18    {
19        $date = (new \BO\Zmsentities\Helper\DateTime($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($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($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($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    public function withNew(DayoffList $dayoffList): self
67    {
68        $list = new self();
69        foreach ($dayoffList as $entity) {
70            if (! $this->hasEntityByDate($entity->getDateTime())) {
71                $list->addEntity($entity);
72            }
73        }
74        return $list;
75    }
76
77    /**
78     * Check if dayoff is newer than given time
79     *
80     * @return bool
81     */
82    public function isNewerThan(\DateTimeInterface $dateTime, $filterByAvailability = null, $now = null)
83    {
84        foreach ($this as $dayoff) {
85            if ($dayoff->isNewerThan($dateTime, $filterByAvailability, $now)) {
86                return true;
87            }
88        }
89        return false;
90    }
91}