Skip to content

Commit

Permalink
fix: support sub paths
Browse files Browse the repository at this point in the history
  • Loading branch information
mishavantol committed Oct 24, 2020
1 parent 5793b32 commit c56e0dc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
19 changes: 15 additions & 4 deletions src/DataBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,18 @@ private function getByPath(string $path, $default = null)
return $this->data[$entityType][$path] ?? $default;
}

// Indexed
[$path, $index] = explode('.', $path, 2);

if (empty($this->data[$entityType][$path])) {
return $default;
}

// Sub path
if (\array_key_exists($index, $this->data[$entityType][$path])) {
return $this->data[$entityType][$path][$index] ?? $default;
}

// Indexed
return $this->getIndexed((array)$this->data[$entityType][$path], $index, $default);
}

Expand Down Expand Up @@ -193,17 +198,23 @@ private function setByPath(string $path, $value): void
return;
}

// Indexed
$this->setIndexed($entityType, $path, $value);
// Sub path or indexed
$this->setSubPathOrIndexed($entityType, $path, $value);
}

/**
* @param mixed $value
*/
private function setIndexed(string $entityType, string $path, $value): void
private function setSubPathOrIndexed(string $entityType, string $path, $value): void
{
[$path, $index] = explode('.', $path, 2);

// Sub path
if (!\is_array($value) && !\is_numeric($index) && \strpos($index, '.') === false) {
$this->data[$entityType][$path][$index] = $value;
return;
}

$field = null;
if (strpos($index, '.') > 0) {
[$index, $field] = explode('.', $index, 2);
Expand Down
9 changes: 9 additions & 0 deletions tests/GetFromDataBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ public function it_can_get_data_using_a_path(string $path, $expected): void
self::assertEquals($expected, $this->dataBag->get($path, 'default'));
}

/**
* @test
*/
public function it_can_get_sub_paths(): void
{
$databag = DataBag::fromEntityData('file', ['metadata' => ['path' => 'Foo']]);
self::assertSame('Foo', $databag->get('file.metadata.path'));
}

protected function setUp(): void
{
$personData = [
Expand Down
17 changes: 15 additions & 2 deletions tests/SetInBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function emptyDataBagProvider(): array
return [
['foo.bar', 1, ['foo' => ['bar' => 1]]],
['foo.bar.0', 1, ['foo' => ['bar' => [1]]]],
['foo.bar.test', 1, ['foo' => ['bar' => [1]]]],
['foo.bar.test', 1, ['foo' => ['bar' => ['test' => 1]]]],
['foo.bar.test', ['baz' => 3], ['foo' => ['bar' => [['baz' => 3, 'type' => 'test']]]]],
['foo.bar.0.baz', 1, ['foo' => ['bar' => [['baz' => 1]]]]],
['foo.bar.test.baz', 1, ['foo' => ['bar' => [['baz' => 1, 'type' => 'test']]]]],
Expand Down Expand Up @@ -84,7 +84,7 @@ public function filledDataBagProvider(): array
[
'foo.b.s',
2,
['foo' => ['a' => 1, 'b' => [['type' => 'f', 'c' => 2], 2]]]
['foo' => ['a' => 1, 'b' => [['type' => 'f', 'c' => 2], ['type' => 's', 'c' => 3], 's' => 2]]]
],
[
'foo.b.s',
Expand Down Expand Up @@ -128,6 +128,19 @@ public function it_can_handle_new_type_if_target_is_an_array_with_an_empty_array
);
}

/**
* @test
*/
public function it_can_set_by_sub_path(): void
{
$dataBag = DataBag::fromEntityData('file', []);
$dataBag->set('file.metadata.path', 'Foo');
self::assertSame(
['metadata' => ['path' => 'Foo']],
$dataBag->getState('file')
);
}

protected function setUp(): void
{
$this->emptyDataBag = DataBag::create();
Expand Down

0 comments on commit c56e0dc

Please sign in to comment.