|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BestServedCold\PhalueObjects\DateTime; |
|
4
|
|
|
|
|
5
|
|
|
use BestServedCold\PhalueObjects\DateTime\Unit\Day\Month as Day; |
|
6
|
|
|
use BestServedCold\PhalueObjects\Contract\DateTime\Day as DayInterface; |
|
7
|
|
|
use BestServedCold\PhalueObjects\Contract\DateTime as DateTimeInterface; |
|
8
|
|
|
use BestServedCold\PhalueObjects\DateTime\Unit\DayTrait; |
|
9
|
|
|
use BestServedCold\PhalueObjects\DateTime\Unit\Month; |
|
10
|
|
|
use BestServedCold\PhalueObjects\DateTime\Unit\Year; |
|
11
|
|
|
use BestServedCold\PhalueObjects\Variadic; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Date |
|
15
|
|
|
* |
|
16
|
|
|
* @package BestServedCold\PhalueObjects\DateTime |
|
17
|
|
|
* @author Adam Lewis <[email protected]> |
|
18
|
|
|
* @copyright Copyright (c) 2015 Best Served Cold Media Limited |
|
19
|
|
|
* @license http://http://opensource.org/licenses/GPL-3.0 GPL License |
|
20
|
|
|
* @link http://bestservedcold.com |
|
21
|
|
|
* @since 0.0.1-alpha |
|
22
|
|
|
* @version 0.0.2-alpha |
|
23
|
|
|
*/ |
|
24
|
|
|
class Date extends Variadic implements DateTimeInterface, DayInterface |
|
25
|
|
|
{ |
|
26
|
|
|
use DayTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Year |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $year; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var Month |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $month; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Day |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $day; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param Year $year |
|
45
|
|
|
* @param Month $month |
|
46
|
|
|
* @param Day $day |
|
47
|
|
|
*/ |
|
48
|
24 |
|
public function __construct(Year $year, Month $month, Day $day) |
|
49
|
|
|
{ |
|
50
|
24 |
|
$this->year = $year; |
|
51
|
24 |
|
$this->month = $month; |
|
52
|
24 |
|
$this->day = $day; |
|
53
|
24 |
|
$this->native = new \DateTime($year.'-'.$month.'-'.$day); |
|
54
|
24 |
|
$this->timestamp = $this->native->getTimestamp(); |
|
55
|
24 |
|
parent::__construct($year, $month, $day); |
|
56
|
24 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return static |
|
60
|
|
|
*/ |
|
61
|
10 |
|
public static function now() |
|
62
|
|
|
{ |
|
63
|
10 |
|
return new static(Year::now(), Month::now(), Day::now()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param \DateTime $dateTime |
|
68
|
|
|
* @return static |
|
69
|
|
|
*/ |
|
70
|
13 |
|
public static function fromNative(\DateTime $dateTime) |
|
71
|
|
|
{ |
|
72
|
13 |
|
return new static( |
|
73
|
13 |
|
new Year((int) $dateTime->format('Y')), |
|
74
|
13 |
|
new Month((int) $dateTime->format('n')), |
|
75
|
13 |
|
new Day((int) $dateTime->format('j')) |
|
76
|
13 |
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
11 |
|
public function __toString() |
|
83
|
|
|
{ |
|
84
|
11 |
|
return $this->year.'-'.$this->month.'-'.$this->day; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return int |
|
89
|
|
|
*/ |
|
90
|
4 |
|
public function getValue() |
|
91
|
|
|
{ |
|
92
|
4 |
|
return (int) $this->timestamp; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return Year |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function getYear() |
|
99
|
|
|
{ |
|
100
|
1 |
|
return $this->year; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return Month |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function getMonth() |
|
107
|
|
|
{ |
|
108
|
1 |
|
return $this->month; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return Day |
|
113
|
|
|
*/ |
|
114
|
1 |
|
public function getDay() |
|
115
|
|
|
{ |
|
116
|
1 |
|
return $this->day; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return bool |
|
121
|
|
|
*/ |
|
122
|
2 |
|
public function isWeekend() |
|
123
|
|
|
{ |
|
124
|
2 |
|
return in_array($this->native->format('w'), [ 0, 6 ]); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return bool |
|
129
|
|
|
*/ |
|
130
|
1 |
|
public function isWeekDay() |
|
131
|
|
|
{ |
|
132
|
1 |
|
return !$this->isWeekend(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return bool |
|
137
|
|
|
*/ |
|
138
|
1 |
|
public function isLeap() |
|
139
|
|
|
{ |
|
140
|
1 |
|
return $this->year->isLeap(); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|