Skip to content

Commit

Permalink
Merge pull request #38 from adbario/feat/custom-delimiter
Browse files Browse the repository at this point in the history
Implement optional custom delimeter
  • Loading branch information
adbario committed Oct 15, 2022
2 parents 76e3398 + 39ab326 commit 6cab20b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 17 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The MIT License (MIT)

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

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ $dot = new \Adbar\Dot($array);

// Or with auto parsing dot notation keys in existing array
$dot = new \Adbar\Dot($array, true);

// You can also set a custom delimiter instead of the default dot (.)
$dot = new \Adbar\Dot($array, false, "_");
```

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

// Or with auto parsing dot notation keys in existing array
$dot = dot($array, true);

// You can also set a custom delimiter instead of the default dot (.)
$dot = dot($array, true, "_");
```

All methods not returning a specific value returns the Dot object for chaining:
Expand Down
26 changes: 18 additions & 8 deletions src/Dot.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* @author Riku Särkinen <riku@adbar.io>
* @link https://github.com/adbario/php-dot-notation
* @license https://github.com/adbario/php-dot-notation/blob/2.x/LICENSE.md (MIT License)
* @license https://github.com/adbario/php-dot-notation/blob/3.x/LICENSE.md (MIT License)
*/

namespace Adbar;
Expand Down Expand Up @@ -36,19 +36,29 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
*
* @var array<TKey, TValue>
*/
protected $items = [];
protected $items;

/**
* The character to use as a delimiter, defaults to dot (.)
*
* @var non-empty-string
*/
protected $delimiter = ".";

/**
* Create a new Dot instance
*
* @param mixed $items
* @param bool $parse
* @param non-empty-string $delimiter
* @return void
*/
public function __construct($items = [], $parse = false)
public function __construct($items = [], $parse = false, $delimiter = ".")
{
$items = $this->getArrayItems($items);

$this->delimiter = $delimiter ?: ".";

if ($parse) {
$this->set($items);
} else {
Expand Down Expand Up @@ -128,7 +138,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 @@ -201,13 +211,13 @@ public function get($key = null, $default = null)
return $this->items[$key];
}

if (!is_string($key) || strpos($key, '.') === false) {
if (!is_string($key) || 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 @@ -258,7 +268,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 @@ -487,7 +497,7 @@ public function set($keys, $value = null)
$items = &$this->items;

if (is_string($keys)) {
foreach (explode('.', $keys) as $key) {
foreach (explode($this->delimiter, $keys) as $key) {
if (!isset($items[$key]) || !is_array($items[$key])) {
$items[$key] = [];
}
Expand Down
7 changes: 4 additions & 3 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* @author Riku Särkinen <riku@adbar.io>
* @link https://github.com/adbario/php-dot-notation
* @license https://github.com/adbario/php-dot-notation/blob/2.x/LICENSE.md (MIT License)
* @license https://github.com/adbario/php-dot-notation/blob/3.x/LICENSE.md (MIT License)
*/

use Adbar\Dot;
Expand All @@ -16,10 +16,11 @@
*
* @param mixed $items
* @param bool $parse
* @param non-empty-string $delimiter
* @return \Adbar\Dot<array-key, mixed>
*/
function dot($items, $parse = false)
function dot($items, $parse = false, $delimiter = ".")
{
return new Dot($items, $parse);
return new Dot($items, $parse, $delimiter);
}
}
40 changes: 35 additions & 5 deletions tests/DotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* @author Riku Särkinen <riku@adbar.io>
* @link https://github.com/adbario/php-dot-notation
* @license https://github.com/adbario/php-dot-notation/blob/2.x/LICENSE.md (MIT License)
* @license https://github.com/adbario/php-dot-notation/blob/3.x/LICENSE.md (MIT License)
*/

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

public function testConstructWithParsing(): void
{
$dot = new Dot(['foo.bar' => 'baz']);

$this->assertEquals(['foo.bar' => 'baz'], $dot->get());

$dot = new Dot(['foo.bar' => 'baz'], true);

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

public function testConstructWithCustomDelimiter(): void
{
$dot = new Dot(['foo_bar' => 'baz'], false, "_");

$this->assertEquals(['foo_bar' => 'baz'], $dot->get());

$dot = new Dot(['foo_bar' => 'baz'], true, "_");

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

public function testConstructHelper(): void
{
$dot = dot(['foo' => 'bar']);
Expand All @@ -58,13 +80,20 @@ public function testConstructHelper(): void
$this->assertEquals('bar', $dot->get('foo'));
}

public function testConstructWithParsing(): void
public function testConstructHelpertWithParsing(): void
{
$dot = new Dot(['foo.bar' => 'baz']);
$dot = dot(['foo.bar' => 'baz'], true);

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

$dot = new Dot(['foo.bar' => 'baz'], true);
public function testConstructHelpertWithCustomDelimiter(): void
{
$dot = dot(['foo_bar' => 'baz'], false, "_");

$this->assertEquals(['foo_bar' => 'baz'], $dot->get());

$dot = dot(['foo_bar' => 'baz'], true, "_");

$this->assertEquals(['foo' => ['bar' => 'baz']], $dot->get());
}
Expand Down Expand Up @@ -788,6 +817,7 @@ public function testVarExport(): void
" 'bar' => 'baz',\n" .
" ),\n" .
" ),\n" .
" 'delimiter' => '.',\n" .
"))",
var_export($dot, true)
);
Expand Down

0 comments on commit 6cab20b

Please sign in to comment.