Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/Dot.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,25 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
*/
protected $items = [];


/**
* The delimiter (alternative to a '.') to be used.
*
* @var string
*/
protected $delimiter = '.';


/**
* Create a new Dot instance
*
* @param mixed $items
* @param string $delimiter
*/
public function __construct($items = [])
public function __construct($items = [], $delimiter = '.')
{
$this->items = $this->getArrayItems($items);
$this->delimiter = $delimiter;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the delimiter is an empty string, explode() will throw an error. So the delimiter must default to a dot if an empty string was passed to the constructor.

}

/**
Expand Down Expand Up @@ -104,7 +115,7 @@ public function delete($keys)
}

$items = &$this->items;
$segments = explode('.', $key);
$segments = explode($this->delimiter, $key);
$lastSegment = array_pop($segments);

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

if (!func_num_args()) {
$delimiter = $this->delimiter;
}

foreach ($items as $key => $value) {
if (is_array($value) && !empty($value)) {
$flatten = array_merge(
Expand Down Expand Up @@ -179,13 +194,13 @@ public function get($key = null, $default = null)
return $this->items[$key];
}

if (strpos($key, '.') === false) {
if (strpos($key, $this->delimiter) === false) {
return $default;
}

$items = $this->items;

foreach (explode('.', $key) as $segment) {
foreach (explode($this->delimiter, $key) as $segment) {
if (!is_array($items) || !$this->exists($items, $segment)) {
return $default;
}
Expand Down Expand Up @@ -234,7 +249,7 @@ public function has($keys)
continue;
}

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

$items = &$this->items;

foreach (explode('.', $keys) as $key) {
foreach (explode($this->delimiter, $keys) as $key) {
if (!isset($items[$key]) || !is_array($items[$key])) {
$items[$key] = [];
}
Expand Down
47 changes: 45 additions & 2 deletions tests/DotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public function testAddKeyValuePair()
$this->assertEquals('baz', $dot->get('foo.bar'));
}

public function testAddKeyValuePairWithCustomDelimeter()
{
$dot = new Dot([], '/');
$dot->add('foo/bar', 'baz');

$this->assertEquals('baz', $dot->get('foo/bar'));
}

public function testAddValueToExistingKey()
{
$dot = new Dot(['foo' => 'bar']);
Expand Down Expand Up @@ -116,6 +124,14 @@ public function testClearKey()
$this->assertSame([], $dot->get('foo.bar'));
}

public function testClearKeyWithCustomDelimiter()
{
$dot = new Dot(['foo' => ['bar' => 'baz']], '/');
$dot->clear('foo/bar');

$this->assertSame([], $dot->get('foo/bar'));
}

public function testClearNonExistingKey()
{
$dot = new Dot;
Expand Down Expand Up @@ -154,6 +170,14 @@ public function testDeleteKey()
$this->assertFalse($dot->has('foo.bar'));
}

public function testDeleteKeyWithCustomDelimeter()
{
$dot = new Dot(['foo' => ['bar' => 'baz']], '/');
$dot->delete('foo/bar');

$this->assertFalse($dot->has('foo/bar'));
}

public function testDeleteNonExistingKey()
{
$dot = new Dot(['foo' => 'bar']);
Expand Down Expand Up @@ -182,15 +206,25 @@ public function testFlatten()
$this->assertEquals('xyz', $flatten['foo.abc']);
$this->assertEquals('baz', $flatten['foo.bar.0']);
}

public function testFlattenWithCustomDelimiter()
{
$dot = new Dot(['foo' => ['abc' => 'xyz', 'bar' => ['baz']]]);
$dot = new Dot(['foo' => ['abc' => 'xyz', 'bar' => ['baz']]], '/');
$flatten = $dot->flatten();
$this->assertEquals('xyz', $flatten['foo/abc']);
$this->assertEquals('baz', $flatten['foo/bar/0']);
}


public function testFlattenWithDoubleCustomDelimiter()
{
$dot = new Dot(['foo' => ['abc' => 'xyz', 'bar' => ['baz']]], '/');
$flatten = $dot->flatten('_');
$this->assertEquals('xyz', $flatten['foo_abc']);
$this->assertEquals('baz', $flatten['foo_bar_0']);
}


/*
* --------------------------------------------------------------
* Get
Expand Down Expand Up @@ -532,6 +566,15 @@ public function testSetKeyValuePair()
$this->assertEquals('baz', $dot->get('foo.bar'));
}

public function testSetKeyValuePairWithCustomDelimiter()
{
$dot = new Dot([], '/');
$dot->set('foo/bar', 'baz');

$this->assertEquals('baz', $dot->get('foo/bar'));
}


public function testSetArrayOfKeyValuePairs()
{
$dot = new Dot;
Expand Down