1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\LaminasDoctrine\Factory\Hydrator\Strategy; |
6
|
|
|
|
7
|
|
|
use Arp\Entity\EntityInterface; |
8
|
|
|
use Arp\LaminasDoctrine\Hydrator\Strategy\HydratorStrategy; |
9
|
|
|
use Arp\LaminasDoctrine\Repository\EntityRepositoryInterface; |
10
|
|
|
use Arp\LaminasDoctrine\Repository\RepositoryManager; |
11
|
|
|
use Arp\LaminasFactory\AbstractFactory; |
12
|
|
|
use Laminas\Hydrator\HydratorPluginManager; |
13
|
|
|
use Laminas\Hydrator\Strategy\Exception\InvalidArgumentException; |
14
|
|
|
use Laminas\Hydrator\Strategy\HydratorStrategy as LaminasHydratorStrategy; |
15
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
16
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotFoundException; |
17
|
|
|
use Psr\Container\ContainerExceptionInterface; |
18
|
|
|
use Psr\Container\ContainerInterface; |
19
|
|
|
|
20
|
|
|
final class HydratorStrategyFactory extends AbstractFactory |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param ContainerInterface $container |
24
|
|
|
* @param string $requestedName |
25
|
|
|
* @param array<string, mixed>|null $options |
26
|
|
|
* |
27
|
|
|
* @return HydratorStrategy |
28
|
|
|
* |
29
|
|
|
* @throws ServiceNotCreatedException |
30
|
|
|
* @throws ServiceNotFoundException |
31
|
|
|
* @throws InvalidArgumentException |
32
|
|
|
* @throws ContainerExceptionInterface |
33
|
|
|
*/ |
34
|
|
|
public function __invoke( |
35
|
|
|
ContainerInterface $container, |
36
|
|
|
string $requestedName, |
37
|
|
|
array $options = null |
38
|
|
|
): HydratorStrategy { |
39
|
|
|
$options = $options ?? $this->getServiceOptions($container, $requestedName); |
40
|
|
|
|
41
|
|
|
$entityName = $options['entity_name'] ?? null; |
42
|
|
|
if (null === $entityName) { |
43
|
|
|
throw new ServiceNotCreatedException( |
44
|
|
|
sprintf( |
45
|
|
|
'The required \'entity_name\' configuration option is missing for service \'%s\'', |
46
|
|
|
$requestedName |
47
|
|
|
) |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$hydrator = $options['hydrator'] ?? null; |
52
|
|
|
if (null === $hydrator) { |
53
|
|
|
throw new ServiceNotCreatedException( |
54
|
|
|
sprintf( |
55
|
|
|
'The required \'hydrator\' configuration option is missing for service \'%s\'', |
56
|
|
|
$requestedName |
57
|
|
|
) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** @var HydratorPluginManager $hydratorManager */ |
62
|
|
|
$hydratorManager = $this->getService($container, 'HydratorManager', $requestedName); |
63
|
|
|
|
64
|
|
|
$laminasHydrator = new LaminasHydratorStrategy( |
65
|
|
|
$this->getService($hydratorManager, $hydrator, $requestedName), |
|
|
|
|
66
|
|
|
$entityName |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
/** @var RepositoryManager $repositoryManager */ |
70
|
|
|
$repositoryManager = $this->getService($container, RepositoryManager::class, $requestedName); |
71
|
|
|
|
72
|
|
|
/** @var EntityRepositoryInterface<EntityInterface> $repository */ |
73
|
|
|
$repository = $this->getService($repositoryManager, $entityName, $requestedName); |
74
|
|
|
|
75
|
|
|
return new HydratorStrategy($repository, $laminasHydrator); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|