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
|
22 |
|
public function __construct(string $policy) |
37
|
|
|
{ |
38
|
22 |
|
$this->policy = $policy; |
39
|
22 |
|
} |
40
|
|
|
/** |
41
|
|
|
* Accepts the PHP class property name and returns the name that should |
42
|
|
|
* appear in json |
43
|
|
|
* |
44
|
|
|
* @param string $propertyName |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
21 |
|
public function translateName(string $propertyName): string |
48
|
|
|
{ |
49
|
21 |
|
switch ($this->policy) { |
50
|
21 |
|
case PropertyNamingPolicy::IDENTITY: |
51
|
2 |
|
return $propertyName; |
52
|
19 |
|
case PropertyNamingPolicy::LOWER_CASE_WITH_DASHES: |
53
|
3 |
|
return strtolower($this->prependUpperCaseWith($propertyName, '-')); |
54
|
16 |
|
case PropertyNamingPolicy::LOWER_CASE_WITH_UNDERSCORES: |
55
|
3 |
|
return strtolower($this->prependUpperCaseWith($propertyName, '_')); |
56
|
13 |
|
case PropertyNamingPolicy::UPPER_CASE_WITH_DASHES: |
57
|
3 |
|
return strtoupper($this->prependUpperCaseWith($propertyName, '-')); |
58
|
10 |
|
case PropertyNamingPolicy::UPPER_CASE_WITH_UNDERSCORES: |
59
|
3 |
|
return strtoupper($this->prependUpperCaseWith($propertyName, '_')); |
60
|
7 |
|
case PropertyNamingPolicy::UPPER_CAMEL_CASE: |
61
|
3 |
|
return ucfirst($propertyName); |
62
|
4 |
|
case PropertyNamingPolicy::UPPER_CAMEL_CASE_WITH_SPACES: |
63
|
3 |
|
return ucfirst($this->prependUpperCaseWith($propertyName, ' ')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** @noinspection ExceptionsAnnotatingAndHandlingInspection */ |
67
|
1 |
|
throw new RuntimeException('Gson: This property naming strategy is not supported'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Prepend upper case letters |
72
|
|
|
* |
73
|
|
|
* @param string $string |
74
|
|
|
* @param string $replacement |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
15 |
|
private function prependUpperCaseWith(string $string, string $replacement): string |
78
|
|
|
{ |
79
|
15 |
|
return preg_replace('/(?<!^)([A-Z])/', $replacement.'\\1', $string); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|