|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arp\DateTime; |
|
6
|
|
|
|
|
7
|
|
|
use Arp\DateTime\Exception\DateTimeFactoryException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @author Alex Patterson <[email protected]> |
|
11
|
|
|
* @package Arp\DateTime |
|
12
|
|
|
*/ |
|
13
|
|
|
final class DateTimeImmutableFactory implements DateTimeFactoryInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var DateTimeFactory |
|
17
|
|
|
*/ |
|
18
|
|
|
private DateTimeFactory $dateTimeFactory; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param string|null $dateTimeClassName |
|
22
|
|
|
* @param DateTimeZoneFactoryInterface|null $dateTimeZoneFactory |
|
23
|
|
|
* |
|
24
|
|
|
* @throws DateTimeFactoryException |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct( |
|
27
|
|
|
?string $dateTimeClassName = null, |
|
28
|
|
|
?DateTimeZoneFactoryInterface $dateTimeZoneFactory = null |
|
29
|
|
|
) { |
|
30
|
|
|
$dateTimeClassName ??= \DateTimeImmutable::class; |
|
31
|
|
|
if (!is_a($dateTimeClassName, \DateTimeImmutable::class, true)) { |
|
32
|
|
|
throw new DateTimeFactoryException( |
|
33
|
|
|
sprintf( |
|
34
|
|
|
'The \'dateTimeClassName\' parameter must be a class name that implements \'%s\'', |
|
35
|
|
|
\DateTimeImmutable::class |
|
36
|
|
|
) |
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$dateTimeZoneFactory ??= new DateTimeZoneFactory(); |
|
41
|
|
|
$this->dateTimeFactory = new DateTimeFactory($dateTimeZoneFactory, $dateTimeClassName); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string|null $spec |
|
46
|
|
|
* @param null|string|\DateTimeZone $timeZone |
|
47
|
|
|
* |
|
48
|
|
|
* @return \DateTimeImmutable&\DateTimeInterface |
|
49
|
|
|
* |
|
50
|
|
|
* @throws DateTimeFactoryException |
|
51
|
|
|
*/ |
|
52
|
|
|
public function createDateTime(?string $spec = null, $timeZone = null): \DateTimeInterface |
|
53
|
|
|
{ |
|
54
|
|
|
/** @phpstan-ignore-next-line */ |
|
55
|
|
|
return $this->dateTimeFactory->createDateTime($spec, $timeZone); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $spec |
|
60
|
|
|
* @param string $format |
|
61
|
|
|
* @param null|string|\DateTimeZone $timeZone |
|
62
|
|
|
* |
|
63
|
|
|
* @return \DateTimeImmutable&\DateTimeInterface |
|
64
|
|
|
* |
|
65
|
|
|
* @throws DateTimeFactoryException |
|
66
|
|
|
*/ |
|
67
|
|
|
public function createFromFormat(string $spec, string $format, $timeZone = null): \DateTimeInterface |
|
68
|
|
|
{ |
|
69
|
|
|
/** @phpstan-ignore-next-line */ |
|
70
|
|
|
return $this->dateTimeFactory->createDateTime($spec, $timeZone); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|