From b4c0e9416aef687bae2ae2617e12bc1abf4459c2 Mon Sep 17 00:00:00 2001 From: Denis Smetannikov Date: Mon, 30 May 2022 19:28:16 +0200 Subject: [PATCH] Custom Separator --- src/Data.php | 42 ++++++++++++++++++++++++++++++++++++------ tests/DataTest.php | 14 ++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/Data.php b/src/Data.php index 5117b99..0096d72 100644 --- a/src/Data.php +++ b/src/Data.php @@ -32,6 +32,8 @@ class Data extends ArrayObject { public const LE = "\n"; + private $delimiter = '.'; + /** * Class constructor * @param array|string|false $data The data array @@ -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); @@ -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"); @@ -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); @@ -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; + } } diff --git a/tests/DataTest.php b/tests/DataTest.php index 67219de..df1da28 100644 --- a/tests/DataTest.php +++ b/tests/DataTest.php @@ -98,6 +98,7 @@ protected function setUp(): void 'sub' => [ 'qwerty' => 'deep-value', ], + 'sub.qwerty' => 'ytrewq', ], // pseudo nested @@ -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')); + } }