Completed
Push — master ( 5c38af...d37987 )
by Markus
48s queued 15s
created

PropertyNameConverter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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