Skip to content

Commit 502aee6

Browse files
committed
Advancer value renamed to key, reference classes reduced
1 parent 53f721c commit 502aee6

12 files changed

+78
-97
lines changed

src/Pointer/Evaluate/Advancer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function canWrite()
119119
}
120120

121121

122-
public function getValue()
122+
public function getKey()
123123
{
124124
return $this
125125
->getCursor()
@@ -128,8 +128,8 @@ public function getValue()
128128
}
129129

130130

131-
public function getValueDescription()
131+
public function getKeyDescription()
132132
{
133-
return "'{$this->getValue()}'";
133+
return "'{$this->getKey()}'";
134134
}
135135
}

src/Pointer/Evaluate/AdvancerIndex.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ abstract class AdvancerIndex extends Advancer
88

99
public function canAdvance()
1010
{
11-
return array_key_exists($this->getValue(), $this->getCursor()->getData());
11+
return array_key_exists($this->getKey(), $this->getCursor()->getData());
1212
}
1313

1414

1515
public function advance()
1616
{
1717
$data = &$this
1818
->getCursor()
19-
->getData()[$this->getValue()];
19+
->getData()[$this->getKey()];
2020
$this
2121
->getCursor()
2222
->setData($data);
@@ -26,13 +26,13 @@ public function advance()
2626

2727
public function write($data)
2828
{
29-
$this->getCursor()->getData()[$this->getValue()] = $data;
29+
$this->getCursor()->getData()[$this->getKey()] = $data;
3030
return $this;
3131
}
3232

3333

3434
public function fail()
3535
{
36-
throw new EvaluateException("Array index {$this->getValueDescription()} is not found");
36+
throw new EvaluateException("Array index {$this->getKeyDescription()} is not found");
3737
}
3838
}

src/Pointer/Evaluate/AdvancerNextIndex.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ public function write($data)
2727
}
2828

2929

30-
public function getValue()
30+
public function getKey()
3131
{
3232
throw new LogicException("Next index don't have advanceable key");
3333
}
3434

3535

36-
public function getValueDescription()
36+
public function getKeyDescription()
3737
{
3838
return "'-'";
3939
}

src/Pointer/Evaluate/AdvancerNonNumericIndex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function fail()
4141
return parent::fail();
4242
}
4343
throw new EvaluateException(
44-
"Non-numeric array index {$this->getValueDescription()} is not allowed"
44+
"Non-numeric array index {$this->getKeyDescription()} is not allowed"
4545
);
4646
}
4747
}

src/Pointer/Evaluate/AdvancerNumericIndex.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ public function forbidGaps()
2222
}
2323

2424

25-
public function getValue()
25+
public function getKey()
2626
{
27-
return (int) parent::getValue();
27+
return (int) parent::getKey();
2828
}
2929

3030

31-
public function getValueDescription()
31+
public function getKeyDescription()
3232
{
33-
return "{$this->getValue()}";
33+
return "{$this->getKey()}";
3434
}
3535

3636

@@ -43,7 +43,7 @@ public function canWrite()
4343
return true;
4444
}
4545
return
46-
0 == $this->getValue() && empty($this->getCursor()->getData()) ||
47-
array_key_exists($this->getValue() - 1, $this->getCursor()->getData());
46+
0 == $this->getKey() && empty($this->getCursor()->getData()) ||
47+
array_key_exists($this->getKey() - 1, $this->getCursor()->getData());
4848
}
4949
}

src/Pointer/Evaluate/AdvancerProperty.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class AdvancerProperty extends Advancer
88

99
public function canAdvance()
1010
{
11-
return property_exists($this->getCursor()->getData(), $this->getValue());
11+
return property_exists($this->getCursor()->getData(), $this->getKey());
1212
}
1313

1414

@@ -17,7 +17,7 @@ public function advance()
1717
$data = &$this
1818
->getCursor()
1919
->getData()
20-
->{$this->getValue()};
20+
->{$this->getKey()};
2121
$this
2222
->getCursor()
2323
->setData($data);
@@ -30,13 +30,13 @@ public function write($data)
3030
$this
3131
->getCursor()
3232
->getData()
33-
->{$this->getValue()} = $data;
33+
->{$this->getKey()} = $data;
3434
return $this;
3535
}
3636

3737

3838
public function fail()
3939
{
40-
throw new EvaluateException("Property {$this->getValueDescription()} is not found");
40+
throw new EvaluateException("Property {$this->getKeyDescription()} is not found");
4141
}
4242
}

src/Pointer/Evaluate/LocatorEvaluate.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ abstract class LocatorEvaluate
5656
protected $result;
5757

5858
/**
59-
* @var ReferenceAdvanceable|null
59+
* @var ReferenceEvaluate|null
6060
*/
6161
protected $referenceEvaluate;
6262

@@ -274,13 +274,13 @@ protected function createAdvancerForCursor()
274274

275275

276276
/**
277-
* @return ReferenceAdvanceable
277+
* @return ReferenceEvaluate
278278
*/
279279
abstract protected function createReferenceEvaluate();
280280

281281

282282
/**
283-
* @return ReferenceAdvanceable
283+
* @return ReferenceEvaluate
284284
* @throws LogicException
285285
*/
286286
protected function getReferenceEvaluate()

src/Pointer/Evaluate/ReferenceAdvanceable.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/Pointer/Evaluate/ReferenceEvaluate.php

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,19 @@
22

33
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
44

5-
use Remorhaz\JSONPointer\Locator\Reference;
6-
75
abstract class ReferenceEvaluate
86
{
97

8+
/**
9+
* @var Advancer|null
10+
*/
11+
protected $advancer;
12+
1013
protected $result;
1114

1215
protected $isResultSet;
1316

1417

15-
/**
16-
* Performs reference evaluation.
17-
*
18-
* @return $this
19-
*/
20-
abstract public function perform();
21-
22-
2318
/**
2419
* Constructor.
2520
*/
@@ -39,25 +34,51 @@ public static function factory()
3934
}
4035

4136

42-
public function isResultSet()
37+
abstract protected function performNonExisting();
38+
39+
40+
/**
41+
* @return Advancer
42+
*/
43+
protected function getAdvancer()
4344
{
44-
return $this->isResultSet;
45+
if (null === $this->advancer) {
46+
throw new LogicException("Advancer is not set in reference evaluator");
47+
}
48+
return $this->advancer;
49+
}
50+
51+
52+
public function setAdvancer(Advancer $advancer)
53+
{
54+
$this->advancer = $advancer;
55+
return $this;
4556
}
4657

4758

4859
/**
49-
* Sets evaluation result.
60+
* Performs reference evaluation.
5061
*
51-
* @param mixed $result
5262
* @return $this
5363
*/
54-
protected function setResult(&$result)
64+
public function perform()
5565
{
56-
$this->result = &$result;
57-
$this->isResultSet = true;
58-
return $this;
66+
$canAdvance = $this
67+
->getAdvancer()
68+
->canAdvance();
69+
if ($canAdvance) {
70+
$this
71+
->getAdvancer()
72+
->advance();
73+
return $this;
74+
}
75+
return $this->performNonExisting();
5976
}
6077

78+
public function isResultSet()
79+
{
80+
return $this->isResultSet;
81+
}
6182

6283
/**
6384
* Returns evaluation result.
@@ -72,4 +93,17 @@ public function &getResult()
7293
}
7394
return $this->result;
7495
}
96+
97+
/**
98+
* Sets evaluation result.
99+
*
100+
* @param mixed $result
101+
* @return ReferenceEvaluate
102+
*/
103+
protected function setResult(&$result)
104+
{
105+
$this->result = &$result;
106+
$this->isResultSet = true;
107+
return $this;
108+
}
75109
}

src/Pointer/Evaluate/ReferenceRead.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 ReferenceRead extends ReferenceAdvanceable
5+
class ReferenceRead extends ReferenceEvaluate
66
{
77

88
protected function performNonExisting()

0 commit comments

Comments
 (0)