Skip to content

Commit 7777aea

Browse files
committed
Adding per collection custom delimiter
1 parent 3bfe678 commit 7777aea

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

src/Dot.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,25 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
2929
*/
3030
protected $items = [];
3131

32+
33+
/**
34+
* The delimiter (alternative to a '.') to be used.
35+
*
36+
* @var string
37+
*/
38+
protected $delimiter = '.';
39+
40+
3241
/**
3342
* Create a new Dot instance
3443
*
3544
* @param mixed $items
45+
* @param string $delimiter
3646
*/
37-
public function __construct($items = [])
47+
public function __construct($items = [], $delimiter = '.')
3848
{
3949
$this->items = $this->getArrayItems($items);
50+
$this->delimiter = $delimiter;
4051
}
4152

4253
/**
@@ -104,7 +115,7 @@ public function delete($keys)
104115
}
105116

106117
$items = &$this->items;
107-
$segments = explode('.', $key);
118+
$segments = explode($this->delimiter, $key);
108119
$lastSegment = array_pop($segments);
109120

110121
foreach ($segments as $segment) {
@@ -148,6 +159,10 @@ public function flatten($delimiter = '.', $items = null, $prepend = '')
148159
$items = $this->items;
149160
}
150161

162+
if (!func_num_args()) {
163+
$delimiter = $this->delimiter;
164+
}
165+
151166
foreach ($items as $key => $value) {
152167
if (is_array($value) && !empty($value)) {
153168
$flatten = array_merge(
@@ -179,13 +194,13 @@ public function get($key = null, $default = null)
179194
return $this->items[$key];
180195
}
181196

182-
if (strpos($key, '.') === false) {
197+
if (strpos($key, $this->delimiter) === false) {
183198
return $default;
184199
}
185200

186201
$items = $this->items;
187202

188-
foreach (explode('.', $key) as $segment) {
203+
foreach (explode($this->delimiter, $key) as $segment) {
189204
if (!is_array($items) || !$this->exists($items, $segment)) {
190205
return $default;
191206
}
@@ -234,7 +249,7 @@ public function has($keys)
234249
continue;
235250
}
236251

237-
foreach (explode('.', $key) as $segment) {
252+
foreach (explode($this->delimiter, $key) as $segment) {
238253
if (!is_array($items) || !$this->exists($items, $segment)) {
239254
return false;
240255
}
@@ -446,7 +461,7 @@ public function set($keys, $value = null)
446461

447462
$items = &$this->items;
448463

449-
foreach (explode('.', $keys) as $key) {
464+
foreach (explode($this->delimiter, $keys) as $key) {
450465
if (!isset($items[$key]) || !is_array($items[$key])) {
451466
$items[$key] = [];
452467
}

tests/DotTest.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ public function testAddKeyValuePair()
7070
$this->assertEquals('baz', $dot->get('foo.bar'));
7171
}
7272

73+
public function testAddKeyValuePairWithCustomDelimeter()
74+
{
75+
$dot = new Dot([], '/');
76+
$dot->add('foo/bar', 'baz');
77+
78+
$this->assertEquals('baz', $dot->get('foo/bar'));
79+
}
80+
7381
public function testAddValueToExistingKey()
7482
{
7583
$dot = new Dot(['foo' => 'bar']);
@@ -116,6 +124,14 @@ public function testClearKey()
116124
$this->assertSame([], $dot->get('foo.bar'));
117125
}
118126

127+
public function testClearKeyWithCustomDelimiter()
128+
{
129+
$dot = new Dot(['foo' => ['bar' => 'baz']], '/');
130+
$dot->clear('foo/bar');
131+
132+
$this->assertSame([], $dot->get('foo/bar'));
133+
}
134+
119135
public function testClearNonExistingKey()
120136
{
121137
$dot = new Dot;
@@ -154,6 +170,14 @@ public function testDeleteKey()
154170
$this->assertFalse($dot->has('foo.bar'));
155171
}
156172

173+
public function testDeleteKeyWithCustomDelimeter()
174+
{
175+
$dot = new Dot(['foo' => ['bar' => 'baz']], '/');
176+
$dot->delete('foo/bar');
177+
178+
$this->assertFalse($dot->has('foo/bar'));
179+
}
180+
157181
public function testDeleteNonExistingKey()
158182
{
159183
$dot = new Dot(['foo' => 'bar']);
@@ -182,15 +206,25 @@ public function testFlatten()
182206
$this->assertEquals('xyz', $flatten['foo.abc']);
183207
$this->assertEquals('baz', $flatten['foo.bar.0']);
184208
}
185-
209+
186210
public function testFlattenWithCustomDelimiter()
187211
{
188-
$dot = new Dot(['foo' => ['abc' => 'xyz', 'bar' => ['baz']]]);
212+
$dot = new Dot(['foo' => ['abc' => 'xyz', 'bar' => ['baz']]], '/');
213+
$flatten = $dot->flatten();
214+
$this->assertEquals('xyz', $flatten['foo/abc']);
215+
$this->assertEquals('baz', $flatten['foo/bar/0']);
216+
}
217+
218+
219+
public function testFlattenWithDoubleCustomDelimiter()
220+
{
221+
$dot = new Dot(['foo' => ['abc' => 'xyz', 'bar' => ['baz']]], '/');
189222
$flatten = $dot->flatten('_');
190223
$this->assertEquals('xyz', $flatten['foo_abc']);
191224
$this->assertEquals('baz', $flatten['foo_bar_0']);
192225
}
193226

227+
194228
/*
195229
* --------------------------------------------------------------
196230
* Get
@@ -532,6 +566,15 @@ public function testSetKeyValuePair()
532566
$this->assertEquals('baz', $dot->get('foo.bar'));
533567
}
534568

569+
public function testSetKeyValuePairWithCustomDelimiter()
570+
{
571+
$dot = new Dot([], '/');
572+
$dot->set('foo/bar', 'baz');
573+
574+
$this->assertEquals('baz', $dot->get('foo/bar'));
575+
}
576+
577+
535578
public function testSetArrayOfKeyValuePairs()
536579
{
537580
$dot = new Dot;

0 commit comments

Comments
 (0)