Skip to content

Commit ffabf1c

Browse files
committed
Additional testing, set cannot clobber strings, get should return null if trying to index into a string instead of an array.
1 parent e44e288 commit ffabf1c

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/Dflydev/DotAccessData/Data.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ public function set($key, $value = null)
100100
if (!isset($currentValue[$currentKey])) {
101101
$currentValue[$currentKey] = array();
102102
}
103+
if (!is_array($currentValue[$currentKey])) {
104+
throw new \RuntimeException("Key path at $currentKey of $key cannot be indexed into (is not an array)");
105+
}
103106
$currentValue =& $currentValue[$currentKey];
104107
}
105108
$currentValue[$endKey] = $value;
@@ -144,7 +147,12 @@ public function get($key)
144147

145148
for ( $i = 0; $i < count($keyPath); $i++ ) {
146149
$currentKey = $keyPath[$i];
147-
if (!isset($currentValue[$currentKey]) ) { return null; }
150+
if (!isset($currentValue[$currentKey]) ) {
151+
return null;
152+
}
153+
if (!is_array($currentValue)) {
154+
return null;
155+
}
148156
$currentValue = $currentValue[$currentKey];
149157
}
150158

tests/Dflydev/Tests/DotAccessData/DataTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ protected function getSampleData()
3030
),
3131
),
3232
'c' => array('c1', 'c2', 'c3'),
33+
'f' => array(
34+
'g' => array(
35+
'h' => 'FGH',
36+
),
37+
),
38+
'h' => array(
39+
'i' => 'I',
40+
),
41+
'i' => array(
42+
'j' => 'J',
43+
),
3344
);
3445
}
3546

@@ -41,6 +52,7 @@ protected function runSampleDataTests(DataInterface $data)
4152
$this->assertEquals('D3', $data->get('b.d.d3'));
4253
$this->assertEquals(array('c1', 'c2', 'c3'), $data->get('c'));
4354
$this->assertNull($data->get('foo'), 'Foo should not exist');
55+
$this->assertNull($data->get('f.g.h.i'));
4456
}
4557

4658
public function testAppend()
@@ -54,6 +66,8 @@ public function testAppend()
5466
$data->append('b.d.d4', 'D');
5567
$data->append('e', 'E');
5668
$data->append('f.a', 'b');
69+
$data->append('h.i', 'I2');
70+
$data->append('i.k.l', 'L');
5771

5872
$this->assertEquals(array('A', 'B'), $data->get('a'));
5973
$this->assertEquals(array('c1', 'c2', 'c3', 'c4'), $data->get('c'));
@@ -62,6 +76,8 @@ public function testAppend()
6276
$this->assertEquals(array('D'), $data->get('b.d.d4'));
6377
$this->assertEquals(array('E'), $data->get('e'));
6478
$this->assertEquals(array('b'), $data->get('f.a'));
79+
$this->assertEquals(array('I', 'I2'), $data->get('h.i'));
80+
$this->assertEquals(array('L'), $data->get('i.k.l'));
6581

6682
$this->setExpectedException('RuntimeException');
6783

@@ -91,6 +107,17 @@ public function testSet()
91107
$data->set('', 'broken');
92108
}
93109

110+
public function testSetClobberStringInPath()
111+
{
112+
$data = new Data;
113+
114+
$data->set('a.b.c', 'Should not be able to write to a.b.c.d.e');
115+
116+
$this->setExpectedException('RuntimeException');
117+
118+
$data->set('a.b.c.d.e', 'broken');
119+
}
120+
94121
public function testRemove()
95122
{
96123
$data = new Data($this->getSampleData());
@@ -100,6 +127,7 @@ public function testRemove()
100127
$data->remove('b.d.d3');
101128
$data->remove('d');
102129
$data->remove('d.e.f');
130+
$data->remove('empty.path');
103131

104132
$this->assertNull($data->get('a'));
105133
$this->assertNull($data->get('b.c'));

0 commit comments

Comments
 (0)