Completed
Push — master ( 111bcd...99ca87 )
by Andrey
26:30
created

NamingStrategyTrait::getCreationOptions()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
nc 1
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 Zend\ServiceManager\ServiceLocatorInterface;
9
use JMS\Serializer\Naming\PropertyNamingStrategyInterface;
10
11
/**
12
 * Class NamingStrategyTrait
13
 *
14
 * @package Nnx\JmsSerializerModule\Visitor
15
 */
16
trait NamingStrategyTrait
17
{
18
    /**
19
     * Настройки переданные при создание
20
     *
21
     * @return array
22
     */
23
    abstract public function getCreationOptions();
24
25
26
    /**
27
     * Возвращает стратегию для работы с именами свойств объектов
28
     *
29
     * @param ServiceLocatorInterface $serviceLocator
30
     *
31
     * @return PropertyNamingStrategyInterface
32
     * @throws \Nnx\JmsSerializerModule\Visitor\Exception\RuntimeException
33
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
34
     */
35
    protected function getNamingStrategyFromContainer(ServiceLocatorInterface $serviceLocator)
36
    {
37
        $creationOptions = $this->getCreationOptions();
38
        if (!array_key_exists('namingStrategy', $creationOptions)) {
39
            $errMsg = 'Naming strategy not specified';
40
            throw new Exception\RuntimeException($errMsg);
41
        }
42
43
        $namingStrategy = $creationOptions['namingStrategy'];
44
45
        return $serviceLocator->get($namingStrategy);
46
    }
47
}
48