94% of 461OPs |
94% of 64Lines |
75% of 52Branches |
65% of 23Paths |
# | |
---|---|
1 |
<?php |
2 |
|
3 |
namespace mageekguy\atoum\asserters; |
4 |
|
5 |
use |
6 |
mageekguy\atoum\asserters, |
7 |
mageekguy\atoum\exceptions |
8 |
; |
9 |
|
10 |
class dateInterval extends asserters\object |
11 |
{ |
12 |
public function __toString()100% |
13 |
{ |
14 |
return (static::isDateInterval($this->value) === false ? parent::__toString() : $this->format($this->value)); |
15 |
} |
16 |
|
17 |
public function __get($asserter)50% |
18 |
{ |
19 |
switch (strtolower($asserter)) |
20 |
{ |
21 |
case 'iszero': |
22 |
return $this->{$asserter}(); |
23 |
|
24 |
default: |
25 |
return parent::__get($asserter); |
26 |
} |
27 |
} |
28 |
|
29 |
public function setWith($value, $checkType = true)100% |
30 |
{ |
31 |
parent::setWith($value, false); |
32 |
|
33 |
if ($checkType === true) |
34 |
{ |
35 |
if (self::isDateInterval($this->value) === true) |
36 |
{ |
37 |
$this->pass(); |
38 |
} |
39 |
else |
40 |
{ |
41 |
$this->fail($this->_('%s is not an instance of \\dateInterval', $this)); |
42 |
} |
43 |
} |
44 |
|
45 |
return $this; |
46 |
} |
47 |
|
48 |
public function isGreaterThan(\dateInterval $interval, $failMessage = null)100% |
49 |
{ |
50 |
list($date1, $date2) = $this->getDates($interval); |
51 |
|
52 |
if ($date1 > $date2) |
53 |
{ |
54 |
$this->pass(); |
55 |
} |
56 |
else |
57 |
{ |
58 |
$this->fail($failMessage ?: $this->_('Interval %s is not greater than %s', $this, $this->format($interval))); |
59 |
} |
60 |
|
61 |
return $this; |
62 |
} |
63 |
|
64 |
public function isGreaterThanOrEqualTo(\dateInterval $interval, $failMessage = null)100% |
65 |
{ |
66 |
list($date1, $date2) = $this->getDates($interval); |
67 |
|
68 |
if ($date1 >= $date2) |
69 |
{ |
70 |
$this->pass(); |
71 |
} |
72 |
else |
73 |
{ |
74 |
$this->fail($failMessage ?: $this->_('Interval %s is not greater than or equal to %s', $this, $this->format($interval))); |
75 |
} |
76 |
|
77 |
return $this; |
78 |
} |
79 |
|
80 |
public function isLessThan(\dateInterval $interval, $failMessage = null)100% |
81 |
{ |
82 |
list($date1, $date2) = $this->getDates($interval); |
83 |
|
84 |
if ($date1 < $date2) |
85 |
{ |
86 |
$this->pass(); |
87 |
} |
88 |
else |
89 |
{ |
90 |
$this->fail($failMessage ?: $this->_('Interval %s is not less than %s', $this, $this->format($interval))); |
91 |
} |
92 |
|
93 |
return $this; |
94 |
} |
95 |
|
96 |
public function isLessThanOrEqualTo(\dateInterval $interval, $failMessage = null)100% |
97 |
{ |
98 |
list($date1, $date2) = $this->getDates($interval); |
99 |
|
100 |
if ($date1 <= $date2) |
101 |
{ |
102 |
$this->pass(); |
103 |
} |
104 |
else |
105 |
{ |
106 |
$this->fail($failMessage ?: $this->_('Interval %s is not less than or equal to %s', $this, $this->format($interval))); |
107 |
} |
108 |
|
109 |
return $this; |
110 |
} |
111 |
|
112 |
public function isEqualTo($interval, $failMessage = null)100% |
113 |
{ |
114 |
list($date1, $date2) = $this->getDates($interval); |
115 |
|
116 |
if ($date1 == $date2) |
117 |
{ |
118 |
$this->pass(); |
119 |
} |
120 |
else |
121 |
{ |
122 |
$this->fail($failMessage ?: $this->_('Interval %s is not equal to %s', $this, $this->format($interval))); |
123 |
} |
124 |
|
125 |
return $this; |
126 |
} |
127 |
|
128 |
public function isZero($failMessage = null)100% |
129 |
{ |
130 |
return $this->isEqualTo(new \dateInterval('P0D'), $failMessage ?: $this->_('Interval %s is not equal to zero', $this)); |
131 |
} |
132 |
|
133 |
protected function valueIsSet($message = 'Interval is undefined')75% |
134 |
{ |
135 |
if (self::isDateInterval(parent::valueIsSet($message)->value) === false) |
136 |
{ |
137 |
throw new exceptions\logic($message); |
138 |
} |
139 |
|
140 |
return $this; |
141 |
} |
142 |
|
143 |
protected function getDates(\dateInterval $interval)100% |
144 |
{ |
145 |
$this->valueIsSet(); |
146 |
|
147 |
$date1 = new \dateTime(); |
148 |
$date2 = clone $date1; |
149 |
|
150 |
return array($date1->add($this->value), $date2->add($interval)); |
151 |
} |
152 |
|
153 |
protected static function isDateInterval($value)100% |
154 |
{ |
155 |
return ($value instanceof \dateInterval); |
156 |
} |
157 |
|
158 |
protected function format(\dateInterval $interval)100% |
159 |
{ |
160 |
return $interval->format($this->_('%Y/%M/%D %H:%I:%S')); |
161 |
} |
162 |
} |