1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/jms-serializer-module |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\JmsSerializerModule\NamingStrategy; |
7
|
|
|
|
8
|
|
|
use JMS\Serializer\Naming\PropertyNamingStrategyInterface; |
9
|
|
|
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface; |
10
|
|
|
use Zend\ServiceManager\AbstractFactoryInterface; |
11
|
|
|
use Zend\ServiceManager\AbstractPluginManager; |
12
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
13
|
|
|
use Nnx\JmsSerializerModule\Options\ModuleOptions; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class NamingStrategyAbstractFactory |
17
|
|
|
* |
18
|
|
|
* @package Nnx\JmsSerializerModule\NamingStrategy |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
class NamingStrategyAbstractFactory implements AbstractFactoryInterface |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @inheritdoc |
24
|
|
|
* |
25
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
26
|
|
|
* @param $name |
27
|
|
|
* @param $requestedName |
28
|
|
|
* |
29
|
|
|
* @return bool|void |
30
|
|
|
*/ |
31
|
|
|
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
32
|
|
|
{ |
33
|
|
|
return 0 === strpos($requestedName, 'nnxJmsSerializer.namingStrategies.'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
* |
39
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
40
|
|
|
* @param $name |
41
|
|
|
* @param $requestedName |
42
|
|
|
* |
43
|
|
|
* @return PropertyNamingStrategyInterface |
44
|
|
|
* @throws \Nnx\JmsSerializerModule\NamingStrategy\Exception\RuntimeException |
45
|
|
|
* @throws \Nnx\JmsSerializerModule\Options\Exception\InvalidArgumentException |
46
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
47
|
|
|
*/ |
48
|
|
|
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
49
|
|
|
{ |
50
|
|
|
$appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() : $serviceLocator; |
51
|
|
|
|
52
|
|
|
$strategyName = substr($requestedName, 34); |
53
|
|
|
|
54
|
|
|
/** @var ModuleOptionsPluginManagerInterface $moduleOptionsManager */ |
55
|
|
|
$moduleOptionsManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class); |
56
|
|
|
|
57
|
|
|
/** @var ModuleOptions $moduleOptions */ |
58
|
|
|
$moduleOptions = $moduleOptionsManager->get(ModuleOptions::class); |
59
|
|
|
|
60
|
|
|
$strategyConfig = $moduleOptions->getNamingStrategy($strategyName); |
61
|
|
|
|
62
|
|
|
$name = $strategyConfig->getName(); |
63
|
|
|
$options = $strategyConfig->getOptions(); |
64
|
|
|
|
65
|
|
|
$strategy = $serviceLocator->get( |
66
|
|
|
$name, |
67
|
|
|
$options |
|
|
|
|
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
if (!$strategy instanceof PropertyNamingStrategyInterface) { |
71
|
|
|
$errMsg = sprintf( |
72
|
|
|
'Naming strategy of type %s is invalid; must implement %s', |
73
|
|
|
(is_object($strategy) ? get_class($strategy) : gettype($strategy)), |
74
|
|
|
PropertyNamingStrategyInterface::class |
75
|
|
|
); |
76
|
|
|
throw new Exception\RuntimeException($errMsg); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $strategy; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.