Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 36 additions & 6 deletions src/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Data extends ArrayObject
{
public const LE = "\n";

private $delimiter = '.';

/**
* Class constructor
* @param array|string|false $data The data array
Expand Down Expand Up @@ -151,13 +153,14 @@ public function write(): string
* object that's nested under the current data object.
* Example: $data->find('parent-key.sub-key.sub-sub-key');
*
* @param string $key The key to search for. Can be composed using $separator as the key/su-bkey separator
* @param mixed $default The default value
* @param mixed $filter Filter returned value
* @param string $separator The separator to use when searching for sub keys. Default is '.'
* @param string $key The key to search for. Can be composed using $separator as the key/su-bkey
* separator
* @param mixed $default The default value
* @param mixed $filter Filter returned value
* @param string|null $separator The separator to use when searching for sub keys. Default is '$this->delimiter'
* @return mixed
*/
public function find(string $key, $default = null, $filter = null, string $separator = '.')
public function find(string $key, $default = null, $filter = null, ?string $separator = null)
{
$value = $this->get($key);

Expand All @@ -166,6 +169,10 @@ public function find(string $key, $default = null, $filter = null, string $separ
return self::filter($value, $filter);
}

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

// explode search key and init search data
if ('' === $separator) {
throw new Exception("Separator can't be empty");
Expand Down Expand Up @@ -305,7 +312,7 @@ public function offsetGet($key)
*/
public function is(string $key, $compareWith = true, bool $strictMode = false): bool
{
if (\strpos($key, '.') === false) {
if (\strpos($key, $this->delimiter) === false) {
$value = $this->get($key);
} else {
$value = $this->find($key);
Expand Down Expand Up @@ -453,4 +460,27 @@ public function findSelf(string $key, array $default = []): self
// @phpstan-ignore-next-line
return new static($default);
}

/**
* @param string $delimiter
* @return \JBZoo\Data\Data
*/
public function setDelimiter(string $delimiter): self
{
$delimiter = trim($delimiter);
if ($delimiter === '') {
throw new Exception("Delimiter can't be empty");
}

$this->delimiter = $delimiter;
return $this;
}

/**
* @return string
*/
public function getDelimiter(): string
{
return $this->delimiter;
}
}
14 changes: 14 additions & 0 deletions tests/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ protected function setUp(): void
'sub' => [
'qwerty' => 'deep-value',
],
'sub.qwerty' => 'ytrewq',
],

// pseudo nested
Expand Down Expand Up @@ -473,4 +474,17 @@ public function testEmptySeparator()

$data->find('array_not_empty.123', null, null, '');
}

public function testCustomDelimiter()
{
$data = new Data($this->test);
$data->setDelimiter(' | ');

isSame('|', $data->getDelimiter());

isSame('ytrewq', $data->find('nested.sub.qwerty'));
isSame('ytrewq', $data->get('nested.sub.qwerty'));
isSame('deep-value', $data->find('nested|sub|qwerty'));
isSame('ytrewq', $data->find('nested|sub.qwerty'));
}
}