Completed
Pull Request — master (#10)
by Yılmaz
02:30
created

DateRange::isEmpty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * Daterange value object to represent date periods.
4
 * 
5
 * @author  M. Yilmaz SUSLU <[email protected]>
6
 * @license MIT
7
 *
8
 * @since   Sep 2016
9
 */
10
namespace DDD\Embeddable;
11
12
use Doctrine\ORM\Mapping as ORM;
13
use JsonSerializable;
14
15
/**
16
 * @ORM\Embeddable
17
 */
18
class DateRange implements JsonSerializable
19
{
20
    /**
21
     * start date
22
     * 
23
     * @ORM\Column(type="datetime", nullable=true)
24
     * 
25
     * @var \DateTime
26
     */
27
    private $dateFrom;
28
29
    /**
30
     * end date
31
     *
32
     * @ORM\Column(type="datetime", nullable=true)
33
     *
34
     * @var \DateTime
35
     */
36
    private $dateTo;
37
38
    /**
39
     * Constructor
40
     * 
41
     * @param \DateTime $start
42
     * @param \DateTime $end
43
     */
44 5
    public function __construct(\DateTime $start = null, \DateTime $end = null)
45
    {
46 5
        if ($start === null || $end === null) {
47 1
            return;
48 4
        } elseif ($start >= $end) {
49 2
            throw new \InvalidArgumentException('Start date is greater or equal to end date');
50
        }
51
        
52 2
        $this->dateFrom = $start;
53 2
        $this->dateTo   = $end;
54 2
    }
55
56
    /**
57
     * Gets the start date.
58
     *
59
     * @return \DateTime
60
     */
61 1
    public function getDateFrom()
62
    {
63 1
        return $this->dateFrom;
64
    }
65
66
    /**
67
     * Gets the end date.
68
     *
69
     * @return \DateTime
70
     */
71 1
    public function getDateTo()
72
    {
73 1
        return $this->dateTo;
74
    }
75
76
    /**
77
     * Formats date range to string using given $format
78
     * 
79
     * @param string $f Any format accepted by php date()
80
     * 
81
     * @return string
82
     */
83 2
    public function format($f = 'c')
84
    {
85 2
        if ($this->isEmpty()) {
86 1
            return '';
87
        }
88
89 1
        return $this->getDateFrom()->format($f).' - '.$this->getDateTo()->format($f);
90
    }
91
92
    /**
93
     * String representation of date range.
94
     * 
95
     * @return string
96
     */
97 2
    public function __toString()
98
    {
99 2
        return $this->format('c');
100
    }
101
102
    /**
103
     * Returns duration of the date range in seconds.
104
     * 
105
     * @return int
106
     */
107 3
    public function getDurationInSeconds()
108
    {
109 3
        if (!$this->dateFrom) {
110 1
            return 0;
111
        }
112
113 2
        $interval = $this->dateFrom->diff($this->dateTo);
114
115 2
        return ($interval->y * 365 * 24 * 60 * 60) +
116 2
               ($interval->m * 30 * 24 * 60 * 60) +
117 2
               ($interval->d * 24 * 60 * 60) +
118 2
               ($interval->h * 60 * 60) +
119 2
               ($interval->i * 60) +
120 2
               $interval->s;
121
    }
122
123
    /**
124
     * Array representation of the range
125
     * 
126
     * @return array
127
     */
128 2
    public function toArray($format = 'c')
129
    {
130 2
        if ($this->isEmpty()) {
131 1
            return [];
132
        }
133
134
        return [
135 1
            'start' => $this->getDateFrom()->format($format),
136 1
            'end'   => $this->getDateTo()->format($format),
137 1
        ];
138
    }
139
140
    /**
141
     * Implement json serializable interface.
142
     * 
143
     * @return array
144
     */
145 1
    public function jsonSerialize()
146
    {
147 1
        return $this->toArray();
148
    }
149
150
    /**
151
     * Returns a boolean TRUE if the range instance is
152
     * literally empty, FALSE otherwise.
153
     * 
154
     * @return boolean
155
     */
156 2
    public function isEmpty()
157
    {
158 2
        return !$this->dateFrom || !$this->dateTo;
159
    }
160
}
161