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 JMS\Serializer\JsonSerializationVisitor; |
9
|
|
|
use Zend\ServiceManager\FactoryInterface; |
10
|
|
|
use Zend\ServiceManager\MutableCreationOptionsInterface; |
11
|
|
|
use Zend\ServiceManager\MutableCreationOptionsTrait; |
12
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
13
|
|
|
use Metadata\Driver\FileLocator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class FileLocatorFactory |
17
|
|
|
* |
18
|
|
|
* @package Nnx\JmsSerializerModule\Visitor |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
class JsonSerializationVisitorFactory implements FactoryInterface, MutableCreationOptionsInterface |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
use MutableCreationOptionsTrait, NamingStrategyTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
26
|
|
|
* |
27
|
|
|
* @return FileLocator |
28
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
29
|
|
|
* @throws \Nnx\JmsSerializerModule\Visitor\Exception\RuntimeException |
30
|
|
|
* @throws \Nnx\JmsSerializerModule\MetadataDriver\Exception\RuntimeException |
31
|
|
|
*/ |
32
|
|
|
public function createService(ServiceLocatorInterface $serviceLocator) |
33
|
|
|
{ |
34
|
|
|
$namingStrategy = $this->getNamingStrategyFromContainer($serviceLocator); |
35
|
|
|
$visitor = new JsonSerializationVisitor($namingStrategy); |
36
|
|
|
|
37
|
|
|
$creationOptions = $this->getCreationOptions(); |
38
|
|
|
if (array_key_exists('jsonOptions', $creationOptions)) { |
39
|
|
|
$jsonOptions = $creationOptions['jsonOptions']; |
40
|
|
|
if (!is_array($jsonOptions)) { |
41
|
|
|
$errMsg = 'Json options is not array'; |
42
|
|
|
throw new Exception\RuntimeException($errMsg); |
43
|
|
|
} |
44
|
|
|
if (array_key_exists('options', $jsonOptions)) { |
45
|
|
|
$visitor->setOptions($jsonOptions['options']); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $visitor; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
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.