1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DrLenux\DataHelper; |
6
|
|
|
|
7
|
|
|
use DateInterval; |
8
|
|
|
use DateTimeZone; |
9
|
|
|
use Exception; |
10
|
|
|
use DateTime; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class DateChange |
14
|
|
|
* |
15
|
|
|
* @method $this addDay(int $count = 1) |
16
|
|
|
* @method $this addMonth(int $count = 1) |
17
|
|
|
* @method $this addYear(int $count = 1) |
18
|
|
|
* @method $this addHour(int $count = 1) |
19
|
|
|
* @method $this addMinute(int $count = 1) |
20
|
|
|
* @method $this addSeconds(int $count = 1) |
21
|
|
|
* |
22
|
|
|
* @method $this subDay(int $count = 1) |
23
|
|
|
* @method $this subMonth(int $count = 1) |
24
|
|
|
* @method $this subYear(int $count = 1) |
25
|
|
|
* @method $this subHour(int $count = 1) |
26
|
|
|
* @method $this subMinute(int $count = 1) |
27
|
|
|
* @method $this subSeconds(int $count = 1) |
28
|
|
|
*/ |
29
|
|
|
class DateChange |
30
|
|
|
{ |
31
|
|
|
const SECONDS = 'PT{count}S'; |
32
|
|
|
const MINUTE = 'PT{count}M'; |
33
|
|
|
const HOUR = 'PT{count}H'; |
34
|
|
|
const DAY = 'P{count}D'; |
35
|
|
|
const MONTH = 'P{count}M'; |
36
|
|
|
const YEAR = 'P{count}Y'; |
37
|
|
|
|
38
|
|
|
const TODAY = 'today'; |
39
|
|
|
const DEFAULT_FORMAT = 'Y-m-d H:i:s'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \DateTime |
43
|
|
|
*/ |
44
|
|
|
private $_date; |
45
|
|
|
|
46
|
14 |
|
/** |
47
|
|
|
* DateChange constructor. |
48
|
14 |
|
* @param string $date |
49
|
13 |
|
* @param DateTimeZone|null $timezone |
50
|
|
|
* @throws Exception |
51
|
|
|
*/ |
52
|
|
|
public function __construct(string $date = self::TODAY, DateTimeZone $timezone = null) |
53
|
|
|
{ |
54
|
|
|
$this->_date = new \DateTime($date, $timezone); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function __toString(): string |
61
|
|
|
{ |
62
|
|
|
return $this->getDate(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return DateTime |
67
|
|
|
*/ |
68
|
|
|
public function getDateTime(): DateTime |
69
|
|
|
{ |
70
|
|
|
return $this->_date; |
71
|
13 |
|
} |
72
|
|
|
|
73
|
13 |
|
/** |
74
|
|
|
* @param string $format |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getDate(string $format = self::DEFAULT_FORMAT): string |
78
|
|
|
{ |
79
|
|
|
return $this->_date->format($format); |
80
|
|
|
} |
81
|
|
|
|
82
|
13 |
|
/** |
83
|
|
|
* @param string $name |
84
|
13 |
|
* @param int[] $arguments |
85
|
|
|
* @return $this |
86
|
13 |
|
* @throws \Exception |
87
|
13 |
|
*/ |
88
|
13 |
|
public function __call($name, $arguments) |
89
|
|
|
{ |
90
|
13 |
|
$params = preg_split('/(?=[A-Z])/', $name); |
91
|
1 |
|
if ( |
92
|
|
|
count($params) === 2 && |
|
|
|
|
93
|
13 |
|
in_array($params[0], ['sub', 'add']) && |
94
|
13 |
|
defined('static::' . mb_strtoupper($params[1])) |
95
|
13 |
|
) { |
96
|
13 |
|
if (!count($arguments)) { |
97
|
|
|
$arguments[] = 1; //default |
98
|
|
|
} |
99
|
|
|
if (count($arguments) === 1 && is_int($arguments[0])) { |
100
|
|
|
$type = constant('static::' . mb_strtoupper($params[1])); |
101
|
|
|
$this->{$params[0]}($arguments[0], $type); |
102
|
|
|
return $this; |
103
|
|
|
} else { |
104
|
|
|
throw new Exception('Expected only one integer argument' . $name); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
throw new Exception('Don\'t call method ' . $name); |
108
|
|
|
} |
109
|
7 |
|
|
110
|
|
|
/** |
111
|
7 |
|
* @param int $count |
112
|
7 |
|
* @param string $type |
113
|
|
|
* @throws \Exception |
114
|
|
|
*/ |
115
|
|
|
protected function sub(int $count, string $type) |
116
|
|
|
{ |
117
|
|
|
$this->_date->sub(new DateInterval(str_replace('{count}', $count, $type))); |
118
|
|
|
} |
119
|
7 |
|
|
120
|
|
|
/** |
121
|
7 |
|
* @param int $count |
122
|
7 |
|
* @param string $type |
123
|
|
|
* @throws \Exception |
124
|
|
|
*/ |
125
|
|
|
protected function add(int $count, string $type) |
126
|
|
|
{ |
127
|
|
|
$this->_date->add(new DateInterval(str_replace('{count}', $count, $type))); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param DateChange $date |
132
|
|
|
* @return bool|DateInterval |
133
|
|
|
*/ |
134
|
|
|
public function diff(DateChange $date): DateInterval |
135
|
|
|
{ |
136
|
|
|
return $this->getDateTime()->diff($date->getDateTime()); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
} |