1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TonicForHealth\PagerDutyClient\RepresentProcessor\Representation\Traits; |
4
|
|
|
|
5
|
|
|
use stdClass; |
6
|
|
|
use TonicForHealth\PagerDutyClient\Entity\PagerDutyEntityInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Trait JSONRepresentationTrait. |
10
|
|
|
*/ |
11
|
|
|
trait JSONRepresentationTrait |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param PagerDutyEntityInterface $pagerDutyEntity |
15
|
|
|
* |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
4 |
|
public function persistRepresentJSON(PagerDutyEntityInterface $pagerDutyEntity) |
19
|
|
|
{ |
20
|
4 |
|
return json_encode( |
21
|
4 |
|
$this->persistRepresentObj($pagerDutyEntity), |
22
|
|
|
JSON_PRETTY_PRINT |
23
|
4 |
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $jsonString |
28
|
|
|
* @param PagerDutyEntityInterface|null $pagerDutyEntity |
29
|
|
|
* |
30
|
|
|
* @return PagerDutyEntityInterface |
31
|
|
|
*/ |
32
|
|
|
public function loadRepresentJSON($jsonString, PagerDutyEntityInterface $pagerDutyEntity = null) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
return $this->loadRepresentObj( |
35
|
|
|
json_decode($jsonString) |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param PagerDutyEntityInterface $pagerDutyEntity |
41
|
|
|
* |
42
|
|
|
* @return stdClass |
43
|
|
|
*/ |
44
|
4 |
|
public function persistRepresentObj(PagerDutyEntityInterface $pagerDutyEntity) |
45
|
|
|
{ |
46
|
4 |
|
$representObject = new stdClass(); |
47
|
|
|
|
48
|
4 |
|
$objArray = (array) $pagerDutyEntity; |
49
|
4 |
|
foreach ($objArray as $key => $value) { |
50
|
4 |
|
if (null === $value) { |
51
|
4 |
|
continue; |
52
|
|
|
} |
53
|
4 |
|
$key = $this->cameCaseTransformToUnderscores($key); |
54
|
4 |
|
$representObject->$key = $value; |
55
|
4 |
|
} |
56
|
|
|
|
57
|
4 |
|
return $representObject; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param stdClass $obj |
62
|
|
|
* @param PagerDutyEntityInterface|null $pagerDutyEntity |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function loadRepresentObj(stdClass $obj, PagerDutyEntityInterface $pagerDutyEntity = null) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
if (null === $pagerDutyEntity) { |
67
|
|
|
$entityClassName = static::getEntityClassName(); |
68
|
|
|
$pagerDutyEntity = new $entityClassName(); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
$objArray = (array) $obj; |
71
|
|
|
foreach ($objArray as $key => $value) { |
72
|
|
|
$key = $this->underscoresTransformToCameCase($key); |
73
|
|
|
$representObject->$key = $value; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Decamelize str cameCase string to underscored. |
79
|
|
|
* |
80
|
|
|
* @param string $cameCaseStr |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
4 |
|
protected function cameCaseTransformToUnderscores($cameCaseStr) |
85
|
|
|
{ |
86
|
4 |
|
return ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', $cameCaseStr)), '_'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Decamelize str cameCase string to underscored. |
91
|
|
|
* |
92
|
|
|
* @param string $underscores |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
protected function underscoresTransformToCameCase($underscores) |
97
|
|
|
{ |
98
|
|
|
return ltrim( |
99
|
|
|
preg_replace_callback( |
100
|
|
|
'/_[a-z]/', |
101
|
|
|
function ($matches) { |
102
|
|
|
return substr(strtolower($matches[0]), 1); |
103
|
|
|
}, |
104
|
|
|
$underscores |
105
|
|
|
), |
106
|
|
|
'_' |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.