|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Remorhaz\JSONPointer\Pointer\Evaluate; |
| 4 | + |
| 5 | +use Remorhaz\JSONPointer\Locator\Reference; |
| 6 | + |
| 7 | +abstract class Advancer |
| 8 | +{ |
| 9 | + |
| 10 | + |
| 11 | + protected $dataCursor; |
| 12 | + |
| 13 | + protected $isDataCursorSet = false; |
| 14 | + |
| 15 | + protected $newDataCursor; |
| 16 | + |
| 17 | + protected $isNewDataCursorSet = false; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var Reference|null |
| 21 | + */ |
| 22 | + protected $reference; |
| 23 | + |
| 24 | + |
| 25 | + protected function __construct() |
| 26 | + { |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + /** |
| 31 | + * @return bool |
| 32 | + */ |
| 33 | + abstract public function canAdvance(); |
| 34 | + |
| 35 | + /** |
| 36 | + * @return $this |
| 37 | + */ |
| 38 | + abstract public function advance(); |
| 39 | + |
| 40 | + |
| 41 | + /** |
| 42 | + * @param mixed $data |
| 43 | + * @return $this |
| 44 | + */ |
| 45 | + abstract public function write($data); |
| 46 | + |
| 47 | + |
| 48 | + public static function factory() |
| 49 | + { |
| 50 | + return new static(); |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + public function setDataCursor(&$dataCursor) |
| 55 | + { |
| 56 | + $this->dataCursor = &$dataCursor; |
| 57 | + $this->isDataCursorSet = true; |
| 58 | + return $this; |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + protected function &getDataCursor() |
| 63 | + { |
| 64 | + if (!$this->isDataCursorSet) { |
| 65 | + throw new LogicException("Data cursor is not set in advancer object"); |
| 66 | + } |
| 67 | + return $this->dataCursor; |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + protected function setNewDataCursor(&$dataCursor) |
| 72 | + { |
| 73 | + $this->newDataCursor = &$dataCursor; |
| 74 | + $this->isNewDataCursorSet = true; |
| 75 | + return $this; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + public function &getNewDataCursor() |
| 80 | + { |
| 81 | + if (!$this->isNewDataCursorSet) { |
| 82 | + throw new LogicException("Data cursor is not set in advancer object"); |
| 83 | + } |
| 84 | + return $this->newDataCursor; |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + public function setReference(Reference $reference) |
| 89 | + { |
| 90 | + $this->reference = $reference; |
| 91 | + return $this; |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + protected function getReference() |
| 96 | + { |
| 97 | + if (null === $this->reference) { |
| 98 | + throw new LogicException("Reference is not set in advancer object"); |
| 99 | + } |
| 100 | + return $this->reference; |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + public function getValue() |
| 105 | + { |
| 106 | + return $this |
| 107 | + ->getReference() |
| 108 | + ->getValue(); |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + public function getValueDescription() |
| 113 | + { |
| 114 | + return "'{$this->getValue()}'"; |
| 115 | + } |
| 116 | +} |
0 commit comments