Completed
Push — locale-in-url ( 6d9eda...81052c )
by Kamil
57:16 queued 38:25
created

LocaleAwareCamelKeysNormalizer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 23
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A normalizeString() 0 15 3
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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
namespace Sylius\Bundle\ApiBundle\Normalizer;
13
14
use FOS\RestBundle\Normalizer\CamelKeysNormalizer;
15
16
/**
17
 * Normalizes the array by changing its keys from underscore to camel case, but does not perform this change if the key is an locale code.
18
 *
19
 * @author Łukasz Chruściel <[email protected]>
20
 */
21
final class LocaleAwareCamelKeysNormalizer extends CamelKeysNormalizer
22
{
23
    /**
24
     * @param string $string
25
     *
26
     * @return string
27
     */
28
    protected function normalizeString($string)
29
    {
30
        if (false === strpos($string, '_')) {
31
            return $string;
32
        }
33
34
        // Custom logic to skip locale codes (e.g. nl_NL)
35
        if (1 === preg_match('/^[a-z]{2}_[A-Z]{2}$/', $string)) {
36
            return $string;
37
        }
38
39
        return preg_replace_callback('/_([a-zA-Z0-9])/', function ($matches) {
40
            return strtoupper($matches[1]);
41
        }, $string);
42
    }
43
}
44