Skip to content

Commit b5b0145

Browse files
committed
Pointer options refactoring
- OPTION_NON_NUMERIC_INDICES renamed. - OPTION_WRITE_ANY_INDEX introduced.
1 parent 17e16c0 commit b5b0145

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

src/Pointer.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@ class Pointer
1111
* have string indices. This option can be used to enable non-numeric
1212
* array indices addressing.
1313
*/
14-
const OPTION_NON_NUMERIC_ARRAY_INDICES = 0x01;
14+
const OPTION_NON_NUMERIC_INDICES = 0x01;
15+
/**
16+
* "Native" JSON arrays have consistent indices, so allowing to create
17+
* arbitrary numeric indices will break them. But PHP arrays support
18+
* arrays with "gaps" between indices, and this option enables writing
19+
* to array at any index.
20+
*
21+
* @todo Implement this option.
22+
*/
23+
const OPTION_WRITE_ANY_INDEX = 0x02;
1524

1625
/**
1726
* Bit-packed options.
@@ -134,7 +143,7 @@ public function unsetData()
134143
public function test()
135144
{
136145
return Pointer\Evaluate\Test::factory()
137-
->setNonNumericArrayIndices($this->hasOption(self::OPTION_NON_NUMERIC_ARRAY_INDICES))
146+
->setNonNumericIndices($this->hasOption(self::OPTION_NON_NUMERIC_INDICES))
138147
->setLocator($this->getLocator())
139148
->setData($this->getData())
140149
->perform()
@@ -144,7 +153,7 @@ public function test()
144153
public function &read()
145154
{
146155
return Pointer\Evaluate\Read::factory()
147-
->setNonNumericArrayIndices($this->hasOption(self::OPTION_NON_NUMERIC_ARRAY_INDICES))
156+
->setNonNumericIndices($this->hasOption(self::OPTION_NON_NUMERIC_INDICES))
148157
->setLocator($this->getLocator())
149158
->setData($this->getData())
150159
->perform()
@@ -155,7 +164,7 @@ public function &read()
155164
public function write($value)
156165
{
157166
Pointer\Evaluate\Write::factory()
158-
->setNonNumericArrayIndices($this->hasOption(self::OPTION_NON_NUMERIC_ARRAY_INDICES))
167+
->setNonNumericIndices($this->hasOption(self::OPTION_NON_NUMERIC_INDICES))
159168
->setLocator($this->getLocator())
160169
->setData($this->getData())
161170
->setValue($value)

src/Pointer/Evaluate.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ abstract class Evaluate
5555
*/
5656
protected $result;
5757

58-
protected $nonNumericArrayIndices = false;
58+
protected $nonNumericIndices = false;
5959

6060

6161
/**
@@ -176,12 +176,12 @@ public function &getResult()
176176
}
177177

178178

179-
public function setNonNumericArrayIndices($allow = true)
179+
public function setNonNumericIndices($allow = true)
180180
{
181181
if (!is_bool($allow)) {
182182
throw new Evaluate\InvalidArgumentException("Non-numeric array indices allowance flag must be boolean");
183183
}
184-
$this->nonNumericArrayIndices = $allow;
184+
$this->nonNumericIndices = $allow;
185185
return $this;
186186
}
187187

@@ -267,7 +267,7 @@ protected function processArrayIndex(Reference $reference)
267267
return $this->processNumericArrayIndex($reference);
268268

269269
default:
270-
return $this->nonNumericArrayIndices
270+
return $this->nonNumericIndices
271271
? $this->processAllowedNonNumericArrayIndex($reference)
272272
: $this->processNotAllowedNonNumericArrayIndex($reference);
273273
}

tests/PointerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function providerReadDataWithInvalidLocator()
175175
public function testTestDataWithNonNumericArrayIndices($text, $data)
176176
{
177177
$result = Pointer::factory()
178-
->setOptions(Pointer::OPTION_NON_NUMERIC_ARRAY_INDICES)
178+
->setOptions(Pointer::OPTION_NON_NUMERIC_INDICES)
179179
->setText($text)
180180
->setData($data)
181181
->test();
@@ -192,7 +192,7 @@ public function testTestDataWithNonNumericArrayIndices($text, $data)
192192
public function testReadDataWithNonNumericArrayIndices($text, $data, $result)
193193
{
194194
$pointer = Pointer::factory()
195-
->setOptions(Pointer::OPTION_NON_NUMERIC_ARRAY_INDICES)
195+
->setOptions(Pointer::OPTION_NON_NUMERIC_INDICES)
196196
->setText($text)
197197
->setData($data);
198198
$this->assertEquals($result, $pointer->read(), "Error reading non-numeric array index");

0 commit comments

Comments
 (0)