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

DateTimeSpan   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 99
ccs 32
cts 36
cp 0.8889
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getFrom() 0 4 1
A getTo() 0 4 1
A contains() 0 4 2
A humanize() 0 19 4
A getBusinessDaysInPeriodFrom() 0 8 2
A getBusinessDaysInPeriodTo() 0 8 2
A calculateBusinessDaysInPeriod() 0 13 3
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 6
    public function getBusinessDaysInPeriodFrom(DateTime $startDate = null): int
82
    {
83 6
        if ($startDate === null) {
84 1
            $startDate = new DateTime('today');
85
        }
86
87 6
        return $this->calculateBusinessDaysInPeriod($startDate, $this->from);
88
    }
89
90
    public function getBusinessDaysInPeriodTo(DateTime $startDate = null): int
91
    {
92
        if ($startDate === null) {
93
            $startDate = new DateTime('today');
94
        }
95
96
        return $this->calculateBusinessDaysInPeriod($startDate, $this->to);
97
    }
98
99 12
    private function calculateBusinessDaysInPeriod(DateTime $startDate, DateTime $endDate): int
100
    {
101 12
        $interval = new DateInterval('P1D'); // 1 Day
102 12
        $dateRange = new DatePeriod($startDate, $interval, $endDate);
103 12
        $workingDays = 0;
104 12
        foreach ($dateRange as $date) {
105 6
            if ($date->format('N') <= 5) {
106 6
                ++$workingDays;
107
            }
108
        }
109
110 12
        return $workingDays;
111
    }
112
}
113