|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BestServedCold\PhalueObjects\DateTime; |
|
4
|
|
|
|
|
5
|
|
|
use BestServedCold\PhalueObjects\Mathematical\Operator\ArithmeticTrait; |
|
6
|
|
|
use BestServedCold\PhalueObjects\Mathematical\Operator\ComparisonTrait; |
|
7
|
|
|
use BestServedCold\PhalueObjects\Mathematical\Operator\TypeTrait; |
|
8
|
|
|
use BestServedCold\PhalueObjects\Contract\DateTime as DateTimeInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class DateTimeTrait |
|
12
|
|
|
* |
|
13
|
|
|
* @package BestServedCold\PhalueObjects\DateTime |
|
14
|
|
|
* @author Adam Lewis <[email protected]> |
|
15
|
|
|
* @copyright Copyright (c) 2015 Best Served Cold Media Limited |
|
16
|
|
|
* @license http://http://opensource.org/licenses/GPL-3.0 GPL License |
|
17
|
|
|
* @link http://bestservedcold.com |
|
18
|
|
|
* @since 0.0.1-alpha |
|
19
|
|
|
* @version 0.0.2-alpha |
|
20
|
|
|
*/ |
|
21
|
|
|
trait DateTimeTrait |
|
22
|
|
|
{ |
|
23
|
|
|
use ArithmeticTrait, ComparisonTrait, TypeTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var \DateTime |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $native; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var int |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $timestamp; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param $string |
|
37
|
|
|
* @return mixed |
|
38
|
|
|
*/ |
|
39
|
16 |
|
public static function fromString($string) |
|
40
|
|
|
{ |
|
41
|
16 |
|
return static::fromNative(new \DateTime($string)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param $timestamp |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public static function fromTimestamp($timestamp) |
|
49
|
|
|
{ |
|
50
|
1 |
|
return static::fromNative(static::getNowDateTime()->setTimestamp($timestamp)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $format |
|
55
|
|
|
* @return int |
|
56
|
|
|
*/ |
|
57
|
33 |
|
public static function getNowDateTimeFormat($format) |
|
58
|
|
|
{ |
|
59
|
33 |
|
return (int) static::getNowDateTime()->format($format); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $string |
|
|
|
|
|
|
64
|
|
|
* @return \DateTime |
|
65
|
|
|
*/ |
|
66
|
44 |
|
public static function getDateTime($string = null) |
|
67
|
|
|
{ |
|
68
|
44 |
|
return new \DateTime($string); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return \DateTime |
|
73
|
|
|
*/ |
|
74
|
44 |
|
public static function getNowDateTime() |
|
75
|
2 |
|
{ |
|
76
|
44 |
|
return static::getDateTime('now'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return \DateTime |
|
81
|
|
|
*/ |
|
82
|
8 |
|
public function getNative() |
|
83
|
|
|
{ |
|
84
|
8 |
|
return $this->native; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return int |
|
89
|
|
|
*/ |
|
90
|
11 |
|
public function getTimestamp() |
|
91
|
|
|
{ |
|
92
|
11 |
|
return $this->timestamp; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param DateTimeInterface $object |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function isBefore(DateTimeInterface $object) |
|
100
|
|
|
{ |
|
101
|
1 |
|
return $this->isLessThan($object); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param DateTimeInterface $object |
|
106
|
|
|
* @return bool |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function isAfter(DateTimeInterface $object) |
|
109
|
|
|
{ |
|
110
|
1 |
|
return $this->isGreaterThan($object); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param DateTimeInterface $object |
|
115
|
|
|
* @return bool |
|
116
|
|
|
*/ |
|
117
|
1 |
|
public function isAfterOrIs(DateTimeInterface $object) |
|
118
|
|
|
{ |
|
119
|
1 |
|
return $this->isLessThanOrEqualTo($object); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param DateTimeInterface $object |
|
124
|
|
|
* @return bool |
|
125
|
|
|
*/ |
|
126
|
1 |
|
public function isBeforeOrIs(DateTimeInterface $object) |
|
127
|
|
|
{ |
|
128
|
1 |
|
return $this->isGreaterThanOrEqualTo($object); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.