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

LocaleAwareCamelKeysNormalizer::normalizeString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
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