Completed
Pull Request — master (#7)
by Evstati
01:13
created

DateTimeSpan::getBusinessDaysInPeriodTo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
namespace CL\DateUtils;
4
5
use DateInterval;
6
use DatePeriod;
7
use DateTime;
8
9
/**
10
 * @author    Ivan Kerin <[email protected]>
11
 * @copyright 2015, Clippings Ltd.
12
 * @license   http://spdx.org/licenses/BSD-3-Clause
13
 */
14
class DateTimeSpan
15
{
16
    /**
17
     * @var DateTime
18
     */
19
    private $from;
20
21
    /**
22
     * @var DateTime
23
     */
24
    private $to;
25
26 1
    public function __construct(DateTime $from, DateTime $to)
27
    {
28 1
        $this->from = $from;
29 1
        $this->to = $to;
30 1
    }
31
32
    /**
33
     * @return DateTime
34
     */
35 1
    public function getFrom()
36
    {
37 1
        return $this->from;
38
    }
39
40
    /**
41
     * @return DateTime
42
     */
43 1
    public function getTo()
44
    {
45 1
        return $this->to;
46
    }
47
48
    /**
49
     * @param DateTime $datetime
50
     *
51
     * @return bool
52
     */
53 3
    public function contains(DateTime $datetime)
54
    {
55 3
        return $this->from < $datetime and $this->to > $datetime;
56
    }
57
58
    /**
59
     * @return string
60
     */
61 4
    public function humanize()
62
    {
63 4
        $from = $this->getFrom();
64 4
        $to = $this->getTo();
65
66 4
        if ($from->format('Y') == $to->format('Y')) {
67 3
            if ($from->format('m') == $to->format('m')) {
68 2
                if ($from->format('d') == $to->format('d')) {
69 1
                    return $from->format('j M Y');
70
                } else {
71 1
                    return $from->format('j').' - '.$to->format('j').' '.$from->format('M Y');
72
                }
73
            } else {
74 1
                return $from->format('j M').' - '.$to->format('j M').' '.$from->format('Y');
75
            }
76
        }
77
78 1
        return $from->format('j M Y').' - '.$to->format('j M Y');
79
    }
80
81
82
83 6
    public function getBusinessDaysInPeriodFrom(DateTime $startDate = null): int
84
    {
85 6
        if ($startDate === null) {
86 1
            $startDate = new DateTime('today');
87
        }
88
89 6
        return $this->calculateBusinessDaysInPeriod($startDate, $this->from);
90
    }
91
92
93
    public function getBusinessDaysInPeriodTo(DateTime $startDate = null): int
94
    {
95
        if ($startDate === null) {
96
            $startDate = new DateTime('today');
97
        }
98
99
        return $this->calculateBusinessDaysInPeriod($startDate, $this->to);
100
    }
101
102 12
    private function calculateBusinessDaysInPeriod(DateTime $startDate, DateTime $endDate): int
103
    {
104 12
        $interval = new DateInterval('P1D'); // 1 Day
105 12
        $dateRange = new DatePeriod($startDate, $interval, $endDate);
106 12
        $workingDays = 0;
107 12
        foreach ($dateRange as $date) {
108 6
            if ($date->format('N') <= 5) {
109 6
                ++$workingDays;
110
            }
111
        }
112
113 12
        return $workingDays;
114
    }
115
}
116