Skip to content

Commit 407e464

Browse files
committed
Complex read reference evaluators removed
1 parent 3c7cd74 commit 407e464

13 files changed

+69
-88
lines changed

src/Pointer/Evaluate/Advancer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ abstract public function advance();
4545
abstract public function write($data);
4646

4747
/**
48+
* @return $this
4849
* @throws EvaluateException
4950
*/
5051
abstract public function fail();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
4+
5+
abstract class AdvancerIndex extends Advancer
6+
{
7+
8+
9+
public function canAdvance()
10+
{
11+
return array_key_exists($this->getValue(), $this->getDataCursor());
12+
}
13+
14+
15+
public function advance()
16+
{
17+
return $this->setNewDataCursor($this->dataCursor[$this->getValue()]);
18+
}
19+
20+
21+
public function write($data)
22+
{
23+
$this->dataCursor[$this->getValue()] = $data;
24+
return $this;
25+
}
26+
27+
28+
public function fail()
29+
{
30+
throw new EvaluateException("Array index {$this->getValueDescription()} is not found");
31+
}
32+
}

src/Pointer/Evaluate/AdvancerNextIndex.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
44

5-
class AdvancerNextIndex extends Advancer
5+
class AdvancerNextIndex extends AdvancerIndex
66
{
77

88

@@ -25,15 +25,9 @@ public function write($data)
2525
}
2626

2727

28-
public function fail()
29-
{
30-
throw new EvaluateException("Index {$this->getValueDescription()} is not found");
31-
}
32-
33-
3428
public function getValue()
3529
{
36-
throw new LogicException("Next index cannot have advanceable value");
30+
throw new LogicException("Next index don't have advanceable key");
3731
}
3832

3933

src/Pointer/Evaluate/AdvancerNonNumericIndex.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,40 @@
22

33
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
44

5-
class AdvancerNonNumericIndex extends Advancer
5+
class AdvancerNonNumericIndex extends AdvancerIndex
66
{
77

88

9-
public function canAdvance()
9+
protected $isAllowed = false;
10+
11+
12+
public function allow()
1013
{
11-
return array_key_exists($this->getValue(), $this->getDataCursor());
14+
$this->isAllowed = true;
15+
return $this;
1216
}
1317

1418

15-
public function advance()
19+
public function forbid()
1620
{
17-
return $this->setNewDataCursor($this->dataCursor[$this->getValue()]);
21+
$this->isAllowed = false;
22+
return $this;
1823
}
1924

2025

21-
public function write($data)
26+
public function canAdvance()
2227
{
23-
$this->dataCursor[$this->getValue()] = $data;
24-
return $this;
28+
return $this->isAllowed && parent::canAdvance();
2529
}
2630

2731

2832
public function fail()
2933
{
30-
throw new EvaluateException("Index {$this->getValueDescription()} is not found");
34+
if ($this->isAllowed) {
35+
return parent::fail();
36+
}
37+
throw new EvaluateException(
38+
"Non-numeric array index {$this->getValueDescription()} is not allowed"
39+
);
3140
}
3241
}

src/Pointer/Evaluate/AdvancerNumericIndex.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 AdvancerNumericIndex extends AdvancerNonNumericIndex
5+
class AdvancerNumericIndex extends AdvancerIndex
66
{
77

88

src/Pointer/Evaluate/ReferenceReadProperty.php renamed to src/Pointer/Evaluate/ReferenceRead.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace Remorhaz\JSONPointer\Pointer\Evaluate;
44

5-
class ReferenceReadProperty extends ReferenceAdvanceable
5+
class ReferenceRead extends ReferenceAdvanceable
66
{
77

8-
98
protected function performNonExisting()
109
{
1110
$this

src/Pointer/Evaluate/ReferenceReadAllowedNonNumericIndex.php

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

src/Pointer/Evaluate/ReferenceReadFactory.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,38 @@ class ReferenceReadFactory extends ReferenceEvaluateFactory
88

99
protected function createProperty()
1010
{
11-
return ReferenceReadProperty::factory()
11+
return ReferenceRead::factory()
1212
->setAdvancer(AdvancerProperty::factory());
1313
}
1414

1515

1616
protected function createNextIndex()
1717
{
18-
return ReferenceReadNextIndex::factory();
18+
return ReferenceRead::factory()
19+
->setAdvancer(AdvancerNextIndex::factory());
1920
}
2021

2122

2223
protected function createNumericIndex()
2324
{
24-
return ReferenceReadNumericIndex::factory()
25+
return ReferenceRead::factory()
2526
->setAdvancer(AdvancerNumericIndex::factory());
2627
}
2728

2829

2930
protected function createAllowedNonNumericIndex()
3031
{
31-
return ReferenceReadAllowedNonNumericIndex::factory()
32-
->setAdvancer(AdvancerNonNumericIndex::factory());
32+
$advancer = AdvancerNonNumericIndex::factory()
33+
->allow();
34+
return ReferenceRead::factory()
35+
->setAdvancer($advancer);
3336
}
3437

3538

3639
protected function createNotAllowedNonNumericIndex()
3740
{
38-
return ReferenceReadNotAllowedNonNumericIndex::factory();
41+
return ReferenceRead::factory()
42+
->setAdvancer(AdvancerNonNumericIndex::factory());
3943
}
4044

4145

src/Pointer/Evaluate/ReferenceReadNextIndex.php

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

src/Pointer/Evaluate/ReferenceReadNotAllowedNonNumericIndex.php

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

0 commit comments

Comments
 (0)