Skip to content

Commit 507a699

Browse files
committed
Allow forward-slashes to be use as path delimiters
1 parent c969ee1 commit 507a699

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

src/Data.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
class Data implements DataInterface, ArrayAccess
2222
{
23+
private const DELIMITERS = ['.', '/'];
24+
2325
/**
2426
* Internal representation of data data
2527
*
@@ -268,6 +270,8 @@ protected function keyToPathArray(string $path): array
268270
throw new InvalidPathException('Path cannot be an empty string');
269271
}
270272

273+
$path = \str_replace(self::DELIMITERS, '.', $path);
274+
271275
return \explode('.', $path);
272276
}
273277
}

tests/DataTest.php

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,18 @@ protected function runSampleDataTests(DataInterface $data)
4949
{
5050
$this->assertEquals('A', $data->get('a'));
5151
$this->assertEquals('B', $data->get('b.b'));
52+
$this->assertEquals('B', $data->get('b/b'));
5253
$this->assertEquals(['C1', 'C2', 'C3'], $data->get('b.c'));
54+
$this->assertEquals(['C1', 'C2', 'C3'], $data->get('b/c'));
5355
$this->assertEquals('D3', $data->get('b.d.d3'));
56+
$this->assertEquals('D3', $data->get('b/d/d3'));
5457
$this->assertEquals(['c1', 'c2', 'c3'], $data->get('c'));
55-
$this->assertNull($data->get('foo'), 'Foo should not exist');
56-
$this->assertNull($data->get('f.g.h.i'));
58+
$this->assertNull($data->get('foo', null), 'Foo should not exist');
59+
$this->assertNull($data->get('f.g.h.i', null));
60+
$this->assertNull($data->get('f/g/h/i', null));
5761
$this->assertEquals($data->get('foo', 'default-value-1'), 'default-value-1', 'Return default value');
5862
$this->assertEquals($data->get('f.g.h.i', 'default-value-2'), 'default-value-2');
63+
$this->assertEquals($data->get('f/g/h/i', 'default-value-2'), 'default-value-2');
5964

6065
$this->expectException(InvalidPathException::class);
6166
$data->get('', 'broken');
@@ -68,12 +73,12 @@ public function testAppend()
6873
$data->append('a', 'B');
6974
$data->append('c', 'c4');
7075
$data->append('b.c', 'C4');
71-
$data->append('b.d.d3', 'D3b');
76+
$data->append('b/d/d3', 'D3b');
7277
$data->append('b.d.d4', 'D');
7378
$data->append('e', 'E');
74-
$data->append('f.a', 'b');
79+
$data->append('f/a', 'b');
7580
$data->append('h.i', 'I2');
76-
$data->append('i.k.l', 'L');
81+
$data->append('i/k/l', 'L');
7782

7883
$this->assertEquals(['A', 'B'], $data->get('a'));
7984
$this->assertEquals(['c1', 'c2', 'c3', 'c4'], $data->get('c'));
@@ -94,18 +99,18 @@ public function testSet()
9499
{
95100
$data = new Data();
96101

97-
$this->assertNull($data->get('a'));
98-
$this->assertNull($data->get('b.c'));
99-
$this->assertNull($data->get('d.e'));
102+
$this->assertNull($data->get('a', null));
103+
$this->assertNull($data->get('b/c', null));
104+
$this->assertNull($data->get('d.e', null));
100105

101106
$data->set('a', 'A');
102-
$data->set('b.c', 'C');
107+
$data->set('b/c', 'C');
103108
$data->set('d.e', ['f' => 'F', 'g' => 'G']);
104109

105110
$this->assertEquals('A', $data->get('a'));
106111
$this->assertEquals(['c' => 'C'], $data->get('b'));
107112
$this->assertEquals('C', $data->get('b.c'));
108-
$this->assertEquals('F', $data->get('d.e.f'));
113+
$this->assertEquals('F', $data->get('d/e/f'));
109114
$this->assertEquals(['e' => ['f' => 'F', 'g' => 'G']], $data->get('d'));
110115

111116
$this->expectException(InvalidPathException::class);
@@ -130,13 +135,13 @@ public function testRemove()
130135

131136
$data->remove('a');
132137
$data->remove('b.c');
133-
$data->remove('b.d.d3');
138+
$data->remove('b/d/d3');
134139
$data->remove('d');
135140
$data->remove('d.e.f');
136141
$data->remove('empty.path');
137142

138143
$this->assertNull($data->get('a'));
139-
$this->assertNull($data->get('b.c'));
144+
$this->assertNull($data->get('b/c'));
140145
$this->assertNull($data->get('b.d.d3'));
141146
$this->assertNull(null);
142147
$this->assertEquals('D2', $data->get('b.d.d2'));
@@ -158,13 +163,13 @@ public function testHas()
158163
$data = new Data($this->getSampleData());
159164

160165
foreach (
161-
['a', 'i', 'b.d', 'f.g.h', 'h.i', 'b.d.d1'] as $existentKey
166+
['a', 'i', 'b.d', 'b/d', 'f.g.h', 'f/g/h', 'h.i', 'h/i', 'b.d.d1', 'b/d/d1'] as $existentKey
162167
) {
163168
$this->assertTrue($data->has($existentKey));
164169
}
165170

166171
foreach (
167-
['p', 'b.b1', 'b.c.C1', 'h.i.I', 'b.d.d1.D1'] as $notExistentKey
172+
['p', 'b.b1', 'b/b1', 'b.c.C1', 'b/c/C1', 'h.i.I', 'h/i/I', 'b.d.d1.D1', 'b/d/d1/D1'] as $notExistentKey
168173
) {
169174
$this->assertFalse($data->has($notExistentKey));
170175
}
@@ -218,13 +223,13 @@ public function testOffsetExists()
218223
$data = new Data($this->getSampleData());
219224

220225
foreach (
221-
['a', 'i', 'b.d', 'f.g.h', 'h.i', 'b.d.d1'] as $existentKey
226+
['a', 'i', 'b.d', 'b/d', 'f.g.h', 'f/g/h', 'h.i', 'h/i', 'b.d.d1', 'b/d/d1'] as $existentKey
222227
) {
223228
$this->assertTrue(isset($data[$existentKey]));
224229
}
225230

226231
foreach (
227-
['p', 'b.b1', 'b.c.C1', 'h.i.I', 'b.d.d1.D1'] as $notExistentKey
232+
['p', 'b.b1', 'b/b1', 'b.c.C1', 'b/c/C1', 'h.i.I', 'h/i/I', 'b.d.d1.D1', 'b/d/d1/D1'] as $notExistentKey
228233
) {
229234
$this->assertFalse(isset($data[$notExistentKey]));
230235
}
@@ -242,11 +247,15 @@ public function testOffsetGet()
242247

243248
$this->assertEquals('A', $data['a']);
244249
$this->assertEquals('B', $data['b.b']);
250+
$this->assertEquals('B', $data['b/b']);
245251
$this->assertEquals(['C1', 'C2', 'C3'], $data['b.c']);
252+
$this->assertEquals(['C1', 'C2', 'C3'], $data['b/c']);
246253
$this->assertEquals('D3', $data['b.d.d3']);
254+
$this->assertEquals('D3', $data['b/d/d3']);
247255
$this->assertEquals(['c1', 'c2', 'c3'], $data['c']);
248256
$this->assertNull($data['foo'], 'Foo should not exist');
249257
$this->assertNull($data['f.g.h.i']);
258+
$this->assertNull($data['f/g/h/i']);
250259
}
251260

252261
public function testOffsetSet()
@@ -257,14 +266,14 @@ public function testOffsetSet()
257266
$this->assertNull($data['b.c']);
258267
$this->assertNull($data['d.e']);
259268

260-
$data['a'] = 'A';
261-
$data['b.c'] = 'C';
269+
$data['a'] = 'A';
270+
$data['b/c'] = 'C';
262271
$data['d.e'] = ['f' => 'F', 'g' => 'G'];
263272

264273
$this->assertEquals('A', $data['a']);
265274
$this->assertEquals(['c' => 'C'], $data['b']);
266275
$this->assertEquals('C', $data['b.c']);
267-
$this->assertEquals('F', $data['d.e.f']);
276+
$this->assertEquals('F', $data['d/e/f']);
268277
$this->assertEquals(['e' => ['f' => 'F', 'g' => 'G']], $data['d']);
269278

270279
$this->expectException(InvalidPathException::class);
@@ -277,15 +286,15 @@ public function testOffsetUnset()
277286
$data = new Data($this->getSampleData());
278287

279288
unset($data['a']);
280-
unset($data['b.c']);
289+
unset($data['b/c']);
281290
unset($data['b.d.d3']);
282291
unset($data['d']);
283292
unset($data['d.e.f']);
284293
unset($data['empty.path']);
285294

286295
$this->assertNull($data['a']);
287296
$this->assertNull($data['b.c']);
288-
$this->assertNull($data['b.d.d3']);
297+
$this->assertNull($data['b/d/d3']);
289298
$this->assertNull(null);
290299
$this->assertEquals('D2', $data['b.d.d2']);
291300

0 commit comments

Comments
 (0)