Completed
Push — master ( 81a403...8230d8 )
by Viacheslav
10s
created

JsonPatch::setFlags()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Swaggest\JsonDiff;
4
5
use Swaggest\JsonDiff\JsonPatch\Add;
6
use Swaggest\JsonDiff\JsonPatch\Copy;
7
use Swaggest\JsonDiff\JsonPatch\Move;
8
use Swaggest\JsonDiff\JsonPatch\OpPath;
9
use Swaggest\JsonDiff\JsonPatch\OpPathFrom;
10
use Swaggest\JsonDiff\JsonPatch\OpPathValue;
11
use Swaggest\JsonDiff\JsonPatch\Remove;
12
use Swaggest\JsonDiff\JsonPatch\Replace;
13
use Swaggest\JsonDiff\JsonPatch\Test;
14
15
/**
16
 * JSON Patch is specified in [RFC 6902](http://tools.ietf.org/html/rfc6902) from the IETF.
17
 *
18
 * Class JsonPatch
19
 */
20
class JsonPatch implements \JsonSerializable
21
{
22
    /**
23
     * Disallow converting empty array to object for key creation
24
     * @see JsonPointer::STRICT_MODE
25
     */
26
    const STRICT_MODE = 2;
27
28
    private $flags = 0;
29
30
    /**
31
     * @param int $options
32
     * @return $this
33
     */
34 1
    public function setFlags($options)
35
    {
36 1
        $this->flags = $options;
37 1
        return $this;
38
    }
39
40
    /** @var OpPath[] */
41
    private $operations = array();
42
43
    /**
44
     * @param array $data
45
     * @return JsonPatch
46
     * @throws Exception
47
     */
48 107
    public static function import(array $data)
49
    {
50 107
        $result = new JsonPatch();
51 107
        foreach ($data as $operation) {
52
            /** @var OpPath|OpPathValue|OpPathFrom $operation */
53 101
            if (is_array($operation)) {
54 3
                $operation = (object)$operation;
55
            }
56
57 101
            if (!isset($operation->op)) {
58 1
                throw new Exception('Missing "op" in operation data');
59
            }
60 100
            if (!isset($operation->path)) {
61 1
                throw new Exception('Missing "path" in operation data');
62
            }
63
64 99
            $op = null;
65 99
            switch ($operation->op) {
66 99
                case Add::OP:
67 39
                    $op = new Add();
68 39
                    break;
69 63
                case Copy::OP:
70 6
                    $op = new Copy();
71 6
                    break;
72 57
                case Move::OP:
73 8
                    $op = new Move();
74 8
                    break;
75 49
                case Remove::OP:
76 17
                    $op = new Remove();
77 17
                    break;
78 35
                case Replace::OP:
79 18
                    $op = new Replace();
80 18
                    break;
81 19
                case Test::OP:
82 17
                    $op = new Test();
83 17
                    break;
84
                default:
85 2
                    throw new Exception('Unknown "op": ' . $operation->op);
86
            }
87 97
            $op->path = $operation->path;
88 97
            if ($op instanceof OpPathValue) {
89 70
                if (!array_key_exists('value', (array)$operation)) {
90 5
                    throw new Exception('Missing "value" in operation data');
91
                }
92 65
                $op->value = $operation->value;
0 ignored issues
show
Bug introduced by
The property value does not seem to exist on Swaggest\JsonDiff\JsonPatch\OpPathFrom.
Loading history...
93 31
            } elseif ($op instanceof OpPathFrom) {
94 14
                if (!isset($operation->from)) {
95 3
                    throw new Exception('Missing "from" in operation data');
96
                }
97 11
                $op->from = $operation->from;
0 ignored issues
show
Bug introduced by
The property from does not seem to exist on Swaggest\JsonDiff\JsonPatch\OpPathValue.
Loading history...
98
            }
99 89
            $result->operations[] = $op;
100
        }
101 95
        return $result;
102
    }
103
104 5
    public static function export(JsonPatch $patch)
105
    {
106 5
        $result = array();
107 5
        foreach ($patch->operations as $operation) {
108 5
            $result[] = (object)(array)$operation;
109
        }
110
111 5
        return $result;
112
    }
113
114 13
    public function op(OpPath $op)
115
    {
116 13
        $this->operations[] = $op;
117 13
        return $this;
118
    }
119
120 4
    public function jsonSerialize()
121
    {
122 4
        return self::export($this);
123
    }
124
125
    /**
126
     * @param mixed $original
127
     * @param bool $stopOnError
128
     * @return Exception[] array of errors
129
     * @throws Exception
130
     */
131 99
    public function apply(&$original, $stopOnError = true)
132
    {
133 99
        $errors = array();
134 99
        foreach ($this->operations as $operation) {
135
            try {
136 93
                $pathItems = JsonPointer::splitPath($operation->path);
137
                switch (true) {
138 93
                    case $operation instanceof Add:
139 39
                        JsonPointer::add($original, $pathItems, $operation->value, $this->flags);
140 31
                        break;
141 56
                    case $operation instanceof Copy:
142 5
                        $fromItems = JsonPointer::splitPath($operation->from);
143 5
                        $value = JsonPointer::get($original, $fromItems);
144 4
                        JsonPointer::add($original, $pathItems, $value, $this->flags);
145 4
                        break;
146 52
                    case $operation instanceof Move:
147 7
                        $fromItems = JsonPointer::splitPath($operation->from);
148 7
                        $value = JsonPointer::get($original, $fromItems);
149 6
                        JsonPointer::remove($original, $fromItems);
150 6
                        JsonPointer::add($original, $pathItems, $value, $this->flags);
151 6
                        break;
152 45
                    case $operation instanceof Remove:
153 17
                        JsonPointer::remove($original, $pathItems);
154 13
                        break;
155 31
                    case $operation instanceof Replace:
156 17
                        JsonPointer::get($original, $pathItems);
157 15
                        JsonPointer::remove($original, $pathItems);
158 15
                        JsonPointer::add($original, $pathItems, $operation->value, $this->flags);
159 15
                        break;
160 17
                    case $operation instanceof Test:
161 17
                        $value = JsonPointer::get($original, $pathItems);
162 14
                        $diff = new JsonDiff($operation->value, $value,
163 14
                            JsonDiff::STOP_ON_DIFF);
164 14
                        if ($diff->getDiffCnt() !== 0) {
165 3
                            throw new Exception('Test operation ' . json_encode($operation, JSON_UNESCAPED_SLASHES)
166 3
                                . ' failed: ' . json_encode($value));
167
                        }
168 72
                        break;
169
                }
170 23
            } catch (Exception $exception) {
171 23
                if ($stopOnError) {
172 21
                    throw $exception;
173
                } else {
174 73
                    $errors[] = $exception;
175
                }
176
            }
177
        }
178 79
        return $errors;
179
    }
180
}