Skip to content

Commit c56e0dc

Browse files
committed
fix: support sub paths
#2 (comment)
1 parent 5793b32 commit c56e0dc

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/DataBag.php

+15-4
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,18 @@ private function getByPath(string $path, $default = null)
8888
return $this->data[$entityType][$path] ?? $default;
8989
}
9090

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

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

97+
// Sub path
98+
if (\array_key_exists($index, $this->data[$entityType][$path])) {
99+
return $this->data[$entityType][$path][$index] ?? $default;
100+
}
101+
102+
// Indexed
98103
return $this->getIndexed((array)$this->data[$entityType][$path], $index, $default);
99104
}
100105

@@ -193,17 +198,23 @@ private function setByPath(string $path, $value): void
193198
return;
194199
}
195200

196-
// Indexed
197-
$this->setIndexed($entityType, $path, $value);
201+
// Sub path or indexed
202+
$this->setSubPathOrIndexed($entityType, $path, $value);
198203
}
199204

200205
/**
201206
* @param mixed $value
202207
*/
203-
private function setIndexed(string $entityType, string $path, $value): void
208+
private function setSubPathOrIndexed(string $entityType, string $path, $value): void
204209
{
205210
[$path, $index] = explode('.', $path, 2);
206211

212+
// Sub path
213+
if (!\is_array($value) && !\is_numeric($index) && \strpos($index, '.') === false) {
214+
$this->data[$entityType][$path][$index] = $value;
215+
return;
216+
}
217+
207218
$field = null;
208219
if (strpos($index, '.') > 0) {
209220
[$index, $field] = explode('.', $index, 2);

tests/GetFromDataBagTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ public function it_can_get_data_using_a_path(string $path, $expected): void
6262
self::assertEquals($expected, $this->dataBag->get($path, 'default'));
6363
}
6464

65+
/**
66+
* @test
67+
*/
68+
public function it_can_get_sub_paths(): void
69+
{
70+
$databag = DataBag::fromEntityData('file', ['metadata' => ['path' => 'Foo']]);
71+
self::assertSame('Foo', $databag->get('file.metadata.path'));
72+
}
73+
6574
protected function setUp(): void
6675
{
6776
$personData = [

tests/SetInBagTest.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function emptyDataBagProvider(): array
4040
return [
4141
['foo.bar', 1, ['foo' => ['bar' => 1]]],
4242
['foo.bar.0', 1, ['foo' => ['bar' => [1]]]],
43-
['foo.bar.test', 1, ['foo' => ['bar' => [1]]]],
43+
['foo.bar.test', 1, ['foo' => ['bar' => ['test' => 1]]]],
4444
['foo.bar.test', ['baz' => 3], ['foo' => ['bar' => [['baz' => 3, 'type' => 'test']]]]],
4545
['foo.bar.0.baz', 1, ['foo' => ['bar' => [['baz' => 1]]]]],
4646
['foo.bar.test.baz', 1, ['foo' => ['bar' => [['baz' => 1, 'type' => 'test']]]]],
@@ -84,7 +84,7 @@ public function filledDataBagProvider(): array
8484
[
8585
'foo.b.s',
8686
2,
87-
['foo' => ['a' => 1, 'b' => [['type' => 'f', 'c' => 2], 2]]]
87+
['foo' => ['a' => 1, 'b' => [['type' => 'f', 'c' => 2], ['type' => 's', 'c' => 3], 's' => 2]]]
8888
],
8989
[
9090
'foo.b.s',
@@ -128,6 +128,19 @@ public function it_can_handle_new_type_if_target_is_an_array_with_an_empty_array
128128
);
129129
}
130130

131+
/**
132+
* @test
133+
*/
134+
public function it_can_set_by_sub_path(): void
135+
{
136+
$dataBag = DataBag::fromEntityData('file', []);
137+
$dataBag->set('file.metadata.path', 'Foo');
138+
self::assertSame(
139+
['metadata' => ['path' => 'Foo']],
140+
$dataBag->getState('file')
141+
);
142+
}
143+
131144
protected function setUp(): void
132145
{
133146
$this->emptyDataBag = DataBag::create();

0 commit comments

Comments
 (0)