|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/nnx-framework/jms-serializer-module |
|
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Nnx\JmsSerializerModule\Visitor; |
|
7
|
|
|
|
|
8
|
|
|
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface; |
|
9
|
|
|
use Zend\ServiceManager\AbstractFactoryInterface; |
|
10
|
|
|
use Zend\ServiceManager\AbstractPluginManager; |
|
11
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
12
|
|
|
use Nnx\JmsSerializerModule\Options\ModuleOptions; |
|
13
|
|
|
use JMS\Serializer\Construction\ObjectConstructorInterface; |
|
14
|
|
|
use PhpCollection\Map; |
|
15
|
|
|
use ReflectionClass; |
|
16
|
|
|
use JMS\Serializer\VisitorInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class VisitorsMapAbstractFactory |
|
20
|
|
|
* |
|
21
|
|
|
* @package Nnx\JmsSerializerModule\Visitor |
|
22
|
|
|
*/ |
|
23
|
|
|
class VisitorsMapAbstractFactory implements AbstractFactoryInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @inheritdoc |
|
27
|
|
|
* |
|
28
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
|
29
|
|
|
* @param $name |
|
30
|
|
|
* @param $requestedName |
|
31
|
|
|
* |
|
32
|
|
|
* @return bool|void |
|
33
|
|
|
*/ |
|
34
|
|
|
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
|
35
|
|
|
{ |
|
36
|
|
|
return 0 === strpos($requestedName, 'nnxJmsSerializer.serializationVisitors.') || 0 === strpos($requestedName, 'nnxJmsSerializer.deserializationVisitors.default'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @inheritdoc |
|
41
|
|
|
* |
|
42
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
|
43
|
|
|
* @param $name |
|
44
|
|
|
* @param $requestedName |
|
45
|
|
|
* |
|
46
|
|
|
* @return ObjectConstructorInterface |
|
47
|
|
|
* @throws \Nnx\JmsSerializerModule\Visitor\Exception\RuntimeException |
|
48
|
|
|
* @throws \Nnx\JmsSerializerModule\ObjectConstructor\Exception\RuntimeException |
|
49
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
|
50
|
|
|
* @throws \Nnx\JmsSerializerModule\Options\Exception\InvalidArgumentException |
|
51
|
|
|
*/ |
|
52
|
|
|
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
|
53
|
|
|
{ |
|
54
|
|
|
$appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() : $serviceLocator; |
|
55
|
|
|
|
|
56
|
|
|
$name = null; |
|
57
|
|
|
|
|
58
|
|
|
$isSerializationVisitors = 0 === strpos($requestedName, 'nnxJmsSerializer.serializationVisitors.'); |
|
59
|
|
|
$isDeserializationVisitors = 0 === strpos($requestedName, 'nnxJmsSerializer.deserializationVisitors.default'); |
|
60
|
|
|
|
|
61
|
|
|
if ($isSerializationVisitors) { |
|
62
|
|
|
$name = substr($requestedName, 39); |
|
63
|
|
|
} elseif ($isDeserializationVisitors) { |
|
64
|
|
|
$name = substr($requestedName, 41); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** @var ModuleOptionsPluginManagerInterface $moduleOptionsManager */ |
|
68
|
|
|
$moduleOptionsManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class); |
|
69
|
|
|
|
|
70
|
|
|
/** @var ModuleOptions $moduleOptions */ |
|
71
|
|
|
$moduleOptions = $moduleOptionsManager->get(ModuleOptions::class); |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
$list = []; |
|
75
|
|
|
|
|
76
|
|
|
if ($isSerializationVisitors) { |
|
77
|
|
|
$list = $moduleOptions->getSerializationVisitor($name); |
|
78
|
|
|
} elseif ($isDeserializationVisitors) { |
|
79
|
|
|
$list = $moduleOptions->getDeserializationVisitor($name); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$map = new Map(); |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
foreach ($list as $format => $visitorName) { |
|
|
|
|
|
|
86
|
|
|
$visitor = null; |
|
87
|
|
|
if (is_string($visitorName)) { |
|
88
|
|
|
if ($serviceLocator->has($visitorName)) { |
|
89
|
|
|
$visitor = $serviceLocator->get($visitorName); |
|
90
|
|
|
} elseif (class_exists($visitorName)) { |
|
91
|
|
|
$r = new ReflectionClass($visitorName); |
|
92
|
|
|
$visitor = $r->newInstance(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (!$visitor instanceof VisitorInterface) { |
|
97
|
|
|
$errMsg = sprintf( |
|
98
|
|
|
'Visitor of type %s is invalid; must implement %s', |
|
99
|
|
|
(is_object($visitor) ? get_class($visitor) : gettype($visitor)), |
|
100
|
|
|
VisitorInterface::class |
|
101
|
|
|
); |
|
102
|
|
|
throw new Exception\RuntimeException($errMsg); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
$map->set($format, $visitor); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
return $map; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.