Skip to content

Commit aca3e0c

Browse files
committed
Implement ArrayAccess interface
1 parent b5899e0 commit aca3e0c

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,21 @@ $data->set('hosts.april', [
105105
$hasKey = $data->has('hosts.dewey.username');
106106
```
107107

108+
`Data` may be used as an array, since it implements `ArrayAccess` interface:
109+
110+
```php
111+
// Get
112+
$data->get('name') === $data['name']; // true
113+
114+
$data['name'] = 'Dewey';
115+
// is equivalent to
116+
$data->set($name, 'Dewey');
117+
118+
isset($data['name']) === $data->has('name');
119+
120+
// Remove key
121+
unset($data['name']);
122+
```
108123

109124
License
110125
-------

src/Data.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
namespace Dflydev\DotAccessData;
1313

1414
use RuntimeException;
15+
use ArrayAccess;
1516

16-
class Data implements DataInterface
17+
class Data implements DataInterface, ArrayAccess
1718
{
1819
/**
1920
* Internal representation of data data
@@ -219,4 +220,37 @@ public function export()
219220
{
220221
return $this->data;
221222
}
223+
224+
/**
225+
* {@inheritdoc}
226+
*/
227+
public function offsetExists($key)
228+
{
229+
return $this->has($key);
230+
}
231+
232+
/**
233+
* {@inheritdoc}
234+
*/
235+
public function offsetGet($key)
236+
{
237+
return $this->get($key);
238+
}
239+
240+
/**
241+
* {@inheritdoc}
242+
*/
243+
public function offsetSet($key, $value)
244+
{
245+
$this->set($key, $value);
246+
}
247+
248+
/**
249+
* {@inheritdoc}
250+
*/
251+
public function offsetUnset($key)
252+
{
253+
$this->remove($key);
254+
}
255+
222256
}

tests/DataTest.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,89 @@ public function testExport()
205205

206206
$this->assertEquals($this->getSampleData(), $data->export());
207207
}
208+
209+
public function testOffsetExists()
210+
{
211+
$data = new Data($this->getSampleData());
212+
213+
foreach (
214+
['a', 'i', 'b.d', 'f.g.h', 'h.i', 'b.d.d1'] as $existentKey
215+
) {
216+
$this->assertTrue(isset($data[$existentKey]));
217+
}
218+
219+
foreach (
220+
['p', 'b.b1', 'b.c.C1', 'h.i.I', 'b.d.d1.D1'] as $notExistentKey
221+
) {
222+
$this->assertFalse(isset($data[$notExistentKey]));
223+
}
224+
}
225+
226+
public function testOffsetGet()
227+
{
228+
$wrappedData = new Data([
229+
'wrapped' => [
230+
'sampleData' => $this->getSampleData()
231+
],
232+
]);
233+
234+
$data = $wrappedData->getData('wrapped.sampleData');
235+
236+
$this->assertEquals('A', $data['a']);
237+
$this->assertEquals('B', $data['b.b']);
238+
$this->assertEquals(['C1', 'C2', 'C3'], $data['b.c']);
239+
$this->assertEquals('D3', $data['b.d.d3']);
240+
$this->assertEquals(['c1', 'c2', 'c3'], $data['c']);
241+
$this->assertNull($data['foo'], 'Foo should not exist');
242+
$this->assertNull($data['f.g.h.i']);
243+
244+
$this->expectException(RuntimeException::class);
245+
246+
$data = $wrappedData->getData('wrapped.sampleData.a');
247+
}
248+
249+
public function testOffsetSet()
250+
{
251+
$data = new Data;
252+
253+
$this->assertNull($data['a']);
254+
$this->assertNull($data['b.c']);
255+
$this->assertNull($data['d.e']);
256+
257+
$data['a'] = 'A';
258+
$data['b.c'] = 'C';
259+
$data['d.e'] = ['f' => 'F', 'g' => 'G'];
260+
261+
$this->assertEquals('A', $data['a']);
262+
$this->assertEquals(['c' => 'C'], $data['b']);
263+
$this->assertEquals('C', $data['b.c']);
264+
$this->assertEquals('F', $data['d.e.f']);
265+
$this->assertEquals(['e' => ['f' => 'F', 'g' => 'G']], $data['d']);
266+
267+
$this->expectException(RuntimeException::class);
268+
269+
$data->set('', 'broken');
270+
}
271+
272+
public function testOffsetUnset()
273+
{
274+
$data = new Data($this->getSampleData());
275+
276+
unset($data['a']);
277+
unset($data['b.c']);
278+
unset($data['b.d.d3']);
279+
unset($data['d']);
280+
unset($data['d.e.f']);
281+
unset($data['empty.path']);
282+
283+
$this->assertNull($data['a']);
284+
$this->assertNull($data['b.c']);
285+
$this->assertNull($data['b.d.d3']);
286+
$this->assertNull(null);
287+
$this->assertEquals('D2', $data['b.d.d2']);
288+
289+
$this->expectException(RuntimeException::class);
290+
291+
unset($data['']);
292+
}
208293
}

0 commit comments

Comments
 (0)