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 ENTITY_CLASS = '\BO\Zmsentities\Dayoff';
11
12    public function hasEntityByDate($date)
13    {
14        return $this->getByDate($date) ? true : false;
15    }
16
17    public function getByDate($date)
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)
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')
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    public function testDatesInYear($year)
52    {
53        foreach ($this as $data) {
54            $entity = new \BO\Zmsentities\Dayoff($data); // if source is an array
55            $entityYear = (new \DateTimeImmutable())->setTimestamp($entity->date)->format('Y');
56            if ($entityYear != $year) {
57                throw new \BO\Zmsentities\Exception\DayoffWrongYear();
58            }
59        }
60        return true;
61    }
62
63    public function withNew(DayoffList $dayoffList)
64    {
65        $list = new self();
66        foreach ($dayoffList as $entity) {
67            if (! $this->hasEntityByDate($entity->getDateTime())) {
68                $list->addEntity($entity);
69            }
70        }
71        return $list;
72    }
73
74    /**
75     * Check if dayoff is newer than given time
76     *
77     * @return bool
78     */
79    public function isNewerThan(\DateTimeInterface $dateTime, $filterByAvailability = null, $now = null)
80    {
81        foreach ($this as $dayoff) {
82            if ($dayoff->isNewerThan($dateTime, $filterByAvailability, $now)) {
83                return true;
84            }
85        }
86        return false;
87    }
88}