Skip to content

Commit 2af0617

Browse files
committed
Test evaluator refactored
1 parent 6ede306 commit 2af0617

10 files changed

+325
-32
lines changed

src/Pointer/Evaluate.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ abstract class Evaluate
5858

5959
protected $nonNumericIndices = false;
6060

61+
/**
62+
* @var ReferenceEvaluate|null
63+
*/
64+
protected $referenceEvaluate;
65+
6166

6267
/**
6368
* Constructor.
@@ -224,6 +229,28 @@ public function perform()
224229
abstract protected function createReferenceEvaluate(Reference $reference);
225230

226231

232+
protected function setupReferenceEvaluate(Reference $reference)
233+
{
234+
$this->referenceEvaluate = $this
235+
->createReferenceEvaluate($reference)
236+
->setReference($reference)
237+
->setData($this->cursor);
238+
return $this;
239+
}
240+
241+
242+
/**
243+
* @return ReferenceEvaluate
244+
*/
245+
protected function getReferenceEvaluate()
246+
{
247+
if (null === $this->referenceEvaluate) {
248+
throw new LogicException("Reference evaluator is not set in locator evaluator");
249+
}
250+
return $this->referenceEvaluate;
251+
}
252+
253+
227254
protected function processReference(Reference $reference)
228255
{
229256
try {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
4+
5+
class ReferenceTestAllowedNonNumericIndex 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+
$result = false;
25+
return $this->setResult($result);
26+
}
27+
28+
29+
protected function getIndex()
30+
{
31+
return $this
32+
->getReference()
33+
->getValue();
34+
}
35+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
4+
5+
class ReferenceTestNextIndex 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+
$result = false;
24+
return $this->setResult($result);
25+
}
26+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
4+
5+
class ReferenceTestNotAllowedNonNumericIndex 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+
$result = false;
26+
return $this->setResult($result);
27+
}
28+
29+
30+
protected function getIndex()
31+
{
32+
return $this
33+
->getReference()
34+
->getValue();
35+
}
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
4+
5+
class ReferenceTestNumericIndex 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+
$result = false;
25+
return $this->setResult($result);
26+
}
27+
28+
29+
protected function getIndex()
30+
{
31+
return (int) $this
32+
->getReference()
33+
->getValue();
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
4+
5+
class ReferenceTestProperty 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+
$result = false;
25+
return $this->setResult($result);
26+
}
27+
28+
29+
protected function getProperty()
30+
{
31+
return $this
32+
->getReference()
33+
->getValue();
34+
}
35+
}

src/Pointer/Evaluate/ReferenceWriteNonNumericIndex.php renamed to src/Pointer/Evaluate/ReferenceWriteAllowedNonNumericIndex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
44

5-
class ReferenceWriteNonNumericIndex extends ReferenceWrite
5+
class ReferenceWriteAllowedNonNumericIndex extends ReferenceWrite
66
{
77

88

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 ReferenceWriteNotAllowedNonNumericIndex extends ReferenceWrite
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 write to 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+
}

src/Pointer/Evaluate/Test.php

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,78 @@ protected function processCursor()
1717

1818
protected function processNextArrayIndex(Reference $reference)
1919
{
20-
$result = false;
21-
return $this->setResult($result);
20+
throw new LogicException("Deprecated method");
2221
}
2322

2423

2524
protected function processNonExistingObjectProperty(Reference $reference)
2625
{
27-
$result = false;
28-
return $this->setResult($result);
26+
throw new LogicException("Deprecated method");
2927
}
3028

3129

3230
protected function processNonExistingArrayIndex(Reference $reference)
3331
{
34-
$result = false;
35-
return $this->setResult($result);
32+
throw new LogicException("Deprecated method");
3633
}
3734

3835

3936
protected function processNotAllowedNonNumericArrayIndex(Reference $reference)
4037
{
41-
$result = false;
42-
return $this->setResult($result);
38+
throw new LogicException("Deprecated method");
4339
}
44-
45-
40+
41+
42+
/**
43+
* @param Reference $reference
44+
* @return ReferenceEvaluate
45+
*/
4646
protected function createReferenceEvaluate(Reference $reference)
4747
{
48-
48+
if (is_object($this->cursor)) {
49+
return ReferenceTestProperty::factory();
50+
}
51+
if (is_array($this->cursor)) {
52+
switch ($reference->getType()) {
53+
case $reference::TYPE_NEXT_INDEX:
54+
return ReferenceTestNextIndex::factory();
55+
56+
case $reference::TYPE_INDEX:
57+
return ReferenceTestNumericIndex::factory();
58+
59+
case $reference::TYPE_PROPERTY:
60+
return $this->nonNumericIndices
61+
? ReferenceTestAllowedNonNumericIndex::factory()
62+
: ReferenceTestNotAllowedNonNumericIndex::factory();
63+
64+
default:
65+
throw new DomainException(
66+
"Failed to create test evaluator for reference of type {$reference->getType()}"
67+
);
68+
}
69+
}
70+
throw new EvaluateException("Cannot test non-structured data by reference");
71+
}
72+
73+
74+
protected function processReferenceEvaluate(Reference $reference)
75+
{
76+
$this
77+
->setupReferenceEvaluate($reference)
78+
->getReferenceEvaluate()
79+
->perform();
80+
$this->cursor = &$this
81+
->getReferenceEvaluate()
82+
->getData();
83+
$isReferenceResultSet = $this
84+
->getReferenceEvaluate()
85+
->isResultSet();
86+
if ($isReferenceResultSet) {
87+
$referenceResult = &$this
88+
->getReferenceEvaluate()
89+
->getResult();
90+
$this->setResult($referenceResult);
91+
}
92+
return $this;
4993
}
5094
}

0 commit comments

Comments
 (0)