|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/nnx-framework/jms-serializer-module |
|
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Nnx\JmsSerializerModule\ObjectConstructor; |
|
7
|
|
|
|
|
8
|
|
|
use Zend\ServiceManager\FactoryInterface; |
|
9
|
|
|
use Zend\ServiceManager\MutableCreationOptionsInterface; |
|
10
|
|
|
use Zend\ServiceManager\MutableCreationOptionsTrait; |
|
11
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
12
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
13
|
|
|
use JMS\Serializer\Construction\ObjectConstructorInterface; |
|
14
|
|
|
use JMS\Serializer\Construction\DoctrineObjectConstructor; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class DoctrineObjectConstructorFactory |
|
18
|
|
|
* |
|
19
|
|
|
* @package Nnx\JmsSerializerModule\ObjectConstructor |
|
20
|
|
|
*/ |
|
21
|
|
|
class DoctrineObjectConstructorFactory implements FactoryInterface, MutableCreationOptionsInterface |
|
22
|
|
|
{ |
|
23
|
|
|
use MutableCreationOptionsTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @inheritdoc |
|
27
|
|
|
* |
|
28
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
|
29
|
|
|
* |
|
30
|
|
|
* @return DoctrineObjectConstructor |
|
31
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
|
32
|
|
|
* @throws \Nnx\JmsSerializerModule\ObjectConstructor\Exception\RuntimeException |
|
33
|
|
|
*/ |
|
34
|
|
|
public function createService(ServiceLocatorInterface $serviceLocator) |
|
35
|
|
|
{ |
|
36
|
|
|
$creationOptions = $this->getCreationOptions(); |
|
37
|
|
|
|
|
38
|
|
|
if (!array_key_exists('managerRegistry', $creationOptions)) { |
|
39
|
|
|
$errMsg = 'Manager registry service name not specified'; |
|
40
|
|
|
throw new Exception\RuntimeException($errMsg); |
|
41
|
|
|
} |
|
42
|
|
|
$managerRegistry = $serviceLocator->get($creationOptions['managerRegistry']); |
|
43
|
|
|
|
|
44
|
|
|
if (!$managerRegistry instanceof ManagerRegistry) { |
|
45
|
|
|
$errMsg = sprintf( |
|
46
|
|
|
'Manager registry of type %s is invalid; must implement %s', |
|
47
|
|
|
(is_object($managerRegistry) ? get_class($managerRegistry) : gettype($managerRegistry)), |
|
48
|
|
|
ManagerRegistry::class |
|
49
|
|
|
); |
|
50
|
|
|
throw new Exception\RuntimeException($errMsg); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if (!array_key_exists('fallbackConstructor', $creationOptions)) { |
|
54
|
|
|
$errMsg = 'Fallback constructor name not specified'; |
|
55
|
|
|
throw new Exception\RuntimeException($errMsg); |
|
56
|
|
|
} |
|
57
|
|
|
$fallbackConstructor = $serviceLocator->get($creationOptions['fallbackConstructor']); |
|
58
|
|
|
|
|
59
|
|
|
if (!$fallbackConstructor instanceof ObjectConstructorInterface) { |
|
60
|
|
|
$errMsg = sprintf( |
|
61
|
|
|
'Fallback constructor of type %s is invalid; must implement %s', |
|
62
|
|
|
(is_object($fallbackConstructor) ? get_class($fallbackConstructor) : gettype($fallbackConstructor)), |
|
63
|
|
|
ObjectConstructorInterface::class |
|
64
|
|
|
); |
|
65
|
|
|
throw new Exception\RuntimeException($errMsg); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return new DoctrineObjectConstructor($managerRegistry, $fallbackConstructor); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|