Skip to content

Commit

Permalink
Merge pull request #36 from mattsah/2.x
Browse files Browse the repository at this point in the history
Adding per collection custom delimiter
  • Loading branch information
adbario authored Oct 14, 2022
2 parents 3bfe678 + c58b2fd commit 081e2cc
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 12 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ $dot = dot();
$dot = dot($array);
```

It is possible use an alternative delimiter from the default dot (`.`) with the second constructor parameter. Using an underscore instead:

```php
$dot = new \Adbar\Dot($array, '_');

// With the helper
$dot = dot($array, '_');
```

## Methods

Dot has the following methods:
Expand Down
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 = strlen($delimiter) ? $delimiter : '.';
}

/**
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
9 changes: 5 additions & 4 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

if (! function_exists('dot')) {
/**
* Create a new Dot object with the given items
* Create a new Dot object with the given items and optional delimiter
*
* @param mixed $items
* @param mixed $items
* @param string $delimiter
* @return \Adbar\Dot
*/
function dot($items)
function dot($items, $delimiter = '.')
{
return new Dot($items);
return new Dot($items, $delimiter);
}
}
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

0 comments on commit 081e2cc

Please sign in to comment.