1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\LaminasDateTime\Factory\Hydrator\Strategy; |
6
|
|
|
|
7
|
|
|
use Arp\LaminasFactory\AbstractFactory; |
8
|
|
|
use Interop\Container\ContainerInterface; |
9
|
|
|
use Laminas\Hydrator\Strategy\DateTimeFormatterStrategy; |
10
|
|
|
use Laminas\Hydrator\Strategy\Exception\InvalidArgumentException; |
11
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
12
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotFoundException; |
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
|
|
|
* @noinspection PhpMissingParamTypeInspection |
22
|
|
|
* |
23
|
|
|
* @param ContainerInterface $container |
24
|
|
|
* @param string $requestedName |
25
|
|
|
* @param array|null $options |
26
|
|
|
* |
27
|
|
|
* @return DateTimeFormatterStrategy |
28
|
|
|
* |
29
|
|
|
* @throws ServiceNotCreatedException |
30
|
|
|
* @throws ServiceNotFoundException |
31
|
|
|
*/ |
32
|
|
|
public function __invoke( |
33
|
|
|
ContainerInterface $container, |
34
|
|
|
$requestedName, |
35
|
|
|
array $options = null |
36
|
|
|
): DateTimeFormatterStrategy { |
37
|
|
|
$options = $options ?? $this->getServiceOptions($container, $requestedName); |
38
|
|
|
|
39
|
|
|
$format = $options['format'] ?? null; |
40
|
|
|
if (null === $format) { |
41
|
|
|
throw new ServiceNotCreatedException( |
42
|
|
|
sprintf( |
43
|
|
|
'The required \'format\' configuration option is missing for service \'%s\'', |
44
|
|
|
$requestedName |
45
|
|
|
) |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
try { |
50
|
|
|
return new DateTimeFormatterStrategy($format, $options['timezone'] ?? null, $options['fallback'] ?? false); |
51
|
|
|
} catch (InvalidArgumentException $e) { |
52
|
|
|
throw new ServiceNotCreatedException( |
53
|
|
|
sprintf('Failed to create date time strategy \'%s\': %s', $requestedName, $e->getMessage()), |
54
|
|
|
$e->getCode(), |
55
|
|
|
$e |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|