1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Images\v2; |
4
|
|
|
|
5
|
|
|
class JsonPatch extends \OpenStack\Common\JsonSchema\JsonPatch |
6
|
|
|
{ |
7
|
1 |
|
public function disableRestrictedPropRemovals(array $diff, array $restrictedProps): array |
8
|
|
|
{ |
9
|
1 |
|
foreach ($diff as $i => $changeSet) { |
10
|
1 |
|
if ($changeSet['op'] == 'remove' && in_array($changeSet['path'], $restrictedProps)) { |
11
|
1 |
|
unset($diff[$i]); |
12
|
1 |
|
} |
13
|
1 |
|
} |
14
|
|
|
|
15
|
1 |
|
return $diff; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@inheritDoc} |
20
|
|
|
* |
21
|
|
|
* We need to override the proper way to handle objects because Glance v2 does not |
22
|
|
|
* support whole document replacement with empty JSON pointers. |
23
|
|
|
*/ |
24
|
3 |
View Code Duplication |
protected function handleObject(\stdClass $srcStruct, \stdClass $desStruct, string $path): array |
|
|
|
|
25
|
|
|
{ |
26
|
3 |
|
$changes = []; |
27
|
|
|
|
28
|
3 |
|
foreach ($desStruct as $key => $val) { |
|
|
|
|
29
|
3 |
|
if (!property_exists($srcStruct, $key)) { |
30
|
1 |
|
$changes[] = $this->makePatch(self::OP_ADD, $this->path($path, $key), $val); |
31
|
3 |
|
} elseif ($srcStruct->$key != $val) { |
32
|
2 |
|
$changes = array_merge($changes, $this->makeDiff($srcStruct->$key, $val, $this->path($path, $key))); |
33
|
2 |
|
} |
34
|
3 |
|
} |
35
|
|
|
|
36
|
3 |
|
if ($this->shouldPartiallyReplace($desStruct, $srcStruct)) { |
37
|
2 |
|
foreach ($srcStruct as $key => $val) { |
|
|
|
|
38
|
2 |
|
if (!property_exists($desStruct, $key)) { |
39
|
1 |
|
$changes[] = $this->makePatch(self::OP_REMOVE, $this->path($path, $key)); |
40
|
1 |
|
} |
41
|
2 |
|
} |
42
|
2 |
|
} |
43
|
|
|
|
44
|
3 |
|
return $changes; |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
protected function handleArray(array $srcStruct, array $desStruct, string $path): array |
48
|
|
|
{ |
49
|
2 |
|
$changes = []; |
50
|
|
|
|
51
|
2 |
|
if ($srcStruct != $desStruct) { |
52
|
2 |
|
if ($diff = $this->arrayDiff($desStruct, $srcStruct)) { |
|
|
|
|
53
|
1 |
|
$changes[] = $this->makePatch(self::OP_REPLACE, $path, $desStruct); |
54
|
1 |
|
} |
55
|
2 |
|
foreach ($srcStruct as $key => $val) { |
56
|
1 |
|
if (!in_array($val, $desStruct, true)) { |
57
|
1 |
|
$changes[] = $this->makePatch(self::OP_REMOVE, $this->path($path, $key)); |
58
|
1 |
|
} |
59
|
2 |
|
} |
60
|
2 |
|
} |
61
|
|
|
|
62
|
2 |
|
return $changes; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
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.