1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DDDominio\EventSourcing\Versioning\JsonTransformer; |
4
|
|
|
|
5
|
|
|
class JsonTransformer |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var TokenExtractor |
9
|
|
|
*/ |
10
|
|
|
private $tokenExtractor; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param TokenExtractor $tokenExtractor |
14
|
|
|
*/ |
15
|
101 |
|
public function __construct(TokenExtractor $tokenExtractor) |
16
|
|
|
{ |
17
|
101 |
|
$this->tokenExtractor = $tokenExtractor; |
18
|
101 |
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $jsonString |
22
|
|
|
* @param $pathExpression |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
10 |
View Code Duplication |
public function removeKey($jsonString, $pathExpression) |
|
|
|
|
26
|
|
|
{ |
27
|
10 |
|
$decodedJson = json_decode($jsonString); |
28
|
10 |
|
$tokens = $this->tokenExtractor->extract($pathExpression); |
29
|
10 |
|
$currentNode = &$decodedJson; |
30
|
10 |
|
while (count($tokens) > 0) { |
31
|
10 |
|
$currentToken = array_shift($tokens); |
32
|
10 |
|
if (count($tokens) === 0) { |
33
|
10 |
|
if ($currentToken instanceof ArrayAccessToken) { |
34
|
4 |
|
unset($currentNode[$currentToken->index()]); |
35
|
4 |
|
$currentNode = array_values($currentNode); |
36
|
6 |
|
} else if ($currentToken instanceof ObjectAccessToken) { |
37
|
10 |
|
unset($currentNode->{$currentToken->fieldName()}); |
38
|
|
|
} |
39
|
|
|
} else { |
40
|
3 |
|
if ($currentToken instanceof ArrayAccessToken) { |
41
|
1 |
|
$currentNode = &$currentNode[$currentToken->index()]; |
42
|
3 |
|
} else if ($currentToken instanceof ObjectAccessToken) { |
43
|
3 |
|
$currentNode = &$currentNode->{$currentToken->fieldName()}; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
10 |
|
return json_encode($decodedJson); |
48
|
|
|
} |
49
|
|
|
|
50
|
8 |
View Code Duplication |
public function addKey($jsonString, $pathExpression, $value) |
|
|
|
|
51
|
|
|
{ |
52
|
8 |
|
$decodedJson = json_decode($jsonString); |
53
|
8 |
|
$tokens = $this->tokenExtractor->extract($pathExpression); |
54
|
8 |
|
$currentNode = &$decodedJson; |
55
|
8 |
|
while (count($tokens) > 0) { |
56
|
8 |
|
$currentToken = array_shift($tokens); |
57
|
8 |
|
if (count($tokens) === 0) { |
58
|
8 |
|
if ($currentToken instanceof ArrayAccessToken) { |
59
|
|
|
$currentNode[$currentToken->index()] = $value; |
60
|
8 |
|
} else if ($currentToken instanceof ObjectAccessToken) { |
61
|
8 |
|
$currentNode->{$currentToken->fieldName()} = $value; |
62
|
|
|
} |
63
|
|
|
} else { |
64
|
2 |
|
if ($currentToken instanceof ArrayAccessToken) { |
65
|
1 |
|
$currentNode = &$currentNode[$currentToken->index()]; |
66
|
2 |
|
} else if ($currentToken instanceof ObjectAccessToken) { |
67
|
2 |
|
$currentNode = &$currentNode->{$currentToken->fieldName()}; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
8 |
|
return json_encode($decodedJson); |
72
|
|
|
} |
73
|
|
|
|
74
|
17 |
|
public function renameKey($jsonString, $pathExpression, $newName) |
75
|
|
|
{ |
76
|
17 |
|
$decodedJson = json_decode($jsonString); |
77
|
17 |
|
$tokens = $this->tokenExtractor->extract($pathExpression); |
78
|
17 |
|
$currentNode = &$decodedJson; |
79
|
17 |
|
while (count($tokens) > 0) { |
80
|
17 |
|
$currentToken = array_shift($tokens); |
81
|
17 |
|
if (count($tokens) === 0) { |
82
|
17 |
|
if ($currentToken instanceof ObjectAccessToken) { |
83
|
17 |
|
$currentNode->{$newName} = $currentNode->{$currentToken->fieldName()}; |
84
|
17 |
|
unset($currentNode->{$currentToken->fieldName()}); |
85
|
|
|
} |
86
|
|
|
} else { |
87
|
2 |
|
if ($currentToken instanceof ArrayAccessToken) { |
88
|
1 |
|
$currentNode = &$currentNode[$currentToken->index()]; |
89
|
2 |
|
} else if ($currentToken instanceof ObjectAccessToken) { |
90
|
2 |
|
$currentNode = &$currentNode->{$currentToken->fieldName()}; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
17 |
|
return json_encode($decodedJson); |
95
|
|
|
} |
96
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.