|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: arnaud |
|
5
|
|
|
* Date: 01/11/15 |
|
6
|
|
|
* Time: 12:10 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Ndrx\Profiler; |
|
10
|
|
|
|
|
11
|
|
|
use Rs\Json\Patch; |
|
12
|
|
|
use Rs\Json\Patch\FailedTestException; |
|
13
|
|
|
use Rs\Json\Patch\InvalidOperationException; |
|
14
|
|
|
use Rs\Json\Patch\InvalidPatchDocumentJsonException; |
|
15
|
|
|
use Rs\Json\Patch\InvalidTargetDocumentJsonException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class JsonPatch |
|
19
|
|
|
* @package Ndrx\Profiler |
|
20
|
|
|
*/ |
|
21
|
|
|
class JsonPatch |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Action are based on http://jsonpatch.com/ description |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
const ACTION_ADD = 'add'; |
|
28
|
|
|
const ACTION_REMOVE = 'remove'; |
|
29
|
|
|
const ACTION_REPLACE = 'replace'; |
|
30
|
|
|
const ACTION_COPY = 'copy'; |
|
31
|
|
|
const ACTION_MOVE = 'move'; |
|
32
|
|
|
const ACTION_TEST = 'test'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param $path |
|
36
|
|
|
* @param $action |
|
37
|
|
|
* @param array|string|boolean $value |
|
38
|
|
|
* @param $append |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
164 |
|
public function generate($path, $action, $value = false, $append = false) |
|
42
|
|
|
{ |
|
43
|
2 |
|
return [ |
|
44
|
164 |
|
'op' => $action, |
|
45
|
164 |
|
'path' => '/' . $path . ($append ? '/-' : ''), |
|
46
|
|
|
'value' => $value |
|
47
|
164 |
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param $path |
|
52
|
|
|
* @param $items |
|
53
|
|
|
* @param bool|false $append |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
116 |
|
public function generateArrayAdd($path, $items, $append = false) |
|
57
|
|
|
{ |
|
58
|
116 |
|
$patches = []; |
|
59
|
116 |
|
$rootParentCreated = false; |
|
60
|
116 |
|
foreach ($items as $key => $element) { |
|
61
|
116 |
|
if (!$rootParentCreated) { |
|
62
|
116 |
|
$patches[] = $this->generate($path, JsonPatch::ACTION_ADD, [], false); |
|
63
|
116 |
|
$rootParentCreated = true; |
|
64
|
116 |
|
} |
|
65
|
|
|
|
|
66
|
116 |
|
$elementPath = $path; |
|
67
|
116 |
|
if (!$append && is_string($key)) { |
|
68
|
16 |
|
$elementPath .= '/' . $key; |
|
69
|
16 |
|
} |
|
70
|
|
|
|
|
71
|
116 |
|
$patches[] = $this->generate($elementPath, JsonPatch::ACTION_ADD, $element, $append); |
|
72
|
116 |
|
} |
|
73
|
|
|
|
|
74
|
116 |
|
return $patches; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @author LAHAXE Arnaud |
|
79
|
|
|
* |
|
80
|
|
|
* @param \Generator $patchs |
|
81
|
|
|
* @param string $targetDocument |
|
82
|
|
|
* |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
*/ |
|
85
|
24 |
|
public function compile($patchs, $targetDocument = '{}') |
|
86
|
|
|
{ |
|
87
|
24 |
|
foreach ($patchs as $patch) { |
|
88
|
|
|
try { |
|
89
|
20 |
|
$patch = new Patch($targetDocument, $patch); |
|
90
|
14 |
|
$targetDocument = $patch->apply(); |
|
91
|
20 |
|
} catch (InvalidPatchDocumentJsonException $e) { |
|
|
|
|
|
|
92
|
6 |
|
} catch (InvalidTargetDocumentJsonException $e) { |
|
|
|
|
|
|
93
|
6 |
|
} catch (InvalidOperationException $e) { |
|
|
|
|
|
|
94
|
6 |
|
} catch (FailedTestException $e) { |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
24 |
|
} |
|
97
|
|
|
|
|
98
|
24 |
|
return json_decode($targetDocument); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|