1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\LaminasEntity\Factory\Hydrator; |
6
|
|
|
|
7
|
|
|
use Arp\LaminasEntity\Hydrator\EntityHydrator; |
8
|
|
|
use Arp\LaminasFactory\AbstractFactory; |
9
|
|
|
use Arp\LaminasFactory\Exception\ServiceNotCreatedException; |
10
|
|
|
use Doctrine\ORM\EntityManager; |
11
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
12
|
|
|
use Interop\Container\ContainerInterface; |
13
|
|
|
use Laminas\Hydrator\NamingStrategy\NamingStrategyEnabledInterface; |
14
|
|
|
use Laminas\Hydrator\Strategy\StrategyEnabledInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Alex Patterson <[email protected]> |
18
|
|
|
* @package Arp\LaminasEntity\Factory\Hydrator |
19
|
|
|
*/ |
20
|
|
|
class EntityHydratorFactory extends AbstractFactory |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private string $defaultClassName = EntityHydrator::class; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ContainerInterface $container |
29
|
|
|
* @param string $requestedName |
30
|
|
|
* @param array|null $options |
31
|
|
|
* |
32
|
|
|
* @return EntityHydrator |
33
|
|
|
* |
34
|
|
|
* @throws ServiceNotCreatedException |
35
|
|
|
*/ |
36
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): EntityHydrator |
37
|
|
|
{ |
38
|
|
|
$options = $options ?? $this->getServiceOptions($container, $requestedName, 'hydrators'); |
39
|
|
|
|
40
|
|
|
$className = $options['class_name'] ?? $this->defaultClassName; |
41
|
|
|
$byValue = $options['by_value'] ?? false; |
42
|
|
|
|
43
|
|
|
$entityManager = $this->getEntityManager($container, $requestedName, $options); |
44
|
|
|
|
45
|
|
|
/** @var EntityHydrator $hydrator */ |
46
|
|
|
$hydrator = new $className($entityManager, $byValue); |
47
|
|
|
|
48
|
|
|
$namingStrategy = $options['naming_strategy'] ?? null; |
49
|
|
|
if (null !== $namingStrategy && $hydrator instanceof NamingStrategyEnabledInterface) { |
50
|
|
|
if (is_string($namingStrategy)) { |
51
|
|
|
$namingStrategy = $this->getService($container, $namingStrategy, $requestedName); |
52
|
|
|
} |
53
|
|
|
$hydrator->setNamingStrategy($namingStrategy); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$strategies = $options['strategies'] ?? []; |
57
|
|
|
if (! empty($strategies) && $hydrator instanceof StrategyEnabledInterface) { |
58
|
|
|
foreach ($strategies as $name => $strategy) { |
59
|
|
|
if (is_string($strategy)) { |
60
|
|
|
$strategy = $this->getService($container, $strategy, $requestedName); |
61
|
|
|
} |
62
|
|
|
$hydrator->addStrategy($name, $strategy); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $hydrator; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return the entity manager provided in configuration options. |
71
|
|
|
* |
72
|
|
|
* @param ContainerInterface $container |
73
|
|
|
* @param string $requestedName |
74
|
|
|
* @param array $options |
75
|
|
|
* |
76
|
|
|
* @return EntityManagerInterface |
77
|
|
|
*/ |
78
|
|
|
private function getEntityManager( |
79
|
|
|
ContainerInterface $container, |
80
|
|
|
string $requestedName, |
81
|
|
|
array $options |
82
|
|
|
): EntityManagerInterface { |
83
|
|
|
$entityManager = $options['entity_manager'] ?? EntityManager::class; |
84
|
|
|
|
85
|
|
|
if (null === $entityManager) { |
86
|
|
|
throw new ServiceNotCreatedException( |
87
|
|
|
sprintf( |
88
|
|
|
'The required \'entity_manager\' configuration option is missing for service \'%s\'', |
89
|
|
|
$requestedName |
90
|
|
|
) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->getService($container, $entityManager, $requestedName); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|