Passed
Pull Request — master (#1999)
by
unknown
03:23
created

InnerFieldsNameConverter::convertInnerFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Elasticsearch\Serializer\NameConverter;
15
16
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
17
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
18
19
final class InnerFieldsNameConverter implements NameConverterInterface
20
{
21
    private $decorated;
22
23
    public function __construct(?NameConverterInterface $decorated = null)
24
    {
25
        $this->decorated = $decorated ?? new CamelCaseToSnakeCaseNameConverter();
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function normalize($propertyName)
32
    {
33
        return $this->convertInnerFields($propertyName, true);
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function denormalize($propertyName)
40
    {
41
        return $this->convertInnerFields($propertyName, false);
42
    }
43
44
    private function convertInnerFields(string $propertyName, bool $normalization)
45
    {
46
        $convertedProperties = [];
47
48
        foreach (explode('.', $propertyName) as $decomposedProperty) {
49
            $convertedProperties[] = $this->decorated->{$normalization ? 'normalize' : 'denormalize'}($decomposedProperty);
50
        }
51
52
        return implode('.', $convertedProperties);
53
    }
54
}
55