Completed
Push — master ( 9d882f...d62d8f )
by Danail
03:52
created

Holidays::getDates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace CL\DateUtils;
4
5
use DateTime;
6
use Countable;
7
8
/**
9
 * @author    Ivan Kerin <[email protected]>
10
 * @copyright 2015, Clippings Ltd.
11
 * @license   http://spdx.org/licenses/BSD-3-Clause
12
 */
13
class Holidays implements Countable
14
{
15
    /**
16
     * @var DateTime[]
17
     */
18
    private $dates = [];
19
20 1
    public function __construct(array $dates = array())
21
    {
22 1
        foreach ($dates as $date) {
23 1
            $this->add($date);
24 1
        }
25 1
    }
26
27
    /**
28
     * @return boolean
29
     */
30 1
    public function isEmpty()
31
    {
32 1
        return empty($this->dates);
33
    }
34
35
    /**
36
     * Implement countable
37
     *
38
     * @return integer
39
     */
40 1
    public function count()
41
    {
42 1
        return count($this->dates);
43
    }
44
45
    /**
46
     * @return DateTime[]
47
     */
48 1
    public function getDates()
49
    {
50 1
        return $this->dates;
51
    }
52
53
    /**
54
     * @param DateTime $current
0 ignored issues
show
Bug introduced by
There is no parameter named $current. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
55
     * @param DateTime $to
0 ignored issues
show
Bug introduced by
There is no parameter named $to. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
56
     */
57 1
    public function addDateTimeSpan(DateTimeSpan $span)
58
    {
59 1
        $current = clone $span->getFrom();
60
61 1
        while ($current <= $span->getTo()) {
62 1
            $this->add($current);
63
64 1
            $current = clone $current;
65 1
            $current->modify('+1 day');
66 1
        }
67
68 1
        return $this;
69
    }
70
71
    /**
72
     * @param DateTime $day
73
     */
74 1
    public function add(DateTime $day)
75
    {
76 1
        $this->dates []= $day;
77
78 1
        sort($this->dates);
79
80 1
        return $this;
81
    }
82
83
    /**
84
     * Check if a date is within any holiday
85
     *
86
     * @param  DateTime $date
87
     * @return boolean
88
     */
89 1
    public function isActive(DateTime $date = null)
90
    {
91 1
        $date = $date ?: new DateTime();
92
93 1
        foreach ($this->dates as $holiday) {
94 1
            if ($date->format('Y-m-d') == $holiday->format('Y-m-d')) {
95 1
                return true;
96
            }
97 1
        }
98
99 1
        return false;
100
    }
101
102
    /**
103
     * @param  DateTimeSpan $span
104
     * @param  DateTime $start_date
105
     * @return DateTimeSpan
106
     */
107 5
    public function extendDateTimeSpan(DateTimeSpan $span, DateTime $start_date = null)
108
    {
109 5
        $from = clone $span->getFrom();
110 5
        $to = clone $span->getTo();
111
112 5
        foreach ($this->dates as $holiday) {
113 5
            if ($start_date and $holiday < $start_date) {
114 1
                continue;
115
            }
116
117 5
            if ($holiday <= $from) {
118 3
                $from->modify('+1 day');
119 3
            }
120
121 5
            if ($holiday <= $to) {
122 5
                $to->modify('+1 day');
123 5
            }
124 5
        }
125
126 5
        return new DateTimeSpan($from, $to);
127
    }
128
129
    /**
130
     * @param  DateTimeSpan $span
131
     * @param  DateTime $start_date
132
     * @return DateTimeSpan
133
     */
134 5
    public function extendBusinessDateTimeSpan(DateTimeSpan $span, DateTime $start_date = null)
135
    {
136 5
        $from = clone $span->getFrom();
137 5
        $to = clone $span->getTo();
138
139 5
        foreach ($this->dates as $holiday) {
140 5
            if (($start_date and $holiday < $start_date) or 5 < $holiday->format('N')) {
141 5
                continue;
142
            }
143
144 5
            if ($holiday <= $from) {
145 3
                $from->modify('+1 weekday');
146 3
            }
147
148 5
            if ($holiday <= $to) {
149 5
                $to->modify('+1 weekday');
150 5
            }
151 5
        }
152
153 5
        return new DateTimeSpan($from, $to);
154
    }
155
}
156