Skip to content

Commit 6cab20b

Browse files
authored
Merge pull request #38 from adbario/feat/custom-delimiter
Implement optional custom delimeter
2 parents 76e3398 + 39ab326 commit 6cab20b

File tree

5 files changed

+64
-17
lines changed

5 files changed

+64
-17
lines changed

LICENSE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# The MIT License (MIT)
22

3-
Copyright (c) 2016-2019 Riku Särkinen
3+
Copyright (c) 2016-2022 Riku Särkinen
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ $dot = new \Adbar\Dot($array);
6262

6363
// Or with auto parsing dot notation keys in existing array
6464
$dot = new \Adbar\Dot($array, true);
65+
66+
// You can also set a custom delimiter instead of the default dot (.)
67+
$dot = new \Adbar\Dot($array, false, "_");
6568
```
6669

6770
You can also use a helper function to create the object:
@@ -73,6 +76,9 @@ $dot = dot($array);
7376

7477
// Or with auto parsing dot notation keys in existing array
7578
$dot = dot($array, true);
79+
80+
// You can also set a custom delimiter instead of the default dot (.)
81+
$dot = dot($array, true, "_");
7682
```
7783

7884
All methods not returning a specific value returns the Dot object for chaining:

src/Dot.php

+18-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* @author Riku Särkinen <[email protected]>
77
* @link https://github.com/adbario/php-dot-notation
8-
* @license https://github.com/adbario/php-dot-notation/blob/2.x/LICENSE.md (MIT License)
8+
* @license https://github.com/adbario/php-dot-notation/blob/3.x/LICENSE.md (MIT License)
99
*/
1010

1111
namespace Adbar;
@@ -36,19 +36,29 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
3636
*
3737
* @var array<TKey, TValue>
3838
*/
39-
protected $items = [];
39+
protected $items;
40+
41+
/**
42+
* The character to use as a delimiter, defaults to dot (.)
43+
*
44+
* @var non-empty-string
45+
*/
46+
protected $delimiter = ".";
4047

4148
/**
4249
* Create a new Dot instance
4350
*
4451
* @param mixed $items
4552
* @param bool $parse
53+
* @param non-empty-string $delimiter
4654
* @return void
4755
*/
48-
public function __construct($items = [], $parse = false)
56+
public function __construct($items = [], $parse = false, $delimiter = ".")
4957
{
5058
$items = $this->getArrayItems($items);
5159

60+
$this->delimiter = $delimiter ?: ".";
61+
5262
if ($parse) {
5363
$this->set($items);
5464
} else {
@@ -128,7 +138,7 @@ public function delete($keys)
128138
}
129139

130140
$items = &$this->items;
131-
$segments = explode('.', $key);
141+
$segments = explode($this->delimiter, $key);
132142
$lastSegment = array_pop($segments);
133143

134144
foreach ($segments as $segment) {
@@ -201,13 +211,13 @@ public function get($key = null, $default = null)
201211
return $this->items[$key];
202212
}
203213

204-
if (!is_string($key) || strpos($key, '.') === false) {
214+
if (!is_string($key) || strpos($key, $this->delimiter) === false) {
205215
return $default;
206216
}
207217

208218
$items = $this->items;
209219

210-
foreach (explode('.', $key) as $segment) {
220+
foreach (explode($this->delimiter, $key) as $segment) {
211221
if (!is_array($items) || !$this->exists($items, $segment)) {
212222
return $default;
213223
}
@@ -258,7 +268,7 @@ public function has($keys)
258268
continue;
259269
}
260270

261-
foreach (explode('.', $key) as $segment) {
271+
foreach (explode($this->delimiter, $key) as $segment) {
262272
if (!is_array($items) || !$this->exists($items, $segment)) {
263273
return false;
264274
}
@@ -487,7 +497,7 @@ public function set($keys, $value = null)
487497
$items = &$this->items;
488498

489499
if (is_string($keys)) {
490-
foreach (explode('.', $keys) as $key) {
500+
foreach (explode($this->delimiter, $keys) as $key) {
491501
if (!isset($items[$key]) || !is_array($items[$key])) {
492502
$items[$key] = [];
493503
}

src/helpers.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* @author Riku Särkinen <[email protected]>
77
* @link https://github.com/adbario/php-dot-notation
8-
* @license https://github.com/adbario/php-dot-notation/blob/2.x/LICENSE.md (MIT License)
8+
* @license https://github.com/adbario/php-dot-notation/blob/3.x/LICENSE.md (MIT License)
99
*/
1010

1111
use Adbar\Dot;
@@ -16,10 +16,11 @@
1616
*
1717
* @param mixed $items
1818
* @param bool $parse
19+
* @param non-empty-string $delimiter
1920
* @return \Adbar\Dot<array-key, mixed>
2021
*/
21-
function dot($items, $parse = false)
22+
function dot($items, $parse = false, $delimiter = ".")
2223
{
23-
return new Dot($items, $parse);
24+
return new Dot($items, $parse, $delimiter);
2425
}
2526
}

tests/DotTest.php

+35-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* @author Riku Särkinen <[email protected]>
77
* @link https://github.com/adbario/php-dot-notation
8-
* @license https://github.com/adbario/php-dot-notation/blob/2.x/LICENSE.md (MIT License)
8+
* @license https://github.com/adbario/php-dot-notation/blob/3.x/LICENSE.md (MIT License)
99
*/
1010

1111
namespace Adbar\Tests;
@@ -50,6 +50,28 @@ public function testConstructWithDot(): void
5050
$this->assertEquals('bar', $dot2->get('foo'));
5151
}
5252

53+
public function testConstructWithParsing(): void
54+
{
55+
$dot = new Dot(['foo.bar' => 'baz']);
56+
57+
$this->assertEquals(['foo.bar' => 'baz'], $dot->get());
58+
59+
$dot = new Dot(['foo.bar' => 'baz'], true);
60+
61+
$this->assertEquals(['foo' => ['bar' => 'baz']], $dot->get());
62+
}
63+
64+
public function testConstructWithCustomDelimiter(): void
65+
{
66+
$dot = new Dot(['foo_bar' => 'baz'], false, "_");
67+
68+
$this->assertEquals(['foo_bar' => 'baz'], $dot->get());
69+
70+
$dot = new Dot(['foo_bar' => 'baz'], true, "_");
71+
72+
$this->assertEquals(['foo' => ['bar' => 'baz']], $dot->get());
73+
}
74+
5375
public function testConstructHelper(): void
5476
{
5577
$dot = dot(['foo' => 'bar']);
@@ -58,13 +80,20 @@ public function testConstructHelper(): void
5880
$this->assertEquals('bar', $dot->get('foo'));
5981
}
6082

61-
public function testConstructWithParsing(): void
83+
public function testConstructHelpertWithParsing(): void
6284
{
63-
$dot = new Dot(['foo.bar' => 'baz']);
85+
$dot = dot(['foo.bar' => 'baz'], true);
6486

65-
$this->assertEquals(['foo.bar' => 'baz'], $dot->get());
87+
$this->assertEquals(['foo' => ['bar' => 'baz']], $dot->get());
88+
}
6689

67-
$dot = new Dot(['foo.bar' => 'baz'], true);
90+
public function testConstructHelpertWithCustomDelimiter(): void
91+
{
92+
$dot = dot(['foo_bar' => 'baz'], false, "_");
93+
94+
$this->assertEquals(['foo_bar' => 'baz'], $dot->get());
95+
96+
$dot = dot(['foo_bar' => 'baz'], true, "_");
6897

6998
$this->assertEquals(['foo' => ['bar' => 'baz']], $dot->get());
7099
}
@@ -788,6 +817,7 @@ public function testVarExport(): void
788817
" 'bar' => 'baz',\n" .
789818
" ),\n" .
790819
" ),\n" .
820+
" 'delimiter' => '.',\n" .
791821
"))",
792822
var_export($dot, true)
793823
);

0 commit comments

Comments
 (0)