JsonPatch::generate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 5
nc 2
nop 4
crap 2
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) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
92 6
            } catch (InvalidTargetDocumentJsonException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
93 6
            } catch (InvalidOperationException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
94 6
            } catch (FailedTestException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
95
            }
96 24
        }
97
98 24
        return json_decode($targetDocument);
99
    }
100
}
101