Passed
Branch v4.2 (a5186f)
by Masiukevich
09:03
created

PropertyNameConverter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 37
ccs 11
cts 12
cp 0.9167
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 3 1
A denormalize() 0 17 2
1
<?php
2
3
/**
4
 * Messages serializer implementation.
5
 *
6
 * @author  Maksim Masiukevich <[email protected]>
7
 * @license MIT
8
 * @license https://opensource.org/licenses/MIT
9
 */
10
11
declare(strict_types = 1);
12
13
namespace ServiceBus\MessageSerializer\Symfony\Extensions;
14
15
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
16
17
/**
18
 * Convert snake_case to lowerCamelCase.
19
 */
20
final class PropertyNameConverter implements NameConverterInterface
21
{
22
    /**
23
     * Local cache.
24
     *
25
     * @psalm-var array<string, string>
26
     */
27
    private $localStorage = [];
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 8
    public function normalize(string $propertyName): string
33
    {
34 8
        return $propertyName;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 7
    public function denormalize(string $propertyName): string
41
    {
42 7
        if (isset($this->localStorage[$propertyName]) === false)
43
        {
44 7
            $joinedString = \preg_replace_callback(
45 7
                '/_(.?)/',
46 7
                static function (array $matches): string
47
                {
48
                    return \ucfirst((string) $matches[1]);
49 7
                },
50 7
                $propertyName
51
            );
52
53 7
            $this->localStorage[$propertyName] = \lcfirst((string) $joinedString);
54
        }
55
56 7
        return $this->localStorage[$propertyName];
57
    }
58
}
59