Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ArrayAccess interface #17

Merged
merged 1 commit into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ $data->set('hosts.april', [
$hasKey = $data->has('hosts.dewey.username');
```

`Data` may be used as an array, since it implements `ArrayAccess` interface:

```php
// Get
$data->get('name') === $data['name']; // true

$data['name'] = 'Dewey';
// is equivalent to
$data->set($name, 'Dewey');

isset($data['name']) === $data->has('name');

// Remove key
unset($data['name']);
```

License
-------
Expand Down
36 changes: 35 additions & 1 deletion src/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
namespace Dflydev\DotAccessData;

use RuntimeException;
use ArrayAccess;

class Data implements DataInterface
class Data implements DataInterface, ArrayAccess
{
/**
* Internal representation of data data
Expand Down Expand Up @@ -219,4 +220,37 @@ public function export()
{
return $this->data;
}

/**
* {@inheritdoc}
*/
public function offsetExists($key)
{
return $this->has($key);
}

/**
* {@inheritdoc}
*/
public function offsetGet($key)
{
return $this->get($key);
}

/**
* {@inheritdoc}
*/
public function offsetSet($key, $value)
{
$this->set($key, $value);
}

/**
* {@inheritdoc}
*/
public function offsetUnset($key)
{
$this->remove($key);
}

}
85 changes: 85 additions & 0 deletions tests/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,89 @@ public function testExport()

$this->assertEquals($this->getSampleData(), $data->export());
}

public function testOffsetExists()
{
$data = new Data($this->getSampleData());

foreach (
['a', 'i', 'b.d', 'f.g.h', 'h.i', 'b.d.d1'] as $existentKey
) {
$this->assertTrue(isset($data[$existentKey]));
}

foreach (
['p', 'b.b1', 'b.c.C1', 'h.i.I', 'b.d.d1.D1'] as $notExistentKey
) {
$this->assertFalse(isset($data[$notExistentKey]));
}
}

public function testOffsetGet()
{
$wrappedData = new Data([
'wrapped' => [
'sampleData' => $this->getSampleData()
],
]);

$data = $wrappedData->getData('wrapped.sampleData');

$this->assertEquals('A', $data['a']);
$this->assertEquals('B', $data['b.b']);
$this->assertEquals(['C1', 'C2', 'C3'], $data['b.c']);
$this->assertEquals('D3', $data['b.d.d3']);
$this->assertEquals(['c1', 'c2', 'c3'], $data['c']);
$this->assertNull($data['foo'], 'Foo should not exist');
$this->assertNull($data['f.g.h.i']);

$this->expectException(RuntimeException::class);

$data = $wrappedData->getData('wrapped.sampleData.a');
}

public function testOffsetSet()
{
$data = new Data;

$this->assertNull($data['a']);
$this->assertNull($data['b.c']);
$this->assertNull($data['d.e']);

$data['a'] = 'A';
$data['b.c'] = 'C';
$data['d.e'] = ['f' => 'F', 'g' => 'G'];

$this->assertEquals('A', $data['a']);
$this->assertEquals(['c' => 'C'], $data['b']);
$this->assertEquals('C', $data['b.c']);
$this->assertEquals('F', $data['d.e.f']);
$this->assertEquals(['e' => ['f' => 'F', 'g' => 'G']], $data['d']);

$this->expectException(RuntimeException::class);

$data->set('', 'broken');
}

public function testOffsetUnset()
{
$data = new Data($this->getSampleData());

unset($data['a']);
unset($data['b.c']);
unset($data['b.d.d3']);
unset($data['d']);
unset($data['d.e.f']);
unset($data['empty.path']);

$this->assertNull($data['a']);
$this->assertNull($data['b.c']);
$this->assertNull($data['b.d.d3']);
$this->assertNull(null);
$this->assertEquals('D2', $data['b.d.d2']);

$this->expectException(RuntimeException::class);

unset($data['']);
}
}