Completed
Pull Request — master (#1036)
by Hector
03:29
created

CamelCaseToDashedCaseNameConverter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 59
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B normalize() 0 20 5
B denormalize() 0 16 5
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\Serializer\NameConverter;
15
16
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
17
18
/**
19
 * CamelCase to dashed name converter.
20
 *
21
 * Based on Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter.
22
 *
23
 * @author Kévin Dunglas <[email protected]>
24
 */
25
class CamelCaseToDashedCaseNameConverter implements NameConverterInterface
26
{
27
    private $attributes;
28
    private $lowerCamelCase;
29
30
    /**
31
     * @param null|array $attributes     The list of attributes to rename or null for all attributes
32
     * @param bool       $lowerCamelCase Use lowerCamelCase style
33
     */
34
    public function __construct(array $attributes = null, bool $lowerCamelCase = true)
35
    {
36
        $this->attributes = $attributes;
37
        $this->lowerCamelCase = $lowerCamelCase;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function normalize($propertyName)
44
    {
45
        if (null === $this->attributes || in_array($propertyName, $this->attributes, true)) {
46
            $lcPropertyName = lcfirst($propertyName);
47
            $snakeCasedName = '';
48
49
            $len = strlen($lcPropertyName);
50
            for ($i = 0; $i < $len; ++$i) {
51
                if (ctype_upper($lcPropertyName[$i])) {
52
                    $snakeCasedName .= '-'.strtolower($lcPropertyName[$i]);
53
                } else {
54
                    $snakeCasedName .= strtolower($lcPropertyName[$i]);
55
                }
56
            }
57
58
            return $snakeCasedName;
59
        }
60
61
        return $propertyName;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function denormalize($propertyName)
68
    {
69
        $camelCasedName = preg_replace_callback('/(^|-|\.)+(.)/', function ($match) {
70
            return ('.' === $match[1] ? '-' : '').strtoupper($match[2]);
71
        }, $propertyName);
72
73
        if ($this->lowerCamelCase) {
74
            $camelCasedName = lcfirst($camelCasedName);
75
        }
76
77
        if (null === $this->attributes || in_array($camelCasedName, $this->attributes, true)) {
78
            return $camelCasedName;
79
        }
80
81
        return $propertyName;
82
    }
83
}
84