Skip to content

Commit da970b7

Browse files
committed
Allow null values
1 parent c383e69 commit da970b7

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/Data.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function get(string $key, $default = null)
124124
$keyPath = self::keyToPathArray($key);
125125

126126
foreach ($keyPath as $currentKey) {
127-
if (!is_array($currentValue) || !isset($currentValue[$currentKey])) {
127+
if (!is_array($currentValue) || !array_key_exists($currentKey, $currentValue)) {
128128
if ($hasDefault) {
129129
return $default;
130130
}

tests/DataTest.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ protected function getSampleData()
4444
'i' => [
4545
'j' => 'J',
4646
],
47+
'n' => null,
48+
'n2' => [
49+
'n' => null,
50+
],
4751
];
4852
}
4953

@@ -63,6 +67,8 @@ protected function runSampleDataTests(DataInterface $data)
6367
$this->assertEquals($data->get('foo', 'default-value-1'), 'default-value-1', 'Return default value');
6468
$this->assertEquals($data->get('f.g.h.i', 'default-value-2'), 'default-value-2');
6569
$this->assertEquals($data->get('f/g/h/i', 'default-value-2'), 'default-value-2');
70+
$this->assertNull($data->get('n'));
71+
$this->assertNull($data->get('n2/n'));
6672

6773
$this->expectException(InvalidPathException::class);
6874
$this->expectExceptionMessage('Path cannot be an empty string');
@@ -82,6 +88,8 @@ public function testAppend()
8288
$data->append('f/a', 'b');
8389
$data->append('h.i', 'I2');
8490
$data->append('i/k/l', 'L');
91+
$data->append('n', 'N');
92+
$data->append('n2/n', 'N');
8593

8694
$this->assertEquals(['A', 'B'], $data->get('a'));
8795
$this->assertEquals(['c1', 'c2', 'c3', 'c4'], $data->get('c'));
@@ -92,6 +100,8 @@ public function testAppend()
92100
$this->assertEquals(['b'], $data->get('f.a'));
93101
$this->assertEquals(['I', 'I2'], $data->get('h.i'));
94102
$this->assertEquals(['L'], $data->get('i.k.l'));
103+
$this->assertEquals(['N'], $data->get('n'));
104+
$this->assertEquals(['N'], $data->get('n2/n'));
95105

96106
$this->expectException(InvalidPathException::class);
97107
$this->expectExceptionMessage('Path cannot be an empty string');
@@ -116,6 +126,10 @@ public function testSet()
116126
$this->assertEquals('F', $data->get('d/e/f'));
117127
$this->assertEquals(['e' => ['f' => 'F', 'g' => 'G']], $data->get('d'));
118128

129+
$data->set('a', null);
130+
$this->assertTrue($data->has('a'), 'Data should exist with a null value');
131+
$this->assertNull($data->get('a'), 'Data should exist with a null value');
132+
119133
$this->expectException(InvalidPathException::class);
120134
$this->expectExceptionMessage('Path cannot be an empty string');
121135
$data->set('', 'broken');
@@ -143,8 +157,11 @@ public function testRemove()
143157
$data->remove('d.e.f');
144158
$data->remove('empty.path');
145159

160+
$this->assertFalse($data->has('a'));
146161
$this->assertNull($data->get('a', null));
162+
$this->assertFalse($data->has('b/c'));
147163
$this->assertNull($data->get('b/c', null));
164+
$this->assertFalse($data->has('b.d.d3'));
148165
$this->assertNull($data->get('b.d.d3', null));
149166
$this->assertNull(null);
150167
$this->assertEquals('D2', $data->get('b.d.d2'));
@@ -181,7 +198,7 @@ public function testHas()
181198
$data = new Data($this->getSampleData());
182199

183200
foreach (
184-
['a', 'i', 'b.d', 'b/d', 'f.g.h', 'f/g/h', 'h.i', 'h/i', 'b.d.d1', 'b/d/d1'] as $existentKey
201+
['a', 'i', 'b.d', 'b/d', 'f.g.h', 'f/g/h', 'h.i', 'h/i', 'b.d.d1', 'b/d/d1', 'n', 'n2/n'] as $existentKey
185202
) {
186203
$this->assertTrue($data->has($existentKey));
187204
}
@@ -249,7 +266,7 @@ public function testOffsetExists()
249266
$data = new Data($this->getSampleData());
250267

251268
foreach (
252-
['a', 'i', 'b.d', 'b/d', 'f.g.h', 'f/g/h', 'h.i', 'h/i', 'b.d.d1', 'b/d/d1'] as $existentKey
269+
['a', 'i', 'b.d', 'b/d', 'f.g.h', 'f/g/h', 'h.i', 'h/i', 'b.d.d1', 'b/d/d1', 'n', 'n2/n'] as $existentKey
253270
) {
254271
$this->assertTrue(isset($data[$existentKey]));
255272
}
@@ -282,6 +299,8 @@ public function testOffsetGet()
282299
$this->assertNull($data['foo'], 'Foo should not exist');
283300
$this->assertNull($data['f.g.h.i']);
284301
$this->assertNull($data['f/g/h/i']);
302+
$this->assertNull($data['n']);
303+
$this->assertNull($data['n2/n']);
285304
}
286305

287306
public function testOffsetSet()
@@ -295,12 +314,14 @@ public function testOffsetSet()
295314
$data['a'] = 'A';
296315
$data['b/c'] = 'C';
297316
$data['d.e'] = ['f' => 'F', 'g' => 'G'];
317+
$data['foo'] = null;
298318

299319
$this->assertEquals('A', $data['a']);
300320
$this->assertEquals(['c' => 'C'], $data['b']);
301321
$this->assertEquals('C', $data['b.c']);
302322
$this->assertEquals('F', $data['d/e/f']);
303323
$this->assertEquals(['e' => ['f' => 'F', 'g' => 'G']], $data['d']);
324+
$this->assertNull($data['foo']);
304325

305326
$this->expectException(InvalidPathException::class);
306327
$this->expectExceptionMessage('Path cannot be an empty string');
@@ -324,6 +345,16 @@ public function testOffsetUnset()
324345
$this->assertNull(null);
325346
$this->assertEquals('D2', $data['b.d.d2']);
326347

348+
$this->assertTrue($data->has('n'));
349+
$this->assertNull($data->get('n'));
350+
unset($data['n']);
351+
$this->assertFalse($data->has('n'));
352+
353+
$this->assertTrue($data->has('n2/n'));
354+
$this->assertNull($data->get('n2/n'));
355+
unset($data['n2/n']);
356+
$this->assertFalse($data->has('n2/n'));
357+
327358
$this->expectException(InvalidPathException::class);
328359
$this->expectExceptionMessage('Path cannot be an empty string');
329360
unset($data['']);

0 commit comments

Comments
 (0)