1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace BestServedCold\PhalueObjects;
|
4
|
|
|
|
5
|
|
|
use BestServedCold\PhalueObjects\DateTime\Date;
|
6
|
|
|
use BestServedCold\PhalueObjects\DateTime\Time;
|
7
|
|
|
use BestServedCold\PhalueObjects\Contract\DateTime\Day as DayInterface;
|
8
|
|
|
use BestServedCold\PhalueObjects\DateTime\Unit\DayTrait;
|
9
|
|
|
use BestServedCold\PhalueObjects\DateTime\Unit\Day\Month as Day;
|
10
|
|
|
use BestServedCold\PhalueObjects\DateTime\Unit\Hour;
|
11
|
|
|
use BestServedCold\PhalueObjects\DateTime\Unit\Minute;
|
12
|
|
|
use BestServedCold\PhalueObjects\DateTime\Unit\Month;
|
13
|
|
|
use BestServedCold\PhalueObjects\DateTime\Unit\Second;
|
14
|
|
|
use BestServedCold\PhalueObjects\DateTime\Unit\Year;
|
15
|
|
|
|
16
|
|
|
/**
|
17
|
|
|
* Class DateTime
|
18
|
|
|
*
|
19
|
|
|
* @package BestServedCold\PhalueObjects
|
20
|
|
|
* @author Adam Lewis <[email protected]>
|
21
|
|
|
* @copyright Copyright (c) 2015 Best Served Cold Media Limited
|
22
|
|
|
* @license http://http://opensource.org/licenses/GPL-3.0 GPL License
|
23
|
|
|
* @link http://bestservedcold.com
|
24
|
|
|
* @since 0.0.1-alpha
|
25
|
|
|
* @version 0.0.2-alpha
|
26
|
|
|
*/
|
27
|
|
|
final class DateTime extends Variadic implements DayInterface
|
28
|
|
|
{
|
29
|
|
|
use DayTrait;
|
30
|
|
|
|
31
|
|
|
protected $date;
|
32
|
|
|
protected $time;
|
33
|
|
|
|
34
|
|
|
/**
|
35
|
|
|
* @param Date $date
|
36
|
|
|
* @param Time $time
|
37
|
|
|
*/
|
38
|
10 |
|
public function __construct(Date $date, Time $time)
|
39
|
|
|
{
|
40
|
10 |
|
$this->date = $date;
|
41
|
10 |
|
$this->time = $time;
|
42
|
10 |
|
$this->timestamp = (int) $date->getTimestamp() + (int) $time->getTimestamp();
|
43
|
10 |
|
$this->native = self::getDateTime($date.' '.$time);
|
44
|
10 |
|
parent::__construct($date, $time);
|
45
|
10 |
|
}
|
46
|
|
|
|
47
|
|
|
/**
|
48
|
|
|
* @return DateTime
|
49
|
|
|
*/
|
50
|
9 |
|
public static function now()
|
51
|
9 |
|
{
|
52
|
4 |
|
return new self(Date::now(), Time::now());
|
53
|
9 |
|
}
|
54
|
|
|
|
55
|
|
|
/**
|
56
|
|
|
* @param \DateTime $dateTime
|
57
|
|
|
* @return static
|
58
|
|
|
*/
|
59
|
9 |
|
public static function fromNative(\DateTime $dateTime)
|
60
|
|
|
{
|
61
|
9 |
|
return new static(
|
62
|
9 |
|
new Date(
|
63
|
9 |
|
new Year((int) $dateTime->format('Y')),
|
64
|
9 |
|
new Month((int) $dateTime->format('n')),
|
65
|
9 |
|
new Day((int) $dateTime->format('j'))
|
66
|
9 |
|
),
|
67
|
9 |
|
new Time(
|
68
|
9 |
|
new Hour((int) $dateTime->format('G')),
|
69
|
9 |
|
new Minute((int) $dateTime->format('i')),
|
70
|
9 |
|
new Second((int) $dateTime->format('s'))
|
71
|
9 |
|
)
|
72
|
9 |
|
);
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
/**
|
76
|
|
|
* @return int
|
77
|
|
|
*/
|
78
|
1 |
|
public function getValue()
|
79
|
|
|
{
|
80
|
1 |
|
return $this->timestamp;
|
81
|
|
|
}
|
82
|
|
|
|
83
|
|
|
/**
|
84
|
|
|
* @param $timestamp
|
85
|
|
|
* @return DateTime
|
86
|
|
|
*/
|
87
|
1 |
|
public static function fromTimestamp($timestamp)
|
88
|
|
|
{
|
89
|
1 |
|
return self::fromNative((new \DateTime())->setTimestamp($timestamp));
|
90
|
|
|
}
|
91
|
|
|
|
92
|
|
|
/**
|
93
|
|
|
* @return Date
|
94
|
|
|
*/
|
95
|
1 |
|
public function getDate()
|
96
|
|
|
{
|
97
|
1 |
|
return $this->date;
|
98
|
|
|
}
|
99
|
|
|
|
100
|
|
|
/**
|
101
|
|
|
* @return Time
|
102
|
|
|
*/
|
103
|
1 |
|
public function getTime()
|
104
|
|
|
{
|
105
|
1 |
|
return $this->time;
|
106
|
|
|
}
|
107
|
|
|
|
108
|
|
|
/**
|
109
|
|
|
* @return string
|
110
|
|
|
*/
|
111
|
2 |
|
public function __toString()
|
112
|
|
|
{
|
113
|
2 |
|
return $this->date.' '.$this->time;
|
114
|
|
|
}
|
115
|
|
|
}
|
116
|
|
|
|