PropertyNameConverter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 13
c 1
b 0
f 0
dl 0
loc 53
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A denormalize() 0 11 3
A normalize() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\SerializerSymfony\NameConverter;
6
7
use Jellyfish\Serializer\NameConverter\PropertyNameConverterStrategyProviderInterface;
8
9
class PropertyNameConverter implements PropertyNameConverterInterface
10
{
11
    /**
12
     * @var \Jellyfish\Serializer\NameConverter\PropertyNameConverterStrategyProviderInterface
13
     */
14
    protected $strategyProvider;
15
16
    /**
17
     * @param \Jellyfish\Serializer\NameConverter\PropertyNameConverterStrategyProviderInterface $strategyProvider
18
     */
19
    public function __construct(PropertyNameConverterStrategyProviderInterface $strategyProvider)
20
    {
21
        $this->strategyProvider = $strategyProvider;
22
    }
23
24
    /**
25
     * @param string $propertyName
26
     * @param string|null $class
27
     * @param string|null $format
28
     *
29
     * @return string
30
     */
31
    public function normalize($propertyName, ?string $class = null, ?string $format = null): string
32
    {
33
        foreach ($this->strategyProvider->getAllStrategies() as $strategy) {
34
            $convertedPropertyName = $strategy->convertAfterNormalize($propertyName, $class, $format);
35
36
            if ($convertedPropertyName !== null) {
37
                return $convertedPropertyName;
38
            }
39
        }
40
41
        return $propertyName;
42
    }
43
44
    /**
45
     * @param string $propertyName
46
     * @param string|null $class
47
     * @param string|null $format
48
     *
49
     * @return string
50
     */
51
    public function denormalize($propertyName, ?string $class = null, ?string $format = null): string
52
    {
53
        foreach ($this->strategyProvider->getAllStrategies() as $strategy) {
54
            $convertedPropertyName = $strategy->convertAfterDenormalize($propertyName, $class, $format);
55
56
            if ($convertedPropertyName !== null) {
57
                return $convertedPropertyName;
58
            }
59
        }
60
61
        return $propertyName;
62
    }
63
}
64