1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Common\JsonSchema; |
4
|
|
|
|
5
|
|
|
class JsonPatch |
6
|
|
|
{ |
7
|
|
|
const OP_ADD = 'add'; |
8
|
|
|
const OP_REPLACE = 'replace'; |
9
|
|
|
const OP_REMOVE = 'remove'; |
10
|
|
|
|
11
|
1 |
|
public static function diff($src, $dest) |
12
|
|
|
{ |
13
|
1 |
|
return (new static)->makeDiff($src, $dest); |
14
|
|
|
} |
15
|
|
|
|
16
|
4 |
|
public function makeDiff($srcStruct, $desStruct, string $path = ''): array |
17
|
|
|
{ |
18
|
4 |
|
$changes = []; |
19
|
|
|
|
20
|
4 |
|
if (is_object($srcStruct)) { |
21
|
4 |
|
$changes = $this->handleObject($srcStruct, $desStruct, $path); |
22
|
4 |
|
} elseif (is_array($srcStruct)) { |
23
|
3 |
|
$changes = $this->handleArray($srcStruct, $desStruct, $path); |
24
|
3 |
|
} elseif ($srcStruct != $desStruct) { |
25
|
2 |
|
$changes[] = $this->makePatch(self::OP_REPLACE, $path, $desStruct); |
26
|
2 |
|
} |
27
|
|
|
|
28
|
4 |
|
return $changes; |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
protected function handleArray(array $srcStruct, array $desStruct, string $path): array |
32
|
|
|
{ |
33
|
1 |
|
$changes = []; |
34
|
|
|
|
35
|
1 |
|
if ($diff = $this->arrayDiff($desStruct, $srcStruct)) { |
36
|
1 |
|
foreach ($diff as $key => $val) { |
37
|
1 |
|
if (is_object($val)) { |
38
|
1 |
|
$changes = array_merge($changes, $this->makeDiff($srcStruct[$key], $val, $this->path($path, $key))); |
39
|
1 |
|
} else { |
40
|
1 |
|
$op = array_key_exists($key, $srcStruct) && !in_array($srcStruct[$key], $desStruct, true) |
41
|
1 |
|
? self::OP_REPLACE : self::OP_ADD; |
42
|
1 |
|
$changes[] = $this->makePatch($op, $this->path($path, $key), $val); |
43
|
|
|
} |
44
|
1 |
|
} |
45
|
1 |
|
} elseif ($srcStruct != $desStruct) { |
46
|
1 |
|
foreach ($srcStruct as $key => $val) { |
47
|
1 |
|
if (!in_array($val, $desStruct, true)) { |
48
|
1 |
|
$changes[] = $this->makePatch(self::OP_REMOVE, $this->path($path, $key)); |
49
|
1 |
|
} |
50
|
1 |
|
} |
51
|
1 |
|
} |
52
|
|
|
|
53
|
1 |
|
return $changes; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
View Code Duplication |
protected function handleObject(\stdClass $srcStruct, \stdClass $desStruct, string $path): array |
|
|
|
|
57
|
|
|
{ |
58
|
1 |
|
$changes = []; |
59
|
|
|
|
60
|
1 |
|
if ($this->shouldPartiallyReplace($srcStruct, $desStruct)) { |
61
|
1 |
|
foreach ($desStruct as $key => $val) { |
|
|
|
|
62
|
1 |
|
if (!property_exists($srcStruct, $key)) { |
63
|
1 |
|
$changes[] = $this->makePatch(self::OP_ADD, $this->path($path, $key), $val); |
64
|
1 |
|
} elseif ($srcStruct->$key != $val) { |
65
|
1 |
|
$changes = array_merge($changes, $this->makeDiff($srcStruct->$key, $val, $this->path($path, $key))); |
66
|
1 |
|
} |
67
|
1 |
|
} |
68
|
1 |
|
} elseif ($this->shouldPartiallyReplace($desStruct, $srcStruct)) { |
69
|
1 |
|
foreach ($srcStruct as $key => $val) { |
|
|
|
|
70
|
1 |
|
if (!property_exists($desStruct, $key)) { |
71
|
1 |
|
$changes[] = $this->makePatch(self::OP_REMOVE, $this->path($path, $key)); |
72
|
1 |
|
} |
73
|
1 |
|
} |
74
|
1 |
|
} |
75
|
|
|
|
76
|
1 |
|
return $changes; |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
protected function shouldPartiallyReplace(\stdClass $o1, \stdClass $o2): bool |
80
|
|
|
{ |
81
|
4 |
|
return count(array_diff_key((array) $o1, (array) $o2)) < count($o1); |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
protected function arrayDiff(array $a1, array $a2): array |
85
|
|
|
{ |
86
|
3 |
|
$result = []; |
87
|
|
|
|
88
|
3 |
|
foreach ($a1 as $key => $val) { |
89
|
3 |
|
if (!in_array($val, $a2, true)) { |
90
|
2 |
|
$result[$key] = $val; |
91
|
2 |
|
} |
92
|
3 |
|
} |
93
|
|
|
|
94
|
3 |
|
return $result; |
95
|
|
|
} |
96
|
|
|
|
97
|
4 |
|
protected function path(string $root, $path): string |
98
|
|
|
{ |
99
|
4 |
|
$path = (string) $path; |
100
|
1 |
|
|
101
|
1 |
|
if ($path === '_empty_') { |
102
|
|
|
$path = ''; |
103
|
4 |
|
} |
104
|
|
|
|
105
|
|
|
return rtrim($root, '/') . '/' . ltrim($path, '/'); |
106
|
4 |
|
} |
107
|
|
|
|
108
|
|
|
protected function makePatch(string $op, string $path, $val = null): array |
109
|
4 |
|
{ |
110
|
3 |
|
switch ($op) { |
111
|
4 |
|
default: |
112
|
3 |
|
return ['op' => $op, 'path' => $path, 'value' => $val]; |
113
|
3 |
|
case self::OP_REMOVE: |
114
|
|
|
return ['op' => $op, 'path' => $path]; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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.