Skip to content

Commit f04efb6

Browse files
committed
Read evaluator refactored
1 parent 2af0617 commit f04efb6

File tree

7 files changed

+212
-5
lines changed

7 files changed

+212
-5
lines changed

src/Pointer/Evaluate/Read.php

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,68 @@ protected function processCursor()
1616

1717
protected function processNextArrayIndex(Reference $reference)
1818
{
19-
throw new EvaluateException("Index '{$reference->getValue()}' in array is write-only");
19+
throw new LogicException("Deprecated method");
2020
}
2121

2222

2323
protected function processNonExistingObjectProperty(Reference $reference)
2424
{
25-
throw new EvaluateException("No property '{$reference->getValue()}' in object");
25+
throw new LogicException("Deprecated method");
2626
}
2727

2828

2929
protected function processNonExistingArrayIndex(Reference $reference)
3030
{
31-
throw new EvaluateException("No index '{$reference->getValue()}' in array");
31+
throw new LogicException("Deprecated method");
3232
}
3333

3434

3535
protected function createReferenceEvaluate(Reference $reference)
3636
{
37+
if (is_object($this->cursor)) {
38+
return ReferenceReadProperty::factory();
39+
}
40+
if (is_array($this->cursor)) {
41+
switch ($reference->getType()) {
42+
case $reference::TYPE_NEXT_INDEX:
43+
return ReferenceReadNextIndex::factory();
3744

45+
case $reference::TYPE_INDEX:
46+
return ReferenceReadNumericIndex::factory();
47+
48+
case $reference::TYPE_PROPERTY:
49+
return $this->nonNumericIndices
50+
? ReferenceReadAllowedNonNumericIndex::factory()
51+
: ReferenceReadNotAllowedNonNumericIndex::factory();
52+
53+
default:
54+
throw new DomainException(
55+
"Failed to create read evaluator for reference of type {$reference->getType()}"
56+
);
57+
}
58+
}
59+
throw new EvaluateException("Cannot read non-structured data by reference");
60+
}
61+
62+
63+
protected function processReferenceEvaluate(Reference $reference)
64+
{
65+
$this
66+
->setupReferenceEvaluate($reference)
67+
->getReferenceEvaluate()
68+
->perform();
69+
$this->cursor = &$this
70+
->getReferenceEvaluate()
71+
->getData();
72+
$isReferenceResultSet = $this
73+
->getReferenceEvaluate()
74+
->isResultSet();
75+
if ($isReferenceResultSet) {
76+
$referenceResult = &$this
77+
->getReferenceEvaluate()
78+
->getResult();
79+
$this->setResult($referenceResult);
80+
}
81+
return $this;
3882
}
3983
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
4+
5+
class ReferenceReadAllowedNonNumericIndex extends ReferenceEvaluate
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+
throw new EvaluateException("No index '{$this->getIndex()}' in array");
25+
}
26+
27+
28+
protected function getIndex()
29+
{
30+
return $this
31+
->getReference()
32+
->getValue();
33+
}
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
4+
5+
class ReferenceReadNextIndex extends ReferenceEvaluate
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+
throw new EvaluateException("Next index in array is write-only");
24+
}
25+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
4+
5+
class ReferenceReadNotAllowedNonNumericIndex extends ReferenceEvaluate
6+
{
7+
8+
9+
protected function doesExist()
10+
{
11+
return false; // We don't allow it to exist :)
12+
}
13+
14+
15+
protected function performExisting()
16+
{
17+
throw new LogicException(
18+
"Non-numeric index '{$this->getIndex()}' is not allowed to exist"
19+
);
20+
}
21+
22+
23+
protected function performNonExisting()
24+
{
25+
throw new EvaluateException(
26+
"Cannot read non-numeric index '{$this->getIndex()}' in array"
27+
);
28+
}
29+
30+
31+
protected function getIndex()
32+
{
33+
return $this
34+
->getReference()
35+
->getValue();
36+
}
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
4+
5+
class ReferenceReadNumericIndex extends ReferenceEvaluate
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+
throw new EvaluateException("No index {$this->getIndex()} in array");
25+
}
26+
27+
28+
protected function getIndex()
29+
{
30+
return (int) $this
31+
->getReference()
32+
->getValue();
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
4+
5+
class ReferenceReadProperty extends ReferenceEvaluate
6+
{
7+
8+
9+
protected function doesExist()
10+
{
11+
return property_exists($this->getData(), $this->getProperty());
12+
}
13+
14+
15+
protected function performExisting()
16+
{
17+
$this->data = &$this->data->{$this->getProperty()};
18+
return $this;
19+
}
20+
21+
22+
protected function performNonExisting()
23+
{
24+
throw new EvaluateException("No property '{$this->getProperty()}' in object");
25+
}
26+
27+
28+
protected function getProperty()
29+
{
30+
return $this
31+
->getReference()
32+
->getValue();
33+
}
34+
}

src/Pointer/Evaluate/Write.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ protected function processNonExistingArrayIndex(Reference $reference)
8282
protected function createReferenceEvaluate(Reference $reference)
8383
{
8484
if (is_object($this->cursor)) {
85-
return ReferenceWriteProperty::factory()
86-
->setValue($this->getValue());
85+
return ReferenceWriteProperty::factory();
8786
}
8887
if (is_array($this->cursor)) {
8988
switch ($reference->getType()) {

0 commit comments

Comments
 (0)