1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\LaminasDateTime\Factory\Hydrator\Strategy; |
6
|
|
|
|
7
|
|
|
use Arp\LaminasFactory\AbstractFactory; |
8
|
|
|
use Laminas\Hydrator\Strategy\DateTimeFormatterStrategy; |
9
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
10
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotFoundException; |
11
|
|
|
use Psr\Container\ContainerExceptionInterface; |
12
|
|
|
use Psr\Container\ContainerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Alex Patterson <[email protected]> |
16
|
|
|
* @package Arp\LaminasDateTime\Factory\Hydrator\Strategy |
17
|
|
|
*/ |
18
|
|
|
final class DateTimeStrategyFactory extends AbstractFactory |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param ContainerInterface $container |
22
|
|
|
* @param string $requestedName |
23
|
|
|
* @param array<string, mixed>|null $options |
24
|
|
|
* |
25
|
|
|
* @return DateTimeFormatterStrategy |
26
|
|
|
* |
27
|
|
|
* @throws ServiceNotCreatedException |
28
|
|
|
* @throws ServiceNotFoundException |
29
|
|
|
* @throws ContainerExceptionInterface |
30
|
|
|
*/ |
31
|
|
|
public function __invoke( |
32
|
|
|
ContainerInterface $container, |
33
|
|
|
string $requestedName, |
34
|
|
|
array $options = null |
35
|
|
|
): DateTimeFormatterStrategy { |
36
|
|
|
$options = $options ?? $this->getServiceOptions($container, $requestedName); |
37
|
|
|
|
38
|
|
|
$format = $options['format'] ?? null; |
39
|
|
|
if (null === $format) { |
40
|
|
|
throw new ServiceNotCreatedException( |
41
|
|
|
sprintf( |
42
|
|
|
'The required \'format\' configuration option is missing for service \'%s\'', |
43
|
|
|
$requestedName |
44
|
|
|
) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return new DateTimeFormatterStrategy($format, $options['timezone'] ?? null, $options['fallback'] ?? false); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|