From c56e0dc50b164e520dea5d8eebc849e2229f8528 Mon Sep 17 00:00:00 2001 From: Misha van Tol Date: Sat, 24 Oct 2020 22:20:52 +0200 Subject: [PATCH] fix: support sub paths https://github.com/kingsquare/communibase-databag/issues/2#issue-645661313 --- src/DataBag.php | 19 +++++++++++++++---- tests/GetFromDataBagTest.php | 9 +++++++++ tests/SetInBagTest.php | 17 +++++++++++++++-- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/DataBag.php b/src/DataBag.php index 9d0e6bb..2a89ff0 100644 --- a/src/DataBag.php +++ b/src/DataBag.php @@ -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); } @@ -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); diff --git a/tests/GetFromDataBagTest.php b/tests/GetFromDataBagTest.php index f386e10..54b1d93 100644 --- a/tests/GetFromDataBagTest.php +++ b/tests/GetFromDataBagTest.php @@ -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 = [ diff --git a/tests/SetInBagTest.php b/tests/SetInBagTest.php index cf90efc..19076d9 100644 --- a/tests/SetInBagTest.php +++ b/tests/SetInBagTest.php @@ -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']]]]], @@ -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', @@ -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();