DefaultPropertyNamingStrategy::translateName()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 16
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 20
ccs 17
cts 17
cp 1
crap 8
rs 8.4444
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Gson\Internal\Naming;
10
11
use RuntimeException;
12
use Tebru\Gson\PropertyNamingPolicy;
13
use Tebru\Gson\PropertyNamingStrategy;
14
15
/**
16
 * Class DefaultPropertyNamingStrategy
17
 *
18
 * Converts camelCase property names to snake_case
19
 *
20
 * @author Nate Brunette <[email protected]>
21
 */
22
final class DefaultPropertyNamingStrategy implements PropertyNamingStrategy
23
{
24
    /**
25
     * Property naming policy
26
     *
27
     * @var string
28
     */
29
    private $policy;
30
31
    /**
32
     * Constructor
33
     *
34
     * @param string $policy
35
     */
36 29
    public function __construct(string $policy)
37
    {
38 29
        $this->policy = $policy;
39 29
    }
40
41
    /**
42
     * Accepts the PHP class property name and returns the name that should
43
     * appear in json
44
     *
45
     * @param string $propertyName
46
     * @return string
47
     * @throws RuntimeException
48
     */
49 23
    public function translateName(string $propertyName): string
50
    {
51 23
        switch ($this->policy) {
52 23
            case PropertyNamingPolicy::IDENTITY:
53 2
                return $propertyName;
54 21
            case PropertyNamingPolicy::LOWER_CASE_WITH_DASHES:
55 3
                return strtolower($this->prependUpperCaseWith($propertyName, '-'));
56 18
            case PropertyNamingPolicy::LOWER_CASE_WITH_UNDERSCORES:
57 5
                return strtolower($this->prependUpperCaseWith($propertyName, '_'));
58 13
            case PropertyNamingPolicy::UPPER_CASE_WITH_DASHES:
59 3
                return strtoupper($this->prependUpperCaseWith($propertyName, '-'));
60 10
            case PropertyNamingPolicy::UPPER_CASE_WITH_UNDERSCORES:
61 3
                return strtoupper($this->prependUpperCaseWith($propertyName, '_'));
62 7
            case PropertyNamingPolicy::UPPER_CAMEL_CASE:
63 3
                return ucfirst($propertyName);
64 4
            case PropertyNamingPolicy::UPPER_CAMEL_CASE_WITH_SPACES:
65 3
                return ucfirst($this->prependUpperCaseWith($propertyName, ' '));
66
        }
67
68 1
        throw new RuntimeException('Gson: This property naming strategy is not supported');
69
    }
70
71
    /**
72
     * Prepend upper case letters
73
     *
74
     * @param string $string
75
     * @param string $replacement
76
     * @return string
77
     */
78 17
    private function prependUpperCaseWith(string $string, string $replacement): string
79
    {
80 17
        return preg_replace('/(?<!^)([A-Z])/', $replacement.'\\1', $string);
81
    }
82
}
83