Skip to content

Commit 6ede306

Browse files
committed
Write evaluator refactored
1 parent 5fee633 commit 6ede306

12 files changed

+476
-39
lines changed

src/Pointer/Evaluate.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Remorhaz\JSONPointer\Locator;
66
use Remorhaz\JSONPointer\Locator\Reference;
7+
use Remorhaz\JSONPointer\Pointer\Evaluate\ReferenceEvaluate;
78

89
/**
910
* Abstract JSON Pointer evaluator.
@@ -216,16 +217,17 @@ public function perform()
216217
}
217218

218219

220+
/**
221+
* @param Reference $reference
222+
* @return ReferenceEvaluate
223+
*/
224+
abstract protected function createReferenceEvaluate(Reference $reference);
225+
226+
219227
protected function processReference(Reference $reference)
220228
{
221229
try {
222-
if (is_object($this->cursor)) {
223-
$this->processObjectProperty($reference);
224-
} elseif (is_array($this->cursor)) {
225-
$this->processArrayIndex($reference);
226-
} else {
227-
throw new Evaluate\EvaluateException("Accessing non-structured data with reference");
228-
}
230+
$this->processReferenceEvaluate($reference);
229231
} catch (Evaluate\EvaluateException $e) {
230232
throw new Evaluate\EvaluateException(
231233
"Error evaluating data for path '{$reference->getPath()}': {$e->getMessage()}",
@@ -237,6 +239,18 @@ protected function processReference(Reference $reference)
237239
}
238240

239241

242+
protected function processReferenceEvaluate(Reference $reference)
243+
{
244+
if (is_object($this->cursor)) {
245+
$this->processObjectProperty($reference);
246+
} elseif (is_array($this->cursor)) {
247+
$this->processArrayIndex($reference);
248+
} else {
249+
throw new Evaluate\EvaluateException("Accessing non-structured data with reference");
250+
}
251+
}
252+
253+
240254
abstract protected function processCursor();
241255

242256

src/Pointer/Evaluate/Read.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@ protected function processNonExistingArrayIndex(Reference $reference)
3030
{
3131
throw new EvaluateException("No index '{$reference->getValue()}' in array");
3232
}
33+
34+
35+
protected function createReferenceEvaluate(Reference $reference)
36+
{
37+
38+
}
3339
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
4+
5+
use Remorhaz\JSONPointer\Locator\Reference;
6+
7+
abstract class ReferenceEvaluate
8+
{
9+
10+
11+
/**
12+
* Evaluating reference.
13+
*
14+
* @var Reference|null
15+
*/
16+
protected $reference;
17+
18+
/**
19+
* Data at cursor.
20+
*
21+
* @var mixed
22+
*/
23+
protected $data;
24+
25+
protected $result;
26+
27+
protected $isResultSet;
28+
29+
/**
30+
* Flag of having data set.
31+
*
32+
* @var bool
33+
*/
34+
protected $isDataSet = false;
35+
36+
abstract protected function doesExist();
37+
38+
abstract protected function performExisting();
39+
40+
abstract protected function performNonExisting();
41+
42+
43+
/**
44+
* Constructor.
45+
*/
46+
protected function __construct()
47+
{
48+
}
49+
50+
51+
/**
52+
* Creates object instance.
53+
*
54+
* @return static
55+
*/
56+
public static function factory()
57+
{
58+
return new static();
59+
}
60+
61+
62+
/**
63+
* Sets reference for evaluation.
64+
*
65+
* @param Reference $reference
66+
* @return $this
67+
*/
68+
public function setReference(Reference $reference)
69+
{
70+
$this->reference = $reference;
71+
return $this;
72+
}
73+
74+
75+
/**
76+
* Returns reference for evaluation.
77+
*
78+
* @return Reference
79+
*/
80+
protected function getReference()
81+
{
82+
if (null === $this->reference) {
83+
throw new LogicException("Reference is not set in reference evaluator");
84+
}
85+
return $this->reference;
86+
}
87+
88+
89+
public function setData(&$data)
90+
{
91+
$this->data = &$data;
92+
$this->isDataSet = true;
93+
return $this;
94+
}
95+
96+
97+
public function &getData()
98+
{
99+
if (!$this->isDataSet) {
100+
throw new LogicException("Data is not set in reference evaluator");
101+
}
102+
return $this->data;
103+
}
104+
105+
106+
public function isResultSet()
107+
{
108+
return $this->isResultSet;
109+
}
110+
111+
112+
/**
113+
* Sets evaluation result.
114+
*
115+
* @param mixed $result
116+
* @return $this
117+
*/
118+
protected function setResult(&$result)
119+
{
120+
$this->result = &$result;
121+
$this->isResultSet = true;
122+
return $this;
123+
}
124+
125+
126+
/**
127+
* Returns evaluation result.
128+
*
129+
* @return mixed
130+
* @throws LogicException
131+
*/
132+
public function &getResult()
133+
{
134+
if (!$this->isResultSet()) {
135+
throw new LogicException("Evaluation result is not set");
136+
}
137+
return $this->result;
138+
}
139+
140+
141+
/**
142+
* Performs reference evaluation.
143+
*
144+
* @return $this
145+
*/
146+
public function perform()
147+
{
148+
return $this->doesExist()
149+
? $this->performExisting()
150+
: $this->performNonExisting();
151+
}
152+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
4+
5+
abstract class ReferenceWrite extends ReferenceEvaluate
6+
{
7+
8+
protected $value;
9+
10+
protected $isValueSet = false;
11+
12+
13+
public function setValue($value)
14+
{
15+
$this->value = $value;
16+
$this->isValueSet = true;
17+
return $this;
18+
}
19+
20+
21+
protected function getValue()
22+
{
23+
if (!$this->isValueSet) {
24+
throw new LogicException("Value is not set in evaluator");
25+
}
26+
return $this->value;
27+
}
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
4+
5+
class ReferenceWriteNextIndex extends ReferenceWrite
6+
{
7+
8+
9+
protected function doesExist()
10+
{
11+
return false;
12+
}
13+
14+
15+
protected function performExisting()
16+
{
17+
throw new LogicException("Next index never exists");
18+
}
19+
20+
21+
protected function performNonExisting()
22+
{
23+
if (!$this->canPerformNonExisting()) {
24+
throw new EvaluateException("Cannot write to non-existing next index of array if it is not last");
25+
}
26+
$this->data[] = $this->getValue();
27+
$result = null;
28+
return $this->setResult($result);
29+
}
30+
31+
32+
protected function canPerformNonExisting()
33+
{
34+
return $this
35+
->getReference()
36+
->isLast();
37+
}
38+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
4+
5+
class ReferenceWriteNonNumericIndex extends ReferenceWrite
6+
{
7+
8+
9+
protected function doesExist()
10+
{
11+
return array_key_exists($this->getIndex(), $this->getData());
12+
}
13+
14+
15+
protected function performExisting()
16+
{
17+
$this->data = &$this->data[$this->getIndex()];
18+
return $this;
19+
}
20+
21+
22+
protected function performNonExisting()
23+
{
24+
if (!$this->canPerformNonExisting()) {
25+
throw new EvaluateException(
26+
"Cannot write to non-existing index '{$this->getIndex()}'' in array"
27+
);
28+
}
29+
$this->data[$this->getIndex()] = $this->getValue();
30+
$result = null;
31+
return $this->setResult($result);
32+
}
33+
34+
35+
protected function canPerformNonExisting()
36+
{
37+
return $this
38+
->getReference()
39+
->isLast();
40+
}
41+
42+
43+
protected function getIndex()
44+
{
45+
return $this
46+
->getReference()
47+
->getValue();
48+
}
49+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
4+
5+
abstract class ReferenceWriteNumericIndex extends ReferenceWrite
6+
{
7+
8+
abstract protected function canPerformNonExisting();
9+
10+
11+
protected function doesExist()
12+
{
13+
return array_key_exists($this->getIndex(), $this->getData());
14+
}
15+
16+
17+
protected function performExisting()
18+
{
19+
$this->data = &$this->data[$this->getIndex()];
20+
return $this;
21+
}
22+
23+
24+
protected function performNonExisting()
25+
{
26+
$index = $this->getIndex();
27+
if (!$this->canPerformNonExisting()) {
28+
$indexText = is_int($index) ? "{$index}" : "'{$index}'";
29+
throw new EvaluateException("Cannot write to non-existing index {$indexText} in array");
30+
}
31+
$this->data[$index] = $this->getValue();
32+
$result = null;
33+
return $this->setResult($result);
34+
}
35+
36+
37+
protected function getIndex()
38+
{
39+
return (int) $this
40+
->getReference()
41+
->getValue();
42+
}
43+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
4+
5+
class ReferenceWriteNumericIndexWithGaps extends ReferenceWriteNumericIndex
6+
{
7+
8+
9+
protected function canPerformNonExisting()
10+
{
11+
return $this
12+
->getReference()
13+
->isLast();
14+
}
15+
}

0 commit comments

Comments
 (0)