|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arp\DateTime; |
|
6
|
|
|
|
|
7
|
|
|
use Arp\DateTime\Exception\DateFactoryException; |
|
8
|
|
|
use Arp\DateTime\Exception\DateIntervalFactoryException; |
|
9
|
|
|
use Arp\DateTime\Exception\DateTimeFactoryException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Alex Patterson <[email protected]> |
|
13
|
|
|
* @package Arp\DateTime |
|
14
|
|
|
*/ |
|
15
|
|
|
final class DateFactory implements DateFactoryInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var DateTimeFactoryInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private DateTimeFactoryInterface $dateTimeFactory; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var DateIntervalFactoryInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private DateIntervalFactoryInterface $dateIntervalFactory; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param DateTimeFactoryInterface $dateTimeFactory |
|
29
|
|
|
* @param DateIntervalFactoryInterface|null $dateIntervalFactory |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct( |
|
32
|
|
|
DateTimeFactoryInterface $dateTimeFactory, |
|
33
|
|
|
DateIntervalFactoryInterface $dateIntervalFactory = null |
|
34
|
|
|
) { |
|
35
|
|
|
$this->dateTimeFactory = $dateTimeFactory ?? new DateTimeFactory(); |
|
36
|
|
|
$this->dateIntervalFactory = $dateIntervalFactory ?? new DateIntervalFactory(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string|null $spec |
|
41
|
|
|
* @param null $timeZone |
|
|
|
|
|
|
42
|
|
|
* |
|
43
|
|
|
* @return \DateTimeInterface |
|
44
|
|
|
* |
|
45
|
|
|
* @throws DateFactoryException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function createDateTime(string $spec = null, $timeZone = null): \DateTimeInterface |
|
48
|
|
|
{ |
|
49
|
|
|
try { |
|
50
|
|
|
return $this->dateTimeFactory->createDateTime($spec, $timeZone); |
|
51
|
|
|
} catch (DateTimeFactoryException $e) { |
|
52
|
|
|
throw new DateFactoryException($e->getMessage(), $e->getCode(), $e); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $spec |
|
58
|
|
|
* @param string $format |
|
59
|
|
|
* @param null $timeZone |
|
|
|
|
|
|
60
|
|
|
* |
|
61
|
|
|
* @return \DateTimeInterface |
|
62
|
|
|
* |
|
63
|
|
|
* @throws DateFactoryException |
|
64
|
|
|
*/ |
|
65
|
|
|
public function createFromFormat(string $spec, string $format, $timeZone = null): \DateTimeInterface |
|
66
|
|
|
{ |
|
67
|
|
|
try { |
|
68
|
|
|
return $this->dateTimeFactory->createFromFormat($spec, $format, $timeZone); |
|
69
|
|
|
} catch (DateTimeFactoryException $e) { |
|
70
|
|
|
throw new DateFactoryException($e->getMessage(), $e->getCode(), $e); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $spec |
|
76
|
|
|
* |
|
77
|
|
|
* @return \DateTimeZone |
|
78
|
|
|
* |
|
79
|
|
|
* @throws DateFactoryException |
|
80
|
|
|
*/ |
|
81
|
|
|
public function createDateTimeZone(string $spec): \DateTimeZone |
|
82
|
|
|
{ |
|
83
|
|
|
try { |
|
84
|
|
|
return $this->dateTimeFactory->createDateTimeZone($spec); |
|
85
|
|
|
} catch (DateTimeFactoryException $e) { |
|
86
|
|
|
throw new DateFactoryException($e->getMessage(), $e->getCode(), $e); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $spec |
|
92
|
|
|
* |
|
93
|
|
|
* @return \DateInterval |
|
94
|
|
|
* |
|
95
|
|
|
* @throws DateFactoryException |
|
96
|
|
|
*/ |
|
97
|
|
|
public function createDateInterval(string $spec): \DateInterval |
|
98
|
|
|
{ |
|
99
|
|
|
try { |
|
100
|
|
|
return $this->dateIntervalFactory->createDateInterval($spec); |
|
101
|
|
|
} catch (DateIntervalFactoryException $e) { |
|
102
|
|
|
throw new DateFactoryException($e->getMessage(), $e->getCode(), $e); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Perform a diff of two dates and return the \DateInterval |
|
108
|
|
|
* |
|
109
|
|
|
* @param \DateTimeInterface $origin The origin date |
|
110
|
|
|
* @param \DateTimeInterface $target The date to compare to |
|
111
|
|
|
* @param bool $absolute If the interval is negative, should it be forced to be a positive value? |
|
112
|
|
|
* |
|
113
|
|
|
* @return \DateInterval |
|
114
|
|
|
* |
|
115
|
|
|
* @throws DateFactoryException If the date diff cannot be performed |
|
116
|
|
|
*/ |
|
117
|
|
|
public function diff(\DateTimeInterface $origin, \DateTimeInterface $target, bool $absolute = false): \DateInterval |
|
118
|
|
|
{ |
|
119
|
|
|
try { |
|
120
|
|
|
return $this->dateIntervalFactory->diff($origin, $target, $absolute); |
|
121
|
|
|
} catch (DateIntervalFactoryException $e) { |
|
122
|
|
|
throw new DateFactoryException($e->getMessage(), $e->getCode(), $e); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|