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
|
|
|
/** @var DateTimeFactoryInterface|array $dateTimeFactory */ |
51
|
|
|
$dateTimeFactory = $config['date_time_factory'] ?? []; |
52
|
|
|
if (is_array($dateTimeFactory)) { |
|
|
|
|
53
|
|
|
$dateTimeFactory = $this->dateTimeFactoryFactory->create($dateTimeFactory); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** @var DateIntervalFactoryInterface|array $dateIntervalFactory */ |
57
|
|
|
$dateIntervalFactory = $config['date_interval_factory'] ?? []; |
58
|
|
|
if (is_array($dateIntervalFactory)) { |
|
|
|
|
59
|
|
|
$dateIntervalFactory = $this->dateIntervalFactoryFactory->create($dateIntervalFactory); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return new DateFactory($dateTimeFactory, $dateIntervalFactory); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|