|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arp\DateTime\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use Arp\DateTime\DateFactory; |
|
8
|
|
|
use Arp\DateTime\DateIntervalFactoryInterface; |
|
9
|
|
|
use Arp\DateTime\DateTimeFactoryInterface; |
|
10
|
|
|
use Arp\Factory\Exception\FactoryException; |
|
11
|
|
|
use Arp\Factory\FactoryInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author Alex Patterson <[email protected]> |
|
15
|
|
|
* @package Arp\DateTime\Factory |
|
16
|
|
|
*/ |
|
17
|
|
|
final class DateFactoryFactory implements FactoryInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var FactoryInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private FactoryInterface $dateTimeFactoryFactory; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var FactoryInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private FactoryInterface $dateIntervalFactoryFactory; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param FactoryInterface|null $dateTimeFactoryFactory |
|
31
|
|
|
* @param FactoryInterface|null $dateIntervalFactoryFactory |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct( |
|
34
|
|
|
FactoryInterface $dateTimeFactoryFactory = null, |
|
35
|
|
|
FactoryInterface $dateIntervalFactoryFactory = null |
|
36
|
|
|
) { |
|
37
|
|
|
$this->dateTimeFactoryFactory = $dateTimeFactoryFactory ?? new DateTimeFactoryFactory(); |
|
38
|
|
|
$this->dateIntervalFactoryFactory = $dateIntervalFactoryFactory ?? new DateIntervalFactoryFactory(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $config |
|
43
|
|
|
* |
|
44
|
|
|
* @return DateFactory |
|
45
|
|
|
* |
|
46
|
|
|
* @throws FactoryException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function create(array $config = []): DateFactory |
|
49
|
|
|
{ |
|
50
|
|
|
return new DateFactory($this->createDateTimeFactory($config), $this->createDateIntervalFactory($config)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param array $config |
|
55
|
|
|
* |
|
56
|
|
|
* @return DateTimeFactoryInterface |
|
57
|
|
|
* |
|
58
|
|
|
* @throws FactoryException |
|
59
|
|
|
*/ |
|
60
|
|
|
private function createDateTimeFactory(array $config): DateTimeFactoryInterface |
|
61
|
|
|
{ |
|
62
|
|
|
/** @var DateTimeFactoryInterface|array $dateTimeFactory */ |
|
63
|
|
|
$dateTimeFactory = $config['date_time_factory'] ?? []; |
|
64
|
|
|
|
|
65
|
|
|
if (is_array($dateTimeFactory)) { |
|
|
|
|
|
|
66
|
|
|
$dateTimeFactory = $this->dateTimeFactoryFactory->create($dateTimeFactory); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (!$dateTimeFactory instanceof DateTimeFactoryInterface) { |
|
70
|
|
|
throw new FactoryException( |
|
71
|
|
|
sprintf( |
|
72
|
|
|
'The \'date_time_factory\' argument could not be resolved to an object of type \'%s\'', |
|
73
|
|
|
DateTimeFactoryInterface::class |
|
74
|
|
|
) |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $dateTimeFactory; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param array $config |
|
83
|
|
|
* |
|
84
|
|
|
* @return DateIntervalFactoryInterface |
|
85
|
|
|
* |
|
86
|
|
|
* @throws FactoryException |
|
87
|
|
|
*/ |
|
88
|
|
|
private function createDateIntervalFactory(array $config): DateIntervalFactoryInterface |
|
89
|
|
|
{ |
|
90
|
|
|
/** @var DateIntervalFactoryInterface|array $dateIntervalFactory */ |
|
91
|
|
|
$dateIntervalFactory = $config['date_interval_factory'] ?? []; |
|
92
|
|
|
|
|
93
|
|
|
if (is_array($dateIntervalFactory)) { |
|
|
|
|
|
|
94
|
|
|
$dateIntervalFactory = $this->dateIntervalFactoryFactory->create($dateIntervalFactory); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (!$dateIntervalFactory instanceof DateIntervalFactoryInterface) { |
|
98
|
|
|
throw new FactoryException( |
|
99
|
|
|
sprintf( |
|
100
|
|
|
'The \'date_interval_factory\' argument could not be resolved to an object of type \'%s\'', |
|
101
|
|
|
DateIntervalFactoryInterface::class |
|
102
|
|
|
) |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $dateIntervalFactory; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|