From edcedc80f8cc2ad694ed1ce189b9e646c9b7eaac Mon Sep 17 00:00:00 2001 From: microThread Date: Sun, 30 Dec 2018 15:10:59 +0200 Subject: [PATCH 01/37] [*]: Release version 1.0.5 --- .gitattributes | 2 + CHANGELOG.md | 10 + README.md | 19 +- composer.json | 17 +- phpcs.xml.dist | 4 +- phpstan.neon.dist | 2 + src/DotArray.php | 355 ++++----------------------- src/DotFilteringTrait.php | 272 ++++++++++++++++++++ src/DotPathTrait.php | 24 +- tests/Integration/BasicArrayTest.php | 26 +- 10 files changed, 393 insertions(+), 338 deletions(-) create mode 100644 phpstan.neon.dist create mode 100644 src/DotFilteringTrait.php diff --git a/.gitattributes b/.gitattributes index ada1786..037e546 100644 --- a/.gitattributes +++ b/.gitattributes @@ -15,6 +15,7 @@ *.bat text *.sql text *.yml text +*.neon text # Ensure those won't be messed up with @@ -31,6 +32,7 @@ /phpunit.xml export-ignore /phpcs.xml export-ignore /phpmd.xml export-ignore +/phpstan.neon export-ignore /.travis.yml export-ignore /.scrutinizer.yml export-ignore diff --git a/CHANGELOG.md b/CHANGELOG.md index 48e307a..df066db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,16 @@ DotArray Change Log ===================== +1.0.5 December 30, 2018 +----------------------------- + +- Refactoring DotArray: + - Using a Trait (DotFilteringTrait) to split code in more organized units. +- Refactoring DotPathTrait::flatten +- using PHPStan. +- Updating composer.json scripts to use PHPStan. +- More Tests. + 1.0.4 December 30, 2018 ----------------------------- diff --git a/README.md b/README.md index 28345dc..697534b 100644 --- a/README.md +++ b/README.md @@ -261,11 +261,16 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som 'a' => [ 'b' => 'value', ], - + 'b' => [ 1, 2, 3, + 'array' => [ + 1, + 2, + 3, + ] ], ] ); @@ -274,12 +279,14 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som /* The output will be an array: - [ - '{{a}}.{{b}}' => 'value', - '{{b}}.{{0}}' => 1, - '{{b}}.{{1}}' => 2, - '{{b}}.{{2}}' => 3, + '{a}.{b}' => 'value', + '{b}.{0}' => 1, + '{b}.{1}' => 2, + '{b}.{2}' => 3, + '{b}.{array}.{0}' => 1, + '{b}.{array}.{1}' => 2, + '{b}.{array}.{2}' => 3, ], */ ``` diff --git a/composer.json b/composer.json index 4ccf1fc..23331db 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "require-dev": { "squizlabs/php_codesniffer": "3.*", "phpunit/phpunit": "~7.0", - "phpmd/phpmd": "*" + "phpmd/phpmd": "*", + "phpstan/phpstan": "*" }, "suggest": { @@ -69,12 +70,14 @@ "scripts": { "check": [ "@cs-check", + "@phpstan", "@tests" ], "generate-reports": [ "@create-folders", "@cs-report", + "@phpstan-report", "@phpmd-report", "@tests-report-html", "@tests-report-xml", @@ -87,14 +90,16 @@ "cs-check": "phpcs", "cs-fix": "phpcbf", + "phpstan": "phpstan analyze src --no-progress", "phpmd": "phpmd src text phpmd.xml.dist", "tests": "phpunit", - "cs-report": "phpcs --report=json --report-file=build/phpcs-report.json || exit 0;", - "phpmd-report": "phpmd src xml phpmd.xml.dist --reportfile build/phpmd-report.xml || exit 0;", - "tests-report-html": "phpunit --coverage-html build/phpunit/coverage/html || exit 0;", - "tests-report-xml": "phpunit --coverage-xml build/phpunit/coverage/xml || exit 0;", - "tests-report-clover": "phpunit --coverage-clover build/phpunit/coverage/clover/index.xml || exit 0;" + "cs-report": "phpcs --report=json --report-file=build/phpcs-report.json || exit 0;", + "phpstan-report": "phpstan analyze src --error-format=checkstyle > build/phpstan-check-style.xml --no-progress || exit 0;", + "phpmd-report": "phpmd src xml phpmd.xml.dist --reportfile build/phpmd-report.xml || exit 0;", + "tests-report-html": "phpunit --coverage-html build/phpunit/coverage/html || exit 0;", + "tests-report-xml": "phpunit --coverage-xml build/phpunit/coverage/xml || exit 0;", + "tests-report-clover": "phpunit --coverage-clover build/phpunit/coverage/clover/index.xml || exit 0;" }, "scripts-descriptions": { diff --git a/phpcs.xml.dist b/phpcs.xml.dist index f03c800..749f954 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -1,7 +1,7 @@ - + - Base Coding Standards + Base Coding Standard diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..5f962ab --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,2 @@ +includes: + - vendor/phpstan/phpstan/conf/config.levelmax.neon \ No newline at end of file diff --git a/src/DotArray.php b/src/DotArray.php index bef16f1..9afd592 100644 --- a/src/DotArray.php +++ b/src/DotArray.php @@ -20,6 +20,7 @@ class DotArray implements /* Traits. */ use DotPathTrait; + use DotFilteringTrait; /** * Unique object identifier. @@ -116,101 +117,6 @@ protected static function mergeRecursive($array1, $array2 = null) } - /** - * List with internal operators and the associated callbacks. - * - * @return array - */ - protected static function operators() - { - return [ - [ - 'tokens' => ['=', '==', 'eq'], - 'closure' => function ($item, $property, $value) { - return $item[$property] == $value[0]; - }, - ], - - [ - 'tokens' => ['===', 'i'], - 'closure' => function ($item, $property, $value) { - return $item[$property] === $value[0]; - }, - ], - - [ - 'tokens' => ['!=', 'ne'], - 'closure' => function ($item, $property, $value) { - return $item[$property] != $value[0]; - }, - ], - - [ - 'tokens' => ['!==', 'ni'], - 'closure' => function ($item, $property, $value) { - return $item[$property] !== $value[0]; - }, - ], - - [ - 'tokens' => ['<', 'lt'], - 'closure' => function ($item, $property, $value) { - return $item[$property] < $value[0]; - }, - ], - - [ - 'tokens' => ['>', 'gt'], - 'closure' => function ($item, $property, $value) { - return $item[$property] > $value[0]; - }, - ], - - [ - 'tokens' => ['<=', 'lte'], - 'closure' => function ($item, $property, $value) { - return $item[$property] <= $value[0]; - }, - ], - - [ - 'tokens' => ['>=', 'gte'], - 'closure' => function ($item, $property, $value) { - return $item[$property] >= $value[0]; - }, - ], - - [ - 'tokens' => ['in', 'contains'], - 'closure' => function ($item, $property, $value) { - return \in_array($item[$property], (array) $value, true); - }, - ], - - [ - 'tokens' => ['not-in', 'not-contains'], - 'closure' => function ($item, $property, $value) { - return !\in_array($item[$property], (array) $value, true); - }, - ], - - [ - 'tokens' => ['between'], - 'closure' => function ($item, $property, $value) { - return ($item[$property] >= $value[0] && $item[$property] <= $value[1]); - }, - ], - - [ - 'tokens' => ['not-between'], - 'closure' => function ($item, $property, $value) { - return ($item[$property] < $value[0] || $item[$property] > $value[1]); - }, - ], - ]; - } - - /** * DotArray Constructor. * @@ -219,6 +125,8 @@ protected static function operators() public function __construct($items = []) { $this->items = static::normalize($items); + + $this->uniqueIdentifier(); } @@ -229,7 +137,6 @@ public function __destruct() { unset($this->uniqueIdentifier); unset($this->items); - unset($this->nestedPathPattern); } @@ -252,8 +159,7 @@ public function __invoke($key = null) public function uniqueIdentifier() { if (empty($this->uniqueIdentifier)) { - $this->uniqueIdentifier = \vsprintf( - '{%s}.{%s}.{%s}', + $this->uniqueIdentifier = static::segmentsToKey( [ static::class, \uniqid('', true), @@ -297,12 +203,12 @@ public function merge($array) /** - * @param string $key - * @param mixed $default + * @param string|null|mixed $key + * @param mixed $default * * @return array|mixed */ - protected function &read($key, $default) + protected function &read($key = null, $default = null) { $segments = static::pathToSegments($key); $items = &$this->items; @@ -340,10 +246,7 @@ protected function write($key, $value) $segment = $segments[$i]; if ( - ( - !isset($items[$segment]) - || !\is_array($items[$segment]) - ) + (!isset($items[$segment]) || !\is_array($items[$segment])) && ($i < ($count - 1)) ) { $items[$segment] = []; @@ -352,13 +255,15 @@ protected function write($key, $value) $items = &$items[$segment]; } - unset($segments, $count); - if (\is_array($value) || $value instanceof self) { $value = static::normalize($value); } $items = $value; + + if (!\is_array($this->items)) { + $this->items = static::normalize($this->items); + } } @@ -391,8 +296,6 @@ protected function remove($key) $items = &$items[$segment]; } - - unset($segments, $count); } @@ -445,12 +348,12 @@ public function get($key = null, $default = null) /** * Set the given value to the provided key or keys. * - * @param string|array $keys - * @param mixed $value + * @param null|string|array $keys + * @param mixed|mixed $value * * @return static */ - public function set($keys, $value) + public function set($keys = null, $value = []) { $keys = (array) (!isset($keys) ? [$keys] : $keys); @@ -485,7 +388,7 @@ public function delete($keys) * Set the contents of a given key or keys to the given value (default is empty array). * * @param null|string|array $keys - * @param array $value + * @param array|mixed $value * * @return static */ @@ -501,168 +404,6 @@ public function clear($keys = null, $value = []) } - /** - * Find the first item in an array that passes the truth test, otherwise return false - * The signature of the callable must be: `function ($value, $key)` - * - * @param \Closure $closure - * - * @return false|mixed - */ - public function find(\Closure $closure) - { - foreach ($this->items as $key => $value) { - if ($closure($value, $key)) { - if (\is_array($value)) { - $value = static::create($value); - } - - return $value; - } - } - - return false; - } - - - /** - * Use a callable function to filter through items. - * The signature of the callable must be: `function ($value, $key)` - * - * @param \Closure|null $closure - * @param int $flag Flag determining what arguments are sent to callback. - * ARRAY_FILTER_USE_KEY :: pass key as the only argument - * to callback. ARRAY_FILTER_USE_BOTH :: pass both value - * and key as arguments to callback. - * - * @return static - */ - public function filter(\Closure $closure = null, $flag = ARRAY_FILTER_USE_BOTH) - { - $items = $this->items; - - if (!isset($closure)) { - return static::create($items); - } - - return ( - static::create( - \array_values( - \array_filter( - $items, - $closure, - $flag - ) - ) - ) - ); - } - - - /** - * Allow to filter an array using one of the following comparison operators: - * - [ =, ==, eq (equal) ] - * - [ ===, i (identical) ] - * - [ !=, ne (not equal) ] - * - [ !==, ni (not identical) ] - * - [ <, lt (less than) ] - * - [ >, gr (greater than) ] - * - [ <=, lte (less than or equal to) ] - * - [ =>, gte (greater than or equal to) ] - * - [ in, contains ] - * - [ not-in, not-contains ] - * - [ between ] - * - [ not-between ] - * - * @param string $property - * @param string $comparisonOperator - * @param mixed $value - * - * @return static - */ - public function filterBy($property, $comparisonOperator, $value) - { - $args = \func_get_args(); - $value = (array) \array_slice($args, 2, \count($args)); - - $closure = null; - $operators = static::operators(); - - if (isset($value[0]) && \is_array($value[0])) { - $value = $value[0]; - } - - foreach ($operators as $entry) { - if (\in_array($comparisonOperator, $entry['tokens'])) { - $closure = function ($item) use ($entry, $property, $value) { - $item = (array) $item; - - if (!\array_key_exists($property, $item)) { - return false; - } - - return $entry['closure']($item, $property, $value); - }; - - break; - } - } - - return $this->filter($closure); - } - - - /** - * Filtering through array. - * The signature of the call can be: - * - where([property, comparisonOperator, ...value]) - * - where(\Closure) :: The signature of the callable must be: `function ($value, $key)` - * - where([\Closure]) :: The signature of the callable must be: `function ($value, $key)` - * - * Allowed comparison operators: - * - [ =, ==, eq (equal) ] - * - [ ===, i (identical) ] - * - [ !=, ne (not equal) ] - * - [ !==, ni (not identical) ] - * - [ <, lt (less than) ] - * - [ >, gr (greater than) ] - * - [ <=, lte (less than or equal to) ] - * - [ =>, gte (greater than or equal to) ] - * - [ in, contains ] - * - [ not-in, not-contains ] - * - [ between ] - * - [ not-between ] - * - * @param array|callable $criteria - * - * @return static - */ - public function where($criteria) - { - $criteria = (array) $criteria; - - if (empty($criteria)) { - return $this->filter(); - } - - $closure = \array_shift($criteria); - - if ($closure instanceof \Closure) { - return $this->filter($closure); - } - - $property = $closure; - $comparisonOperator = \array_shift($criteria); - $value = $criteria; - - if (isset($value[0]) && \is_array($value[0])) { - $value = $value[0]; - } - - return $this->filterBy($property, $comparisonOperator, $value); - } - - /** * Returning the first value from the current array. * @@ -768,38 +509,6 @@ public function count($mode = COUNT_NORMAL) } - /** - * @return array - */ - public function toArray() - { - return $this->items; - } - - - /** - * @param int $options - * - * @return string - */ - public function toJson($options = 0) - { - return \json_encode($this->items, $options); - } - - - /** - * Flatten the internal array using the dot delimiter, - * also the keys are wrapped inside {{key}} (2 x curly braces). - * - * @return array - */ - public function toFlat() - { - return static::flatten($this->items); - } - - /** * Specify data which should be serialized to JSON * @@ -863,4 +572,36 @@ public function getIterator() } + /** + * @return array + */ + public function toArray() + { + return $this->items; + } + + + /** + * @param int $options + * + * @return string + */ + public function toJson($options = 0) + { + return (string) \json_encode($this->items, $options); + } + + + /** + * Flatten the internal array using the dot delimiter, + * also the keys are wrapped inside {key} (1 x curly braces). + * + * @return array + */ + public function toFlat() + { + return static::flatten($this->items); + } + + } diff --git a/src/DotFilteringTrait.php b/src/DotFilteringTrait.php new file mode 100644 index 0000000..8cf3735 --- /dev/null +++ b/src/DotFilteringTrait.php @@ -0,0 +1,272 @@ + + * @license https://github.com/binary-cube/dot-array/blob/master/LICENSE + * @link https://github.com/binary-cube/dot-array + */ +trait DotFilteringTrait +{ + + + /** + * List with internal operators and the associated callbacks. + * + * @return array + */ + protected static function operators() + { + return [ + [ + 'tokens' => ['=', '==', 'eq'], + 'closure' => function ($item, $property, $value) { + return $item[$property] == $value[0]; + }, + ], + + [ + 'tokens' => ['===', 'i'], + 'closure' => function ($item, $property, $value) { + return $item[$property] === $value[0]; + }, + ], + + [ + 'tokens' => ['!=', 'ne'], + 'closure' => function ($item, $property, $value) { + return $item[$property] != $value[0]; + }, + ], + + [ + 'tokens' => ['!==', 'ni'], + 'closure' => function ($item, $property, $value) { + return $item[$property] !== $value[0]; + }, + ], + + [ + 'tokens' => ['<', 'lt'], + 'closure' => function ($item, $property, $value) { + return $item[$property] < $value[0]; + }, + ], + + [ + 'tokens' => ['>', 'gt'], + 'closure' => function ($item, $property, $value) { + return $item[$property] > $value[0]; + }, + ], + + [ + 'tokens' => ['<=', 'lte'], + 'closure' => function ($item, $property, $value) { + return $item[$property] <= $value[0]; + }, + ], + + [ + 'tokens' => ['>=', 'gte'], + 'closure' => function ($item, $property, $value) { + return $item[$property] >= $value[0]; + }, + ], + + [ + 'tokens' => ['in', 'contains'], + 'closure' => function ($item, $property, $value) { + return \in_array($item[$property], (array) $value, true); + }, + ], + + [ + 'tokens' => ['not-in', 'not-contains'], + 'closure' => function ($item, $property, $value) { + return !\in_array($item[$property], (array) $value, true); + }, + ], + + [ + 'tokens' => ['between'], + 'closure' => function ($item, $property, $value) { + return ($item[$property] >= $value[0] && $item[$property] <= $value[1]); + }, + ], + + [ + 'tokens' => ['not-between'], + 'closure' => function ($item, $property, $value) { + return ($item[$property] < $value[0] || $item[$property] > $value[1]); + }, + ], + ]; + } + + + /** + * Find the first item in an array that passes the truth test, otherwise return false + * The signature of the callable must be: `function ($value, $key)` + * + * @param \Closure $closure + * + * @return false|mixed + */ + public function find(\Closure $closure) + { + foreach ($this->items as $key => $value) { + if ($closure($value, $key)) { + if (\is_array($value)) { + $value = static::create($value); + } + + return $value; + } + } + + return false; + } + + + /** + * Use a callable function to filter through items. + * The signature of the callable must be: `function ($value, $key)` + * + * @param \Closure|null $closure + * @param int $flag Flag determining what arguments are sent to callback. + * ARRAY_FILTER_USE_KEY :: pass key as the only argument + * to callback. ARRAY_FILTER_USE_BOTH :: pass both value + * and key as arguments to callback. + * + * @return static + */ + public function filter(\Closure $closure = null, $flag = ARRAY_FILTER_USE_BOTH) + { + $items = $this->items; + + if (!isset($closure)) { + return static::create($items); + } + + return static::create( + \array_values( + \array_filter( + $items, + $closure, + $flag + ) + ) + ); + } + + + /** + * Allow to filter an array using one of the following comparison operators: + * - [ =, ==, eq (equal) ] + * - [ ===, i (identical) ] + * - [ !=, ne (not equal) ] + * - [ !==, ni (not identical) ] + * - [ <, lt (less than) ] + * - [ >, gr (greater than) ] + * - [ <=, lte (less than or equal to) ] + * - [ =>, gte (greater than or equal to) ] + * - [ in, contains ] + * - [ not-in, not-contains ] + * - [ between ] + * - [ not-between ] + * + * @param string $property + * @param string $comparisonOperator + * @param mixed $value + * + * @return static + */ + public function filterBy($property, $comparisonOperator, $value) + { + $args = \func_get_args(); + $value = \array_slice($args, 2, \count($args)); + + $closure = null; + $operators = static::operators(); + + if (isset($value[0]) && \is_array($value[0])) { + $value = $value[0]; + } + + foreach ($operators as $entry) { + if (\in_array($comparisonOperator, $entry['tokens'])) { + $closure = function ($item) use ($entry, $property, $value) { + $item = (array) $item; + + if (!\array_key_exists($property, $item)) { + return false; + } + + return $entry['closure']($item, $property, $value); + }; + + break; + } + } + + return $this->filter($closure); + } + + + /** + * Filtering through array. + * The signature of the call can be: + * - where([property, comparisonOperator, ...value]) + * - where(\Closure) :: The signature of the callable must be: `function ($value, $key)` + * - where([\Closure]) :: The signature of the callable must be: `function ($value, $key)` + * + * Allowed comparison operators: + * - [ =, ==, eq (equal) ] + * - [ ===, i (identical) ] + * - [ !=, ne (not equal) ] + * - [ !==, ni (not identical) ] + * - [ <, lt (less than) ] + * - [ >, gr (greater than) ] + * - [ <=, lte (less than or equal to) ] + * - [ =>, gte (greater than or equal to) ] + * - [ in, contains ] + * - [ not-in, not-contains ] + * - [ between ] + * - [ not-between ] + * + * @param array|callable $criteria + * + * @return static + */ + public function where($criteria) + { + $criteria = (array) $criteria; + + if (empty($criteria)) { + return $this->filter(); + } + + $closure = \array_shift($criteria); + + if ($closure instanceof \Closure) { + return $this->filter($closure); + } + + $property = $closure; + $comparisonOperator = \array_shift($criteria); + $value = $criteria; + + if (isset($value[0]) && \is_array($value[0])) { + $value = $value[0]; + } + + return $this->filterBy($property, $comparisonOperator, $value); + } + + +} diff --git a/src/DotPathTrait.php b/src/DotPathTrait.php index dd003b5..1ed4dbe 100644 --- a/src/DotPathTrait.php +++ b/src/DotPathTrait.php @@ -20,7 +20,7 @@ trait DotPathTrait */ protected static $dotPathConfig = [ 'template' => '#(?|(?|[](.*?)[])|(.*?))(?:$|\.+)#i', - 'wrapKey' => '{{%s}}', + 'wrapKey' => '{%s}', 'wildcards' => [ '' => ['\'', '\"', '\[', '\(', '\{'], '' => ['\'', '\"', '\]', '\)', '\}'], @@ -127,9 +127,11 @@ protected static function segmentsToKey(array $segments) { return ( \implode( - '', + '.', \array_map( - [static::class, 'wrapSegmentKey'], + function ($segment) { + return static::wrapSegmentKey($segment); + }, $segments ) ) @@ -139,33 +141,33 @@ protected static function segmentsToKey(array $segments) /** * Flatten the internal array using the dot delimiter, - * also the keys are wrapped inside {{key}} (2 x curly braces). + * also the keys are wrapped inside {key} (1 x curly braces). * - * @param array $items - * @param string $prepend + * @param array $items + * @param array $prepend * * @return array */ - protected static function flatten(array $items, $prepend = '') + protected static function flatten(array $items, $prepend = []) { $flatten = []; foreach ($items as $key => $value) { - $wrapKey = static::wrapSegmentKey($key); - if (\is_array($value) && !empty($value)) { $flatten = array_merge( $flatten, static::flatten( $value, - ($prepend . $wrapKey . '.') + array_merge($prepend, [$key]) ) ); continue; } - $flatten[$prepend . $wrapKey] = $value; + $segmentsToKey = static::segmentsToKey(array_merge($prepend, [$key])); + + $flatten[$segmentsToKey] = $value; } return $flatten; diff --git a/tests/Integration/BasicArrayTest.php b/tests/Integration/BasicArrayTest.php index deef0e1..d6469a5 100644 --- a/tests/Integration/BasicArrayTest.php +++ b/tests/Integration/BasicArrayTest.php @@ -158,6 +158,8 @@ public function testSet() self::assertIsInt($this->dot->get('new1.new2')); self::assertIsInt($this->dot['new1']['new2']); self::assertIsInt($this->dot['new1.new2']); + + self::assertSame([], $this->dot->set(null, null)->toArray()); } @@ -407,6 +409,7 @@ public function testCount() * Testing the Find Method. * * @covers \BinaryCube\DotArray\DotPathTrait + * @covers \BinaryCube\DotArray\DotFilteringTrait * @covers \BinaryCube\DotArray\DotArray::find * @covers \BinaryCube\DotArray\DotArray:: * @covers \BinaryCube\DotArray\DotArray:: @@ -447,6 +450,7 @@ function () { * Testing the Filter Method. * * @covers \BinaryCube\DotArray\DotPathTrait + * @covers \BinaryCube\DotArray\DotFilteringTrait * @covers \BinaryCube\DotArray\DotArray::filter * @covers \BinaryCube\DotArray\DotArray:: * @covers \BinaryCube\DotArray\DotArray:: @@ -472,6 +476,7 @@ function ($value) { * Testing the FilterBy Method. * * @covers \BinaryCube\DotArray\DotPathTrait + * @covers \BinaryCube\DotArray\DotFilteringTrait * @covers \BinaryCube\DotArray\DotArray::filterBy * @covers \BinaryCube\DotArray\DotArray:: * @covers \BinaryCube\DotArray\DotArray:: @@ -583,6 +588,7 @@ function ($value) { * Testing the Where Method. * * @covers \BinaryCube\DotArray\DotPathTrait + * @covers \BinaryCube\DotArray\DotFilteringTrait * @covers \BinaryCube\DotArray\DotArray::where * @covers \BinaryCube\DotArray\DotArray:: * @covers \BinaryCube\DotArray\DotArray:: @@ -709,7 +715,7 @@ public function testToJson() /** - * Testing the toFlatten Method. + * Testing the toFlat Method. * * @covers \BinaryCube\DotArray\DotPathTrait * @covers \BinaryCube\DotArray\DotPathTrait::flatten @@ -721,7 +727,7 @@ public function testToJson() * * @return void */ - public function testToFlatten() + public function testToFlat() { $dot = DotArray::create( [ @@ -733,16 +739,24 @@ public function testToFlatten() 1, 2, 3, + 'array' => [ + 1, + 2, + 3, + ] ], ] ); self::assertSame( [ - '{{a}}.{{b}}' => 'value', - '{{b}}.{{0}}' => 1, - '{{b}}.{{1}}' => 2, - '{{b}}.{{2}}' => 3, + '{a}.{b}' => 'value', + '{b}.{0}' => 1, + '{b}.{1}' => 2, + '{b}.{2}' => 3, + '{b}.{array}.{0}' => 1, + '{b}.{array}.{1}' => 2, + '{b}.{array}.{2}' => 3, ], $dot->toFlat() ); From be5d9bc04b5161d8f6d772c331fa99d7e17bab7e Mon Sep 17 00:00:00 2001 From: microThread Date: Sun, 30 Dec 2018 15:33:00 +0200 Subject: [PATCH 02/37] [+]: Sending PHPUnit clover to scrutinizer-ci --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index ecca587..3621a36 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,3 +20,5 @@ script: after_script: - bash <(curl -s https://codecov.io/bash) -f "build/phpunit/coverage/clover/index.xml" + - wget https://scrutinizer-ci.com/ocular.phar + - php ocular.phar code-coverage:upload --format=php-clover build/phpunit/coverage/clover/index.xml From 824d8f62710768f66a5ab1d45fcc4167b4ac109d Mon Sep 17 00:00:00 2001 From: microThread Date: Sun, 30 Dec 2018 18:26:44 +0200 Subject: [PATCH 03/37] [+]: Update CreateTest.php --- tests/Integration/CreateTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Integration/CreateTest.php b/tests/Integration/CreateTest.php index 4c33e78..b51d417 100644 --- a/tests/Integration/CreateTest.php +++ b/tests/Integration/CreateTest.php @@ -82,9 +82,15 @@ public function testCreate() ) ); + self::assertIsArray(DotArray::create(null)->toArray()); + self::assertIsArray(DotArray::create(1)->toArray()); + self::assertIsArray(DotArray::create([])->toArray()); + self::assertIsArray(DotArray::create(DotArray::create([]))->toArray()); + $dot = new DotArray($this->data); self::assertIsArray($dot->toArray()); + self::assertArrayHasKey('empty_array', $dot->toArray()); self::assertArrayHasKey('indexed_array', $dot->toArray()); self::assertArrayHasKey('assoc_array', $dot->toArray()); From e3fed73f94dbf1e6e7f23bff15f1b83dcf1d2eb0 Mon Sep 17 00:00:00 2001 From: microThread Date: Wed, 2 Jan 2019 11:00:13 +0200 Subject: [PATCH 04/37] [+]: Update DotArray.php::normalize (no more check for empty, just cast to array) [+]: Update README.md --- README.md | 58 +++++++++++++++++++++++++++++------------------- src/DotArray.php | 2 +- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 697534b..f7e6111 100644 --- a/README.md +++ b/README.md @@ -8,14 +8,13 @@ Accessing PHP Arrays via DOT notation is easy as: ```php DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{some.dotted.key}') ``` -[![Minimum PHP Version `PHP >= 7.1`](https://img.shields.io/badge/php-%3E%3D%207.1-8892BF.svg)][php-site] -[![Latest Stable Version](https://poser.pugx.org/binary-cube/dot-array/version)][packagist] -[![Total Downloads](https://poser.pugx.org/binary-cube/dot-array/downloads)][packagist] -[![Build Status](https://travis-ci.org/binary-cube/dot-array.svg?branch=master)](https://travis-ci.org/binary-cube/dot-array) -[![Codacy Badge](https://api.codacy.com/project/badge/Grade/003187f2016e4c4cb1b014ccc9bdb5c0)](https://www.codacy.com/app/microThread/dot-array) -[![Code Coverage](https://scrutinizer-ci.com/g/binary-cube/dot-array/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/binary-cube/dot-array/?branch=master) -[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/binary-cube/dot-array/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/binary-cube/dot-array/?branch=master) -[![License](https://poser.pugx.org/binary-cube/dot-array/license)][license] +[![Minimum PHP Version `PHP >= 7.1`][ico-php-require]][link-php-site] +[![Latest Stable Version][ico-version]][link-packagist] +[![Total Downloads][ico-downloads]][link-downloads] +[![Build Status][ico-travis]][link-travis] +[![Code Coverage][ico-scrutinizer]][link-scrutinizer] +[![Quality Score][ico-code-quality]][link-code-quality] +[![License][ico-license]][link-license] ----- @@ -365,7 +364,7 @@ $dummyArray = [ Have a bug or a feature request? Please first read the issue guidelines and search for existing and closed issues. -If your problem or idea is not addressed yet, [please open a new issue][new-issue]. +If your problem or idea is not addressed yet, [please open a new issue][link-new-issue]. @@ -379,14 +378,14 @@ In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. -[Read the full Code of Conduct][code-of-conduct]. +[Read the full Code of Conduct][link-code-of-conduct]. #### Versioning -Through the development of new versions, we're going use the [Semantic Versioning][semver]. +Through the development of new versions, we're going use the [Semantic Versioning][link-semver]. Example: `1.0.0`. - Major release: increment the first digit and reset middle and last digits to zero. Introduces major changes that might break backward compatibility. E.g. 2.0.0 @@ -400,25 +399,38 @@ Example: `1.0.0`. * **Banciu N. Cristian Mihai** -See also the list of [contributors][contributors] who participated in this project. +See also the list of [contributors][link-contributors] who participated in this project. ## License -This project is licensed under the MIT License - see the [LICENSE][license] file for details. +This project is licensed under the MIT License - see the [LICENSE][link-license] file for details. + -[domain]: https://binary-cube.com -[homepage]: https://binary-cube.com -[git-source]: https://github.com/binary-cube/dot-array -[php-site]: https://php.net -[semver]: https://semver.org -[code-of-conduct]: https://github.com/binary-cube/dot-array/blob/master/code-of-conduct.md -[license]: https://github.com/binary-cube/dot-array/blob/master/LICENSE -[contributors]: https://github.com/binary-cube/dot-array/graphs/contributors -[new-issue]: https://github.com/binary-cube/dot-array/issues/new -[packagist]: https://packagist.org/packages/binary-cube/dot-array +[ico-php-require]: https://img.shields.io/badge/php-%3E%3D%207.1-8892BF.svg?style=flat-square +[ico-version]: https://img.shields.io/packagist/v/binary-cube/dot-array.svg?style=flat-square +[ico-downloads]: https://img.shields.io/packagist/dt/binary-cube/dot-array.svg?style=flat-square +[ico-travis]: https://img.shields.io/travis/binary-cube/dot-array/master.svg?style=flat-square +[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/binary-cube/dot-array.svg?style=flat-square +[ico-code-quality]: https://img.shields.io/scrutinizer/g/binary-cube/dot-array.svg?style=flat-square +[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square + +[link-domain]: https://binary-cube.com +[link-homepage]: https://binary-cube.com +[link-git-source]: https://github.com/binary-cube/dot-array +[link-packagist]: https://packagist.org/packages/binary-cube/dot-array +[link-downloads]: https://packagist.org/packages/binary-cube/dot-array +[link-php-site]: https://php.net +[link-semver]: https://semver.org +[link-code-of-conduct]: https://github.com/binary-cube/dot-array/blob/master/code-of-conduct.md +[link-license]: https://github.com/binary-cube/dot-array/blob/master/LICENSE +[link-contributors]: https://github.com/binary-cube/dot-array/graphs/contributors +[link-new-issue]: https://github.com/binary-cube/dot-array/issues/new +[link-travis]: https://travis-ci.org/binary-cube/dot-array +[link-scrutinizer]: https://scrutinizer-ci.com/g/binary-cube/dot-array/code-structure +[link-code-quality]: https://scrutinizer-ci.com/g/binary-cube/dot-array diff --git a/src/DotArray.php b/src/DotArray.php index 9afd592..475fc92 100644 --- a/src/DotArray.php +++ b/src/DotArray.php @@ -83,7 +83,7 @@ protected static function normalize($items) } } - return (array) (empty($items) ? [] : $items); + return (array) $items; } From c9711ecedab02ca2b848f74236527b86349f8a57 Mon Sep 17 00:00:00 2001 From: microThread Date: Fri, 4 Jan 2019 19:41:49 +0200 Subject: [PATCH 05/37] [+]: Restructuring of tests folder. --- phpunit.xml.dist | 4 ++-- tests/{Integration => Unit}/ArrayDataProvider.php | 2 +- tests/{Integration => Unit}/BasicArrayTest.php | 3 +-- tests/{Integration => Unit}/CreateTest.php | 3 +-- tests/{ => Unit}/TestCase.php | 2 +- 5 files changed, 6 insertions(+), 8 deletions(-) rename tests/{Integration => Unit}/ArrayDataProvider.php (99%) rename tests/{Integration => Unit}/BasicArrayTest.php (99%) rename tests/{Integration => Unit}/CreateTest.php (97%) rename tests/{ => Unit}/TestCase.php (88%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 8b08e86..321e6af 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -14,8 +14,8 @@ processIsolation="false"> - - ./tests + + ./tests/Unit diff --git a/tests/Integration/ArrayDataProvider.php b/tests/Unit/ArrayDataProvider.php similarity index 99% rename from tests/Integration/ArrayDataProvider.php rename to tests/Unit/ArrayDataProvider.php index 77dbaea..940979f 100644 --- a/tests/Integration/ArrayDataProvider.php +++ b/tests/Unit/ArrayDataProvider.php @@ -1,6 +1,6 @@ Date: Sat, 5 Jan 2019 21:55:10 +0200 Subject: [PATCH 06/37] [+]: Update README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index f7e6111..3013c13 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,14 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som ``` - **merge**: + > Merges one or more arrays into master recursively. + If each array has an element with the same string key value, the latter + will overwrite the former (different from array_merge_recursive). + Recursive merging will be conducted if both arrays have an element of array + type and are having the same key. + For integer-keyed elements, the elements from the latter array will + be appended to the former array. + - ```php // Example 1. $dot->merge(['key_1' => ['some_key' => 'some_value']]); From f808639a8c84a37e755d97b027ec46be175e4bd0 Mon Sep 17 00:00:00 2001 From: microThread Date: Sat, 5 Jan 2019 23:09:07 +0200 Subject: [PATCH 07/37] [+]: Update README.md --- README.md | 49 +++++++++++++++++++++------------------ src/DotArray.php | 4 ++++ src/DotFilteringTrait.php | 2 +- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 3013c13..a694631 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,8 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som ``` - **clear** *(empty array <=> [])*: + > Set the contents of a given key or keys to the given value (default is empty array). + - ```php $dot->clear('books.{sci-fi & fantasy}'); $dot->clear('books.{sci-fi & fantasy}', null); @@ -117,12 +119,21 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som $dot['books.{sci-fi & fantasy}'] = []; ``` +- **delete** *(unset(...))*: + > Delete the given key or keys. + + - ```php + $dot->delete('books.{sci-fi & fantasy}'); + $dot->delete('books.{sci-fi & fantasy}.0.name'); + $dot->delete(['books.{sci-fi & fantasy}.0', 'books.{childre\'s books}.0']); + ``` + - **merge**: - > Merges one or more arrays into master recursively. + > Merges one or more arrays into master recursively.
If each array has an element with the same string key value, the latter - will overwrite the former (different from array_merge_recursive). + will overwrite the former (different from array_merge_recursive).
Recursive merging will be conducted if both arrays have an element of array - type and are having the same key. + type and are having the same key.
For integer-keyed elements, the elements from the latter array will be appended to the former array. @@ -144,30 +155,21 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som ); ``` -- **delete** *(unset(...))*: - - ```php - $dot->delete('books.{sci-fi & fantasy}'); - $dot->delete('books.{sci-fi & fantasy}.0.name'); - $dot->delete(['books.{sci-fi & fantasy}.0', 'books.{childre\'s books}.0']); - ``` - - **find**: + > Find the first item in an array that passes the truth test, otherwise return false.
+ The signature of the callable must be: `function ($value, $key)`. + - ```php - /* - Find the first item in an array that passes the truth test, otherwise return false - The signature of the callable must be: `function ($value, $key)`. - */ $book = $dot->get('books.{childre\'s books}')->find(function ($value, $key) { return $value['price'] > 0; }); ``` - **filter**: + > Use a callable function to filter through items.
+ The signature of the callable must be: `function ($value, $key)` + - ```php - /* - Use a callable function to filter through items. - The signature of the callable must be: `function ($value, $key)` - */ $books = $dot->get('books.{childre\'s books}')->filter(function ($value, $key) { return $value['name'] === 'Harry Potter and the Order of the Phoenix'; }); @@ -240,9 +242,9 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som ``` - **toArray**: - - ```php - // Getting the internal raw array. + > Getting the internal raw array. + - ```php // Example 1. $dot->toArray(); @@ -251,9 +253,9 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som ``` - **toJson**: - - ```php - // Getting the internal raw array as JSON. + > Getting the internal raw array as JSON. + - ```php // Example 1. $dot->toJson(); @@ -262,6 +264,9 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som ``` - **toFlat**: + > Flatten the internal array using the dot delimiter, + also the keys are wrapped inside {key} (1 x curly braces). + - ```php $dot = DotArray::create( [ diff --git a/src/DotArray.php b/src/DotArray.php index 475fc92..449b181 100644 --- a/src/DotArray.php +++ b/src/DotArray.php @@ -573,6 +573,8 @@ public function getIterator() /** + * Getting the internal raw array. + * * @return array */ public function toArray() @@ -582,6 +584,8 @@ public function toArray() /** + * Getting the internal raw array as JSON. + * * @param int $options * * @return string diff --git a/src/DotFilteringTrait.php b/src/DotFilteringTrait.php index 8cf3735..2bf4fe9 100644 --- a/src/DotFilteringTrait.php +++ b/src/DotFilteringTrait.php @@ -110,7 +110,7 @@ protected static function operators() /** - * Find the first item in an array that passes the truth test, otherwise return false + * Find the first item in an array that passes the truth test, otherwise return false. * The signature of the callable must be: `function ($value, $key)` * * @param \Closure $closure From 9d3e161d8c00b50cc9642c33200e2822fab72ae4 Mon Sep 17 00:00:00 2001 From: microThread Date: Sun, 24 Feb 2019 22:23:45 +0200 Subject: [PATCH 08/37] [-]: Remove DotPathTrait - parts have been moved into DotArray [*]: Code Standard Improvements --- phpcs.xml.dist | 10 ++- src/DotArray.php | 161 ++++++++++++++++++++++++++++++++++++++- src/DotPathTrait.php | 177 ------------------------------------------- 3 files changed, 167 insertions(+), 181 deletions(-) delete mode 100644 src/DotPathTrait.php diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 749f954..fa5b1bf 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -159,9 +159,11 @@ - + + + @@ -212,8 +214,10 @@ - - + + + error + diff --git a/src/DotArray.php b/src/DotArray.php index 449b181..8c666c2 100644 --- a/src/DotArray.php +++ b/src/DotArray.php @@ -19,9 +19,42 @@ class DotArray implements { /* Traits. */ - use DotPathTrait; use DotFilteringTrait; + /** + * Internal Dot Path Config. + * + * @var array + */ + protected static $dotPathConfig = [ + 'template' => '#(?|(?|[](.*?)[])|(.*?))(?:$|\.+)#i', + 'wrapKey' => '{%s}', + 'wildcards' => [ + '' => ['\'', '\"', '\[', '\(', '\{'], + '' => ['\'', '\"', '\]', '\)', '\}'], + ], + ]; + + /** + * The cached pattern that allow to match the JSON paths that use the dot notation. + * + * Allowed tokens for more complex paths: '', "", [], (), {} + * Examples: + * + * - foo.bar + * - foo.'bar' + * - foo."bar" + * - foo.[bar] + * - foo.(bar) + * - foo.{bar} + * + * Or more complex: + * - foo.{bar}.[component].{version.1.0} + * + * @var string + */ + protected static $dotPathPattern; + /** * Unique object identifier. * @@ -61,6 +94,132 @@ public static function createFromJson($json) } + /** + * Getting the dot path pattern. + * + * @return string + */ + protected static function dotPathPattern() + { + if (empty(self::$dotPathPattern)) { + $path = self::$dotPathConfig['template']; + + foreach (self::$dotPathConfig['wildcards'] as $wildcard => $tokens) { + $path = \str_replace($wildcard, \implode('', $tokens), $path); + } + + self::$dotPathPattern = $path; + } + + return self::$dotPathPattern; + } + + + /** + * Converts dot string path to segments. + * + * @param string $path + * + * @return array + */ + protected static function pathToSegments($path) + { + $path = \trim($path, " \t\n\r\0\x0B\."); + $segments = []; + $matches = []; + + if (\mb_strlen($path, 'UTF-8') === 0) { + return []; + } + + \preg_match_all(static::dotPathPattern(), $path, $matches); + + if (!empty($matches[1])) { + $matches = $matches[1]; + + $segments = \array_filter( + $matches, + function ($match) { + return (\mb_strlen($match, 'UTF-8') > 0); + } + ); + } + + unset($path, $matches); + + return $segments; + } + + + /** + * Wrap a given string into special characters. + * + * @param string $key + * + * @return string + */ + protected static function wrapSegmentKey($key) + { + return vsprintf(static::$dotPathConfig['wrapKey'], [$key]); + } + + + /** + * @param array $segments + * + * @return string + */ + protected static function segmentsToKey(array $segments) + { + return ( + \implode( + '.', + \array_map( + function ($segment) { + return static::wrapSegmentKey($segment); + }, + $segments + ) + ) + ); + } + + + /** + * Flatten the internal array using the dot delimiter, + * also the keys are wrapped inside {key} (1 x curly braces). + * + * @param array $items + * @param array $prepend + * + * @return array + */ + protected static function flatten(array $items, $prepend = []) + { + $flatten = []; + + foreach ($items as $key => $value) { + if (\is_array($value) && !empty($value)) { + $flatten = array_merge( + $flatten, + static::flatten( + $value, + array_merge($prepend, [$key]) + ) + ); + + continue; + } + + $segmentsToKey = static::segmentsToKey(array_merge($prepend, [$key])); + + $flatten[$segmentsToKey] = $value; + } + + return $flatten; + } + + /** * Return the given items as an array * diff --git a/src/DotPathTrait.php b/src/DotPathTrait.php deleted file mode 100644 index 1ed4dbe..0000000 --- a/src/DotPathTrait.php +++ /dev/null @@ -1,177 +0,0 @@ - - * @license https://github.com/binary-cube/dot-array/blob/master/LICENSE - * @link https://github.com/binary-cube/dot-array - */ -trait DotPathTrait -{ - - /** - * Internal Dot Path Config. - * - * @var array - */ - protected static $dotPathConfig = [ - 'template' => '#(?|(?|[](.*?)[])|(.*?))(?:$|\.+)#i', - 'wrapKey' => '{%s}', - 'wildcards' => [ - '' => ['\'', '\"', '\[', '\(', '\{'], - '' => ['\'', '\"', '\]', '\)', '\}'], - ], - ]; - - /** - * The cached pattern that allow to match the JSON paths that use the dot notation. - * - * Allowed tokens for more complex paths: '', "", [], (), {} - * Examples: - * - * - foo.bar - * - foo.'bar' - * - foo."bar" - * - foo.[bar] - * - foo.(bar) - * - foo.{bar} - * - * Or more complex: - * - foo.{bar}.[component].{version.1.0} - * - * @var string - */ - protected static $dotPathPattern; - - - /** - * Getting the dot path pattern. - * - * @return string - */ - protected static function dotPathPattern() - { - if (empty(self::$dotPathPattern)) { - $path = self::$dotPathConfig['template']; - - foreach (self::$dotPathConfig['wildcards'] as $wildcard => $tokens) { - $path = \str_replace($wildcard, \implode('', $tokens), $path); - } - - self::$dotPathPattern = $path; - } - - return self::$dotPathPattern; - } - - - /** - * Converts dot string path to segments. - * - * @param string $path - * - * @return array - */ - protected static function pathToSegments($path) - { - $path = \trim($path, " \t\n\r\0\x0B\."); - $segments = []; - $matches = []; - - if (\mb_strlen($path, 'UTF-8') === 0) { - return []; - } - - \preg_match_all(static::dotPathPattern(), $path, $matches); - - if (!empty($matches[1])) { - $matches = $matches[1]; - - $segments = \array_filter( - $matches, - function ($match) { - return (\mb_strlen($match, 'UTF-8') > 0); - } - ); - } - - unset($path, $matches); - - return $segments; - } - - - /** - * Wrap a given string into special characters. - * - * @param string $key - * - * @return string - */ - protected static function wrapSegmentKey($key) - { - return vsprintf(static::$dotPathConfig['wrapKey'], [$key]); - } - - - /** - * @param array $segments - * - * @return string - */ - protected static function segmentsToKey(array $segments) - { - return ( - \implode( - '.', - \array_map( - function ($segment) { - return static::wrapSegmentKey($segment); - }, - $segments - ) - ) - ); - } - - - /** - * Flatten the internal array using the dot delimiter, - * also the keys are wrapped inside {key} (1 x curly braces). - * - * @param array $items - * @param array $prepend - * - * @return array - */ - protected static function flatten(array $items, $prepend = []) - { - $flatten = []; - - foreach ($items as $key => $value) { - if (\is_array($value) && !empty($value)) { - $flatten = array_merge( - $flatten, - static::flatten( - $value, - array_merge($prepend, [$key]) - ) - ); - - continue; - } - - $segmentsToKey = static::segmentsToKey(array_merge($prepend, [$key])); - - $flatten[$segmentsToKey] = $value; - } - - return $flatten; - } - - -} From edf6bf84026b219fb4be7029f05eb979adfbc8e5 Mon Sep 17 00:00:00 2001 From: microThread Date: Mon, 25 Feb 2019 08:24:38 +0200 Subject: [PATCH 09/37] [*]: Fix Unit Testing --- tests/Unit/BasicArrayTest.php | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/tests/Unit/BasicArrayTest.php b/tests/Unit/BasicArrayTest.php index 506414b..ea7890d 100644 --- a/tests/Unit/BasicArrayTest.php +++ b/tests/Unit/BasicArrayTest.php @@ -65,7 +65,6 @@ public function setUp() /** * Testing the Get Method. * - * @covers \BinaryCube\DotArray\DotPathTrait * @covers \BinaryCube\DotArray\DotArray::get * @covers \BinaryCube\DotArray\DotArray::toArray * @covers \BinaryCube\DotArray\DotArray:: @@ -107,7 +106,6 @@ public function testGet() /** * Testing the Set Method. * - * @covers \BinaryCube\DotArray\DotPathTrait * @covers \BinaryCube\DotArray\DotArray::set * @covers \BinaryCube\DotArray\DotArray::toArray * @covers \BinaryCube\DotArray\DotArray:: @@ -165,7 +163,6 @@ public function testSet() /** * Testing the Has Method. * - * @covers \BinaryCube\DotArray\DotPathTrait * @covers \BinaryCube\DotArray\DotArray::has * @covers \BinaryCube\DotArray\DotArray:: * @covers \BinaryCube\DotArray\DotArray:: @@ -196,7 +193,6 @@ public function testHas() /** * Testing the isEmpty Method. * - * @covers \BinaryCube\DotArray\DotPathTrait * @covers \BinaryCube\DotArray\DotArray::isEmpty * @covers \BinaryCube\DotArray\DotArray:: * @covers \BinaryCube\DotArray\DotArray:: @@ -227,7 +223,6 @@ public function testIsEmpty() /** * Testing the Delete Method. * - * @covers \BinaryCube\DotArray\DotPathTrait * @covers \BinaryCube\DotArray\DotArray::delete * @covers \BinaryCube\DotArray\DotArray:: * @covers \BinaryCube\DotArray\DotArray:: @@ -263,7 +258,6 @@ public function testDelete() /** * Testing the Clear Method. * - * @covers \BinaryCube\DotArray\DotPathTrait * @covers \BinaryCube\DotArray\DotArray::clear * @covers \BinaryCube\DotArray\DotArray:: * @covers \BinaryCube\DotArray\DotArray:: @@ -292,7 +286,6 @@ public function testClear() /** * Testing the Merge Method. * - * @covers \BinaryCube\DotArray\DotPathTrait * @covers \BinaryCube\DotArray\DotArray::merge * @covers \BinaryCube\DotArray\DotArray:: * @covers \BinaryCube\DotArray\DotArray:: @@ -385,7 +378,6 @@ public function testMerge() /** * Testing the Count Method. * - * @covers \BinaryCube\DotArray\DotPathTrait * @covers \BinaryCube\DotArray\DotArray::count * @covers \BinaryCube\DotArray\DotArray:: * @covers \BinaryCube\DotArray\DotArray:: @@ -407,7 +399,6 @@ public function testCount() /** * Testing the Find Method. * - * @covers \BinaryCube\DotArray\DotPathTrait * @covers \BinaryCube\DotArray\DotFilteringTrait * @covers \BinaryCube\DotArray\DotArray::find * @covers \BinaryCube\DotArray\DotArray:: @@ -448,7 +439,6 @@ function () { /** * Testing the Filter Method. * - * @covers \BinaryCube\DotArray\DotPathTrait * @covers \BinaryCube\DotArray\DotFilteringTrait * @covers \BinaryCube\DotArray\DotArray::filter * @covers \BinaryCube\DotArray\DotArray:: @@ -474,7 +464,6 @@ function ($value) { /** * Testing the FilterBy Method. * - * @covers \BinaryCube\DotArray\DotPathTrait * @covers \BinaryCube\DotArray\DotFilteringTrait * @covers \BinaryCube\DotArray\DotArray::filterBy * @covers \BinaryCube\DotArray\DotArray:: @@ -586,7 +575,6 @@ function ($value) { /** * Testing the Where Method. * - * @covers \BinaryCube\DotArray\DotPathTrait * @covers \BinaryCube\DotArray\DotFilteringTrait * @covers \BinaryCube\DotArray\DotArray::where * @covers \BinaryCube\DotArray\DotArray:: @@ -661,7 +649,6 @@ function ($value) { /** * Testing the First Method. * - * @covers \BinaryCube\DotArray\DotPathTrait * @covers \BinaryCube\DotArray\DotArray::first * @covers \BinaryCube\DotArray\DotArray:: * @covers \BinaryCube\DotArray\DotArray:: @@ -716,10 +703,9 @@ public function testToJson() /** * Testing the toFlat Method. * - * @covers \BinaryCube\DotArray\DotPathTrait - * @covers \BinaryCube\DotArray\DotPathTrait::flatten - * @covers \BinaryCube\DotArray\DotPathTrait::dotPathPattern - * @covers \BinaryCube\DotArray\DotPathTrait::wrapSegmentKey + * @covers \BinaryCube\DotArray\DotArray::flatten + * @covers \BinaryCube\DotArray\DotArray::dotPathPattern + * @covers \BinaryCube\DotArray\DotArray::wrapSegmentKey * @covers \BinaryCube\DotArray\DotArray::toFlat * @covers \BinaryCube\DotArray\DotArray:: * @covers \BinaryCube\DotArray\DotArray:: From b4369db4de97b8c744d8a01c39d10a76b2132057 Mon Sep 17 00:00:00 2001 From: microThread Date: Mon, 25 Feb 2019 08:42:39 +0200 Subject: [PATCH 10/37] [*]: Minor checks [*]: Minor checks --- src/DotArray.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/DotArray.php b/src/DotArray.php index 8c666c2..c47958c 100644 --- a/src/DotArray.php +++ b/src/DotArray.php @@ -147,7 +147,7 @@ function ($match) { unset($path, $matches); - return $segments; + return (empty($segments) ? [] : $segments); } @@ -351,9 +351,11 @@ public function merge($array) [ $this, 'mergeRecursive', ], - \array_merge( - [$this->items], - \func_get_args() + \array_values( + \array_merge( + [$this->items], + \func_get_args() + ) ) ); From d832137c12b0a5c91f8c212d8cfa0fefd881dc63 Mon Sep 17 00:00:00 2001 From: microThread Date: Mon, 25 Feb 2019 09:22:13 +0200 Subject: [PATCH 11/37] [-]: Remove 1 space before & after function --- phpcs.xml.dist | 2 +- src/DotArray.php | 37 -------------------------------- src/DotFilteringTrait.php | 6 ------ tests/Unit/ArrayDataProvider.php | 2 -- tests/Unit/BasicArrayTest.php | 21 ------------------ tests/Unit/CreateTest.php | 3 --- 6 files changed, 1 insertion(+), 70 deletions(-) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index fa5b1bf..13cacf5 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -424,7 +424,7 @@ --> - + diff --git a/src/DotArray.php b/src/DotArray.php index c47958c..42a00f9 100644 --- a/src/DotArray.php +++ b/src/DotArray.php @@ -69,7 +69,6 @@ class DotArray implements */ protected $items; - /** * Creates an DotArray object. * @@ -82,7 +81,6 @@ public static function create($items) return (new static($items)); } - /** * @param string $json * @@ -93,7 +91,6 @@ public static function createFromJson($json) return static::create(\json_decode($json, true)); } - /** * Getting the dot path pattern. * @@ -114,7 +111,6 @@ protected static function dotPathPattern() return self::$dotPathPattern; } - /** * Converts dot string path to segments. * @@ -150,7 +146,6 @@ function ($match) { return (empty($segments) ? [] : $segments); } - /** * Wrap a given string into special characters. * @@ -163,7 +158,6 @@ protected static function wrapSegmentKey($key) return vsprintf(static::$dotPathConfig['wrapKey'], [$key]); } - /** * @param array $segments * @@ -184,7 +178,6 @@ function ($segment) { ); } - /** * Flatten the internal array using the dot delimiter, * also the keys are wrapped inside {key} (1 x curly braces). @@ -219,7 +212,6 @@ protected static function flatten(array $items, $prepend = []) return $flatten; } - /** * Return the given items as an array * @@ -245,7 +237,6 @@ protected static function normalize($items) return (array) $items; } - /** * @param array|DotArray|mixed $array1 * @param null|array|DotArray|mixed $array2 @@ -275,7 +266,6 @@ protected static function mergeRecursive($array1, $array2 = null) return $res; } - /** * DotArray Constructor. * @@ -288,7 +278,6 @@ public function __construct($items = []) $this->uniqueIdentifier(); } - /** * DotArray Destructor. */ @@ -298,7 +287,6 @@ public function __destruct() unset($this->items); } - /** * Call object as function. * @@ -311,7 +299,6 @@ public function __invoke($key = null) return $this->get($key); } - /** * @return string */ @@ -330,7 +317,6 @@ public function uniqueIdentifier() return $this->uniqueIdentifier; } - /** * Merges one or more arrays into master recursively. * If each array has an element with the same string key value, the latter @@ -362,7 +348,6 @@ public function merge($array) return $this; } - /** * @param string|null|mixed $key * @param mixed $default @@ -390,7 +375,6 @@ protected function &read($key = null, $default = null) return $items; } - /** * @param string $key * @param mixed $value @@ -427,7 +411,6 @@ protected function write($key, $value) } } - /** * Delete the given key or keys. * @@ -459,7 +442,6 @@ protected function remove($key) } } - /** * @param string $key * @@ -472,7 +454,6 @@ public function has($key) return ($identifier !== $this->read($key, $identifier)); } - /** * Check if a given key contains empty values (null, [], 0, false) * @@ -487,7 +468,6 @@ public function isEmpty($key = null) return empty($items); } - /** * @param null|string $key * @param null|mixed $default @@ -505,7 +485,6 @@ public function get($key = null, $default = null) return $items; } - /** * Set the given value to the provided key or keys. * @@ -525,7 +504,6 @@ public function set($keys = null, $value = []) return $this; } - /** * Delete the given key or keys. * @@ -544,7 +522,6 @@ public function delete($keys) return $this; } - /** * Set the contents of a given key or keys to the given value (default is empty array). * @@ -564,7 +541,6 @@ public function clear($keys = null, $value = []) return $this; } - /** * Returning the first value from the current array. * @@ -577,7 +553,6 @@ public function first() return \array_shift($items); } - /** * Whether a offset exists * @@ -595,7 +570,6 @@ public function offsetExists($offset) return $this->has($offset); } - /** * Offset to retrieve * @@ -612,7 +586,6 @@ public function &offsetGet($offset) return $this->read($offset, null); } - /** * Offset to set * @@ -630,7 +603,6 @@ public function offsetSet($offset, $value) $this->write($offset, $value); } - /** * Offset to unset * @@ -652,7 +624,6 @@ public function offsetUnset($offset) $this->remove($offset); } - /** * Count elements of an object * @@ -669,7 +640,6 @@ public function count($mode = COUNT_NORMAL) return \count($this->items, $mode); } - /** * Specify data which should be serialized to JSON * @@ -685,7 +655,6 @@ public function jsonSerialize() return $this->items; } - /** * String representation of object * @@ -700,7 +669,6 @@ public function serialize() return \serialize($this->items); } - /** * Constructs the object * @@ -717,7 +685,6 @@ public function unserialize($serialized) $this->items = \unserialize($serialized); } - /** * Retrieve an external iterator. * @@ -732,7 +699,6 @@ public function getIterator() return new \ArrayIterator($this->items); } - /** * Getting the internal raw array. * @@ -743,7 +709,6 @@ public function toArray() return $this->items; } - /** * Getting the internal raw array as JSON. * @@ -756,7 +721,6 @@ public function toJson($options = 0) return (string) \json_encode($this->items, $options); } - /** * Flatten the internal array using the dot delimiter, * also the keys are wrapped inside {key} (1 x curly braces). @@ -768,5 +732,4 @@ public function toFlat() return static::flatten($this->items); } - } diff --git a/src/DotFilteringTrait.php b/src/DotFilteringTrait.php index 2bf4fe9..6e55514 100644 --- a/src/DotFilteringTrait.php +++ b/src/DotFilteringTrait.php @@ -13,7 +13,6 @@ trait DotFilteringTrait { - /** * List with internal operators and the associated callbacks. * @@ -108,7 +107,6 @@ protected static function operators() ]; } - /** * Find the first item in an array that passes the truth test, otherwise return false. * The signature of the callable must be: `function ($value, $key)` @@ -132,7 +130,6 @@ public function find(\Closure $closure) return false; } - /** * Use a callable function to filter through items. * The signature of the callable must be: `function ($value, $key)` @@ -164,7 +161,6 @@ public function filter(\Closure $closure = null, $flag = ARRAY_FILTER_USE_BOTH) ); } - /** * Allow to filter an array using one of the following comparison operators: * - [ =, ==, eq (equal) ] @@ -217,7 +213,6 @@ public function filterBy($property, $comparisonOperator, $value) return $this->filter($closure); } - /** * Filtering through array. * The signature of the call can be: @@ -268,5 +263,4 @@ public function where($criteria) return $this->filterBy($property, $comparisonOperator, $value); } - } diff --git a/tests/Unit/ArrayDataProvider.php b/tests/Unit/ArrayDataProvider.php index 940979f..129a3ba 100644 --- a/tests/Unit/ArrayDataProvider.php +++ b/tests/Unit/ArrayDataProvider.php @@ -13,7 +13,6 @@ class ArrayDataProvider { - /** * @return array */ @@ -161,5 +160,4 @@ public static function get() return $array; } - } diff --git a/tests/Unit/BasicArrayTest.php b/tests/Unit/BasicArrayTest.php index ea7890d..e729ba2 100644 --- a/tests/Unit/BasicArrayTest.php +++ b/tests/Unit/BasicArrayTest.php @@ -37,7 +37,6 @@ class BasicArrayTest extends TestCase */ protected $dot; - /** * Setup the test env. * @@ -61,7 +60,6 @@ public function setUp() parent::setUp(); } - /** * Testing the Get Method. * @@ -102,7 +100,6 @@ public function testGet() self::assertIsString($this->dot['mixed_array.hello-world.{Nǐ hǎo}']); } - /** * Testing the Set Method. * @@ -159,7 +156,6 @@ public function testSet() self::assertSame([], $this->dot->set(null, null)->toArray()); } - /** * Testing the Has Method. * @@ -189,7 +185,6 @@ public function testHas() self::assertNotTrue($this->dot->has('a.b.c.d')); } - /** * Testing the isEmpty Method. * @@ -219,7 +214,6 @@ public function testIsEmpty() self::assertIsBool($this->dot->get('dotObject')->isEmpty()); } - /** * Testing the Delete Method. * @@ -254,7 +248,6 @@ public function testDelete() self::assertTrue(array_key_exists('one', $this->dot['assoc_array'])); } - /** * Testing the Clear Method. * @@ -282,7 +275,6 @@ public function testClear() self::assertEmpty($users->toArray()); } - /** * Testing the Merge Method. * @@ -374,7 +366,6 @@ public function testMerge() self::assertCount(3, $this->dot->get('mixed_array.{👋.🤘.some-key}.config.memcached.servers')); } - /** * Testing the Count Method. * @@ -395,7 +386,6 @@ public function testCount() self::assertEquals(1, count($this->dot['assoc_array']['three'])); } - /** * Testing the Find Method. * @@ -435,7 +425,6 @@ function () { ); } - /** * Testing the Filter Method. * @@ -460,7 +449,6 @@ function ($value) { self::assertSame([1, 2, 3, 4], $under->toArray()); } - /** * Testing the FilterBy Method. * @@ -571,7 +559,6 @@ function ($value) { ); } - /** * Testing the Where Method. * @@ -645,7 +632,6 @@ function ($value) { self::assertSame($user1, $users->toArray()); } - /** * Testing the First Method. * @@ -663,7 +649,6 @@ public function testFirst() ); } - /** * Testing the toArray Method. * @@ -679,7 +664,6 @@ public function testToArray() self::assertSame($this->data, $this->dot->toArray()); } - /** * Testing the toJson Method. * @@ -699,7 +683,6 @@ public function testToJson() self::assertSame($this->jsonArray, $decode); } - /** * Testing the toFlat Method. * @@ -747,7 +730,6 @@ public function testToFlat() ); } - /** * Testing the serialize & unserialize Methods. * @@ -774,7 +756,6 @@ public function testSerializable() self::assertInstanceOf(DotArray::class, $unserialize); } - /** * Testing the jsonSerialize Methods. * @@ -787,7 +768,6 @@ public function testJsonSerialize() self::assertSame($this->data, $this->dot->jsonSerialize()); } - /** * Testing the getIterator Methods. * @@ -802,5 +782,4 @@ public function testIterator() self::assertSame($this->data, $this->dot->getIterator()->getArrayCopy()); } - } diff --git a/tests/Unit/CreateTest.php b/tests/Unit/CreateTest.php index 6d96b21..7b477e9 100644 --- a/tests/Unit/CreateTest.php +++ b/tests/Unit/CreateTest.php @@ -27,7 +27,6 @@ class CreateTest extends TestCase */ protected $jsonString; - /** * Setup the test env. * @@ -41,7 +40,6 @@ public function setUp() parent::setUp(); } - /** * Testing Object. * @@ -101,5 +99,4 @@ public function testCreate() self::assertArrayHasKey('a', $dot->toArray()); } - } From af13b7462148a7a030aeafbfb6930b616fe770fa Mon Sep 17 00:00:00 2001 From: microThread Date: Tue, 26 Feb 2019 08:23:38 +0200 Subject: [PATCH 12/37] [*]: Minor refactoring in code [+]: More tests --- src/DotArray.php | 160 ++++++++++++---------------------- tests/Unit/BasicArrayTest.php | 70 ++++++++++++++- 2 files changed, 126 insertions(+), 104 deletions(-) diff --git a/src/DotArray.php b/src/DotArray.php index 42a00f9..07531e6 100644 --- a/src/DotArray.php +++ b/src/DotArray.php @@ -21,46 +21,10 @@ class DotArray implements /* Traits. */ use DotFilteringTrait; - /** - * Internal Dot Path Config. - * - * @var array - */ - protected static $dotPathConfig = [ - 'template' => '#(?|(?|[](.*?)[])|(.*?))(?:$|\.+)#i', - 'wrapKey' => '{%s}', - 'wildcards' => [ - '' => ['\'', '\"', '\[', '\(', '\{'], - '' => ['\'', '\"', '\]', '\)', '\}'], - ], - ]; - - /** - * The cached pattern that allow to match the JSON paths that use the dot notation. - * - * Allowed tokens for more complex paths: '', "", [], (), {} - * Examples: - * - * - foo.bar - * - foo.'bar' - * - foo."bar" - * - foo.[bar] - * - foo.(bar) - * - foo.{bar} - * - * Or more complex: - * - foo.{bar}.[component].{version.1.0} - * - * @var string - */ - protected static $dotPathPattern; - - /** - * Unique object identifier. - * - * @var string - */ - protected $uniqueIdentifier; + private const TEMPLATE_PATTERN = '#(?|(?|[%s](.*?)[%s])|(.*?))(?:$|\.+)#i'; + private const WRAP_KEY = '{%s}'; + private const TOKEN_START = ['\'', '\"', '\[', '\(', '\{']; + private const TOKEN_END = ['\'', '\"', '\]', '\)', '\}']; /** * Stores the original data. @@ -92,23 +56,34 @@ public static function createFromJson($json) } /** - * Getting the dot path pattern. + * Getting the path pattern. + * + * Allowed tokens for more complex paths: '', "", [], (), {} + * Examples: + * + * - foo.bar + * - foo.'bar' + * - foo."bar" + * - foo.[bar] + * - foo.(bar) + * - foo.{bar} + * + * Or more complex: + * - foo.{bar}.[component].{version.1.0} * * @return string */ - protected static function dotPathPattern() + protected static function pathPattern() { - if (empty(self::$dotPathPattern)) { - $path = self::$dotPathConfig['template']; - - foreach (self::$dotPathConfig['wildcards'] as $wildcard => $tokens) { - $path = \str_replace($wildcard, \implode('', $tokens), $path); - } - - self::$dotPathPattern = $path; - } - - return self::$dotPathPattern; + return ( + vsprintf( + self::TEMPLATE_PATTERN, + [ + \implode('', self::TOKEN_START), + \implode('', self::TOKEN_END), + ] + ) + ); } /** @@ -118,7 +93,7 @@ protected static function dotPathPattern() * * @return array */ - protected static function pathToSegments($path) + final protected static function pathToSegments($path) { $path = \trim($path, " \t\n\r\0\x0B\."); $segments = []; @@ -128,7 +103,7 @@ protected static function pathToSegments($path) return []; } - \preg_match_all(static::dotPathPattern(), $path, $matches); + \preg_match_all(self::pathPattern(), $path, $matches); if (!empty($matches[1])) { $matches = $matches[1]; @@ -153,9 +128,9 @@ function ($match) { * * @return string */ - protected static function wrapSegmentKey($key) + final protected static function wrapSegmentKey($key) { - return vsprintf(static::$dotPathConfig['wrapKey'], [$key]); + return vsprintf(self::WRAP_KEY, [$key]); } /** @@ -163,18 +138,18 @@ protected static function wrapSegmentKey($key) * * @return string */ - protected static function segmentsToKey(array $segments) + final protected static function segmentsToKey(array $segments) { return ( - \implode( - '.', - \array_map( - function ($segment) { - return static::wrapSegmentKey($segment); - }, - $segments + \implode( + '.', + \array_map( + function ($segment) { + return self::wrapSegmentKey($segment); + }, + $segments + ) ) - ) ); } @@ -187,7 +162,7 @@ function ($segment) { * * @return array */ - protected static function flatten(array $items, $prepend = []) + final protected static function flatten(array $items, $prepend = []) { $flatten = []; @@ -195,7 +170,7 @@ protected static function flatten(array $items, $prepend = []) if (\is_array($value) && !empty($value)) { $flatten = array_merge( $flatten, - static::flatten( + self::flatten( $value, array_merge($prepend, [$key]) ) @@ -204,7 +179,7 @@ protected static function flatten(array $items, $prepend = []) continue; } - $segmentsToKey = static::segmentsToKey(array_merge($prepend, [$key])); + $segmentsToKey = self::segmentsToKey(array_merge($prepend, [$key])); $flatten[$segmentsToKey] = $value; } @@ -219,7 +194,7 @@ protected static function flatten(array $items, $prepend = []) * * @return array */ - protected static function normalize($items) + final protected static function normalize($items) { if ($items instanceof self) { $items = $items->toArray(); @@ -228,7 +203,7 @@ protected static function normalize($items) if (\is_array($items)) { foreach ($items as $k => $v) { if (\is_array($v) || $v instanceof self) { - $v = static::normalize($v); + $v = self::normalize($v); } $items[$k] = $v; } @@ -243,9 +218,9 @@ protected static function normalize($items) * * @return array */ - protected static function mergeRecursive($array1, $array2 = null) + final protected static function mergeRecursive($array1, $array2 = null) { - $args = static::normalize(\func_get_args()); + $args = self::normalize(\func_get_args()); $res = \array_shift($args); while (!empty($args)) { @@ -256,7 +231,7 @@ protected static function mergeRecursive($array1, $array2 = null) } if (\is_array($v) && isset($res[$k]) && \is_array($res[$k])) { - $v = static::mergeRecursive($res[$k], $v); + $v = self::mergeRecursive($res[$k], $v); } $res[$k] = $v; @@ -273,9 +248,7 @@ protected static function mergeRecursive($array1, $array2 = null) */ public function __construct($items = []) { - $this->items = static::normalize($items); - - $this->uniqueIdentifier(); + $this->items = self::normalize($items); } /** @@ -283,7 +256,6 @@ public function __construct($items = []) */ public function __destruct() { - unset($this->uniqueIdentifier); unset($this->items); } @@ -299,24 +271,6 @@ public function __invoke($key = null) return $this->get($key); } - /** - * @return string - */ - public function uniqueIdentifier() - { - if (empty($this->uniqueIdentifier)) { - $this->uniqueIdentifier = static::segmentsToKey( - [ - static::class, - \uniqid('', true), - \microtime(true), - ] - ); - } - - return $this->uniqueIdentifier; - } - /** * Merges one or more arrays into master recursively. * If each array has an element with the same string key value, the latter @@ -356,7 +310,7 @@ public function merge($array) */ protected function &read($key = null, $default = null) { - $segments = static::pathToSegments($key); + $segments = self::pathToSegments($key); $items = &$this->items; foreach ($segments as $segment) { @@ -383,7 +337,7 @@ protected function &read($key = null, $default = null) */ protected function write($key, $value) { - $segments = static::pathToSegments($key); + $segments = self::pathToSegments($key); $count = \count($segments); $items = &$this->items; @@ -401,13 +355,13 @@ protected function write($key, $value) } if (\is_array($value) || $value instanceof self) { - $value = static::normalize($value); + $value = self::normalize($value); } $items = $value; if (!\is_array($this->items)) { - $this->items = static::normalize($this->items); + $this->items = self::normalize($this->items); } } @@ -420,7 +374,7 @@ protected function write($key, $value) */ protected function remove($key) { - $segments = static::pathToSegments($key); + $segments = self::pathToSegments($key); $count = \count($segments); $items = &$this->items; @@ -449,7 +403,7 @@ protected function remove($key) */ public function has($key) { - $identifier = $this->uniqueIdentifier(); + $identifier = \uniqid(static::class, true); return ($identifier !== $this->read($key, $identifier)); } @@ -729,7 +683,7 @@ public function toJson($options = 0) */ public function toFlat() { - return static::flatten($this->items); + return self::flatten($this->items); } } diff --git a/tests/Unit/BasicArrayTest.php b/tests/Unit/BasicArrayTest.php index e729ba2..7ed1b37 100644 --- a/tests/Unit/BasicArrayTest.php +++ b/tests/Unit/BasicArrayTest.php @@ -60,6 +60,74 @@ public function setUp() parent::setUp(); } + /** + * @return array + */ + public function tokens() + { + $array = [ + 'a' => [ + 'b' => [ + 'c' => true, + ], + ], + ]; + + return [ + [ + 'path' => 'a.b.c', + 'array' => $array, + ], + [ + 'path' => "'a'.'b'.'c'", + 'array' => $array, + ], + [ + 'path' => '"a"."b"."c"', + 'array' => $array, + ], + [ + 'path' => '[a].[b].[c]', + 'array' => $array, + ], + [ + 'path' => '(a).(b).(c)', + 'array' => $array, + ], + [ + 'path' => '{a}.{b}.{c}', + 'array' => $array, + ], + [ + 'path' => '[a].(b).{c}', + 'array' => $array, + ], + ]; + } + + /** + * Testing different patterns. + * + * @param string $path The query pattern + * @param array $array The array + * + * @covers \BinaryCube\DotArray\DotArray::get + * @covers \BinaryCube\DotArray\DotArray::toArray + * @covers \BinaryCube\DotArray\DotArray:: + * @covers \BinaryCube\DotArray\DotArray:: + * + * @dataProvider tokens + * + * @return void + */ + public function testTokens($path, $array) + { + $dot = DotArray::create($array); + + self::assertInstanceOf(DotArray::class, $dot); + self::assertTrue($dot->get($path)); + } + /** * Testing the Get Method. * @@ -687,7 +755,7 @@ public function testToJson() * Testing the toFlat Method. * * @covers \BinaryCube\DotArray\DotArray::flatten - * @covers \BinaryCube\DotArray\DotArray::dotPathPattern + * @covers \BinaryCube\DotArray\DotArray::pathPattern * @covers \BinaryCube\DotArray\DotArray::wrapSegmentKey * @covers \BinaryCube\DotArray\DotArray::toFlat * @covers \BinaryCube\DotArray\DotArray:: From e0561b08477ccfe9af7075578835e3eca7d8f9b4 Mon Sep 17 00:00:00 2001 From: microThread Date: Wed, 27 Feb 2019 08:46:24 +0200 Subject: [PATCH 13/37] [*]: Minor improvements --- src/DotArray.php | 36 ++++++++---------------------------- 1 file changed, 8 insertions(+), 28 deletions(-) diff --git a/src/DotArray.php b/src/DotArray.php index 07531e6..2ee36fb 100644 --- a/src/DotArray.php +++ b/src/DotArray.php @@ -130,7 +130,7 @@ function ($match) { */ final protected static function wrapSegmentKey($key) { - return vsprintf(self::WRAP_KEY, [$key]); + return \vsprintf(self::WRAP_KEY, [$key]); } /** @@ -168,18 +168,11 @@ final protected static function flatten(array $items, $prepend = []) foreach ($items as $key => $value) { if (\is_array($value) && !empty($value)) { - $flatten = array_merge( - $flatten, - self::flatten( - $value, - array_merge($prepend, [$key]) - ) - ); - + $flatten = \array_merge($flatten, self::flatten($value, \array_merge($prepend, [$key]))); continue; } - $segmentsToKey = self::segmentsToKey(array_merge($prepend, [$key])); + $segmentsToKey = self::segmentsToKey(\array_merge($prepend, [$key])); $flatten[$segmentsToKey] = $value; } @@ -291,12 +284,7 @@ public function merge($array) [ $this, 'mergeRecursive', ], - \array_values( - \array_merge( - [$this->items], - \func_get_args() - ) - ) + \array_values(\array_merge([$this->items], \func_get_args())) ); return $this; @@ -314,10 +302,7 @@ protected function &read($key = null, $default = null) $items = &$this->items; foreach ($segments as $segment) { - if ( - !\is_array($items) - || !\array_key_exists($segment, $items) - ) { + if (!\is_array($items) || !\array_key_exists($segment, $items)) { return $default; } @@ -381,12 +366,10 @@ protected function remove($key) for ($i = 0; $i < $count; $i++) { $segment = $segments[$i]; - // Nothing to unset. if (!\array_key_exists($segment, $items)) { break; } - // Last item, time to unset. if ($i === ($count - 1)) { unset($items[$segment]); break; @@ -417,9 +400,7 @@ public function has($key) */ public function isEmpty($key = null) { - $items = $this->read($key, null); - - return empty($items); + return empty($this->read($key, null)); } /** @@ -497,14 +478,13 @@ public function clear($keys = null, $value = []) /** * Returning the first value from the current array. + * False otherwise, in case the list is empty. * * @return mixed */ public function first() { - $items = $this->items; - - return \array_shift($items); + return \reset($this->items); } /** From 076cc6d60061ea8fb7f07c471172f46860b40e57 Mon Sep 17 00:00:00 2001 From: microThread Date: Thu, 28 Feb 2019 08:37:01 +0200 Subject: [PATCH 14/37] [*]: Remove commented sniff's [*]: Fix misspelling in README.md --- README.md | 24 ++++++++++++------------ phpcs.xml.dist | 6 ++---- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index a694631..ae3b6ba 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som // Multiple keys. $dot->clear([ 'books.{sci-fi & fantasy}', - 'books.{childre\'s books}' + 'books.{children\'s books}' ]); // Vanilla PHP. @@ -125,7 +125,7 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som - ```php $dot->delete('books.{sci-fi & fantasy}'); $dot->delete('books.{sci-fi & fantasy}.0.name'); - $dot->delete(['books.{sci-fi & fantasy}.0', 'books.{childre\'s books}.0']); + $dot->delete(['books.{sci-fi & fantasy}.0', 'books.{children\'s books}.0']); ``` - **merge**: @@ -160,7 +160,7 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som The signature of the callable must be: `function ($value, $key)`. - ```php - $book = $dot->get('books.{childre\'s books}')->find(function ($value, $key) { + $book = $dot->get('books.{children\'s books}')->find(function ($value, $key) { return $value['price'] > 0; }); ``` @@ -170,7 +170,7 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som The signature of the callable must be: `function ($value, $key)` - ```php - $books = $dot->get('books.{childre\'s books}')->filter(function ($value, $key) { + $books = $dot->get('books.{children\'s books}')->filter(function ($value, $key) { return $value['name'] === 'Harry Potter and the Order of the Phoenix'; }); @@ -195,13 +195,13 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som - [ not-between ] */ // Example 1. - $books = $dot->get('books.{childre\'s books}')->filterBy('price', 'between', 5, 12); + $books = $dot->get('books.{children\'s books}')->filterBy('price', 'between', 5, 12); // Example 2. - $books = $dot->get('books.{childre\'s books}')->filterBy('price', '>', 10); + $books = $dot->get('books.{children\'s books}')->filterBy('price', '>', 10); // Example 3. - $books = $dot->get('books.{childre\'s books}')->filterBy('price', 'in', [8.5, 15.49]); + $books = $dot->get('books.{children\'s books}')->filterBy('price', 'in', [8.5, 15.49]); ``` - **where**: @@ -227,16 +227,16 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som */ // Example 1. (using the signature: [property, comparisonOperator, ...value]) - $books = $dot->get('books.{childre\'s books}')->where(['price', 'between', 5, 12]); + $books = $dot->get('books.{children\'s books}')->where(['price', 'between', 5, 12]); // Example 2. (using the signature: [property, comparisonOperator, ...value]) - $books = $dot->get('books.{childre\'s books}')->where(['price', '>', 10]); + $books = $dot->get('books.{children\'s books}')->where(['price', '>', 10]); // Example 3. (using the signature: [property, comparisonOperator, ...value]) - $books = $dot->get('books.{childre\'s books}')->where(['price', 'in', [8.5, 15.49]]); + $books = $dot->get('books.{children\'s books}')->where(['price', 'in', [8.5, 15.49]]); // Example 4. (using the signature: \Closure) - $books = $dot->get('books.{childre\'s books}')->where(function ($value, $key) { + $books = $dot->get('books.{children\'s books}')->where(function ($value, $key) { return $value['name'] === 'Harry Potter and the Order of the Phoenix'; }); ``` @@ -337,7 +337,7 @@ $dummyArray = [ ], ], - 'childre\'s books' => + 'children\'s books' => [ [ 'name' => 'Harry Potter and the Order of the Phoenix', diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 13cacf5..74d1ea3 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -12,12 +12,12 @@ */vendor/* - + - + @@ -156,8 +156,6 @@ - - From b8faa6ad002f1bdef6f80e36b5a597683c74f856 Mon Sep 17 00:00:00 2001 From: microThread Date: Thu, 28 Feb 2019 22:37:22 +0200 Subject: [PATCH 15/37] Create PULL_REQUEST_TEMPLATE.md --- .github/PULL_REQUEST_TEMPLATE.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .github/PULL_REQUEST_TEMPLATE.md diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..968a845 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,7 @@ +| Q | A +| ------------- | --- +| Is bugfix? | yes/no +| New feature? | yes/no +| Breaks BC? | yes/no +| Tests pass? | yes/no +| Fixed issues | comma-separated list of tickets # fixed by the PR, if any From 98d53136598425b585c4448996ef1e1da4e397ba Mon Sep 17 00:00:00 2001 From: microThread Date: Fri, 1 Mar 2019 07:42:18 +0200 Subject: [PATCH 16/37] Update .gitattributes --- .gitattributes | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitattributes b/.gitattributes index 037e546..a3648c5 100644 --- a/.gitattributes +++ b/.gitattributes @@ -26,6 +26,7 @@ # Ignore some meta files when creating an archive of this repository +/.github export-ignore /.editorconfig export-ignore /.gitattributes export-ignore /.gitignore export-ignore From 786836e030ed22670a6940b5d20017c2d1fec05e Mon Sep 17 00:00:00 2001 From: microThread Date: Fri, 1 Mar 2019 07:51:35 +0200 Subject: [PATCH 17/37] Update issue templates --- .github/ISSUE_TEMPLATE/bug_report.md | 23 +++++++++++++++++++++++ .github/ISSUE_TEMPLATE/feature_request.md | 20 ++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md create mode 100644 .github/ISSUE_TEMPLATE/feature_request.md diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..134ec49 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,23 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: bug +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: +1. Go to '...' +2. Execute .. +3. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..bbcbbe7 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: '' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. From 91f64dde74cbb737a3b14707334b2b1fcaa0d4f9 Mon Sep 17 00:00:00 2001 From: microThread Date: Fri, 1 Mar 2019 07:53:56 +0200 Subject: [PATCH 18/37] Rename bug_report.md to BUG_REPORT.md --- .github/ISSUE_TEMPLATE/{bug_report.md => BUG_REPORT.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/ISSUE_TEMPLATE/{bug_report.md => BUG_REPORT.md} (100%) diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/BUG_REPORT.md similarity index 100% rename from .github/ISSUE_TEMPLATE/bug_report.md rename to .github/ISSUE_TEMPLATE/BUG_REPORT.md From b87bc931157a0a6ce0dc7543a0f0ce3e82c5a5ad Mon Sep 17 00:00:00 2001 From: microThread Date: Fri, 1 Mar 2019 07:54:26 +0200 Subject: [PATCH 19/37] Rename feature_request.md to FEATURE_REQUEST.md --- .github/ISSUE_TEMPLATE/{feature_request.md => FEATURE_REQUEST.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/ISSUE_TEMPLATE/{feature_request.md => FEATURE_REQUEST.md} (100%) diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md similarity index 100% rename from .github/ISSUE_TEMPLATE/feature_request.md rename to .github/ISSUE_TEMPLATE/FEATURE_REQUEST.md From 6dd32e9c5a7409d335deec1a616699f102a66b89 Mon Sep 17 00:00:00 2001 From: microThread Date: Sat, 2 Mar 2019 09:27:11 +0200 Subject: [PATCH 20/37] [*]: Release version 1.1.0 --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index df066db..6993dcd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ DotArray Change Log ===================== +1.1.0 March 02, 2019 +----------------------------- +- Remove DotArray::uniqueIdentifier +- Remove DotPathTrait - parts have been moved into DotArray +- Code Standard Improvements +- Refactoring DotArray: +- More Tests. + 1.0.5 December 30, 2018 ----------------------------- From 49f1b6b97f4a17242c49b94afbea51f332988dbf Mon Sep 17 00:00:00 2001 From: microThread Date: Mon, 4 Mar 2019 19:57:48 +0200 Subject: [PATCH 21/37] [*]: CS Fix [*]: Update title/description in README.md & composer.json --- README.md | 2 +- composer.json | 4 ++-- phpcs.xml.dist | 3 --- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ae3b6ba..fa542b3 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# DotArray - Sail through array using the dot notation +# PHP Dot-Array :: Sail through array using the dot notation

~ Enjoy your :coffee: ~

diff --git a/composer.json b/composer.json index 23331db..46eff2e 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,9 @@ { "name": "binary-cube/dot-array", - "description": "Navigate through array using JSON Dot notation path.", + "description": "PHP Dot-Array :: Sail through array using the dot notation", "keywords": [ - "DotArray", "dot-array", "array", "dot", "php-array", "php-array-json-path" + "DotArray", "dot-array", "array", "dot", "php-array", "php-array-json-path", "php" ], "type": "library", diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 74d1ea3..a33c136 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -229,9 +229,6 @@ - - - From 1ce6bdfc112a38cd058855044b33d4d4b4ec82b9 Mon Sep 17 00:00:00 2001 From: microThread Date: Fri, 3 May 2019 15:14:54 +0300 Subject: [PATCH 22/37] [*]: CS Updates --- phpcs.xml.dist | 1 + 1 file changed, 1 insertion(+) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index a33c136..f73483f 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -162,6 +162,7 @@ + From bdc5fe9bceeaecb3c3ab7c9a3d7347c3f5e3874f Mon Sep 17 00:00:00 2001 From: microThread Date: Tue, 18 Jun 2019 20:13:23 +0300 Subject: [PATCH 23/37] Create .readthedocs.yml --- .readthedocs.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .readthedocs.yml diff --git a/.readthedocs.yml b/.readthedocs.yml new file mode 100644 index 0000000..a36db8a --- /dev/null +++ b/.readthedocs.yml @@ -0,0 +1,23 @@ +# .readthedocs.yml +# Read the Docs configuration file +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Build documentation in the docs/ directory with Sphinx +sphinx: + configuration: docs/conf.py + +# Build documentation with MkDocs +#mkdocs: +# configuration: mkdocs.yml + +# Optionally build your docs in additional formats such as PDF and ePub +formats: all + +# Optionally set the version of Python and requirements required to build your docs +python: + version: 3.7 + install: + - requirements: docs/requirements.txt From b600b9ac8f5edeed5aea75f4056877a9286dc424 Mon Sep 17 00:00:00 2001 From: microThread Date: Tue, 18 Jun 2019 20:16:31 +0300 Subject: [PATCH 24/37] Create requirements.txt --- docs/requirements.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 docs/requirements.txt diff --git a/docs/requirements.txt b/docs/requirements.txt new file mode 100644 index 0000000..38e3db8 --- /dev/null +++ b/docs/requirements.txt @@ -0,0 +1,2 @@ +Sphinx>=1.8.0 +sphinx_rtd_theme>=0.4.0 From 38c8b096c623d48686aebb9a32ab28ca44ab52b1 Mon Sep 17 00:00:00 2001 From: microThread Date: Tue, 18 Jun 2019 20:40:00 +0300 Subject: [PATCH 25/37] Create conf.py --- docs/conf.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 docs/conf.py diff --git a/docs/conf.py b/docs/conf.py new file mode 100644 index 0000000..ddc6d67 --- /dev/null +++ b/docs/conf.py @@ -0,0 +1,54 @@ +# Configuration file for the Sphinx documentation builder. +# +# This file only contains a selection of the most common options. For a full +# list see the documentation: +# http://www.sphinx-doc.org/en/master/config + +# -- Path setup -------------------------------------------------------------- + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +# +# import os +# import sys +# sys.path.insert(0, os.path.abspath('.')) + + +# -- Project information ----------------------------------------------------- + +project = 'DotArray' +copyright = '2019, Banciu N. Cristian Mihai' + +# The full version, including alpha/beta/rc tags +release = '1.0' + + +# -- General configuration --------------------------------------------------- + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = [ +] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +# This pattern also affects html_static_path and html_extra_path. +exclude_patterns = ['_build'] + + +# -- Options for HTML output ------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +# +html_theme = 'sphinx_rtd_theme' + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] From 00b4800da9232f177badba73ef1d700ae456dee4 Mon Sep 17 00:00:00 2001 From: microThread Date: Tue, 18 Jun 2019 20:48:10 +0300 Subject: [PATCH 26/37] Update requirements.txt --- docs/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/requirements.txt b/docs/requirements.txt index 38e3db8..57880fa 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,2 +1,2 @@ -Sphinx>=1.8.0 +Sphinx>=2.1.0 sphinx_rtd_theme>=0.4.0 From 56aef7deab3c7b012b390dc04f4f11f46bc7b258 Mon Sep 17 00:00:00 2001 From: microThread Date: Tue, 18 Jun 2019 20:50:35 +0300 Subject: [PATCH 27/37] Create index.rst --- docs/index.rst | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 docs/index.rst diff --git a/docs/index.rst b/docs/index.rst new file mode 100644 index 0000000..3751eea --- /dev/null +++ b/docs/index.rst @@ -0,0 +1,5 @@ +.. title:: PHP Dot-Array :: Sail through array using the dot notation + +==================== +Dot-Array +==================== From b414188fd64b8d1a2ad234ba0c9c32195ed73176 Mon Sep 17 00:00:00 2001 From: microThread Date: Tue, 18 Jun 2019 20:52:48 +0300 Subject: [PATCH 28/37] Update conf.py --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index ddc6d67..ea2b18b 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -17,7 +17,7 @@ # -- Project information ----------------------------------------------------- -project = 'DotArray' +project = 'Dot-Array' copyright = '2019, Banciu N. Cristian Mihai' # The full version, including alpha/beta/rc tags From 470e8c452fbc494a7c5529b75f2b9687168523a9 Mon Sep 17 00:00:00 2001 From: microThread Date: Tue, 18 Jun 2019 20:54:27 +0300 Subject: [PATCH 29/37] Update conf.py --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index ea2b18b..ddc6d67 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -17,7 +17,7 @@ # -- Project information ----------------------------------------------------- -project = 'Dot-Array' +project = 'DotArray' copyright = '2019, Banciu N. Cristian Mihai' # The full version, including alpha/beta/rc tags From 9e2cfc0a5c52ac1844643ccde99e663f0671b44e Mon Sep 17 00:00:00 2001 From: microThread Date: Tue, 18 Jun 2019 21:56:01 +0300 Subject: [PATCH 30/37] Update index.rst --- docs/index.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 3751eea..77ee93f 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -1,5 +1,5 @@ -.. title:: PHP Dot-Array :: Sail through array using the dot notation +.. title:: PHP DotArray Library:: Sail through array using the DOT notation ==================== -Dot-Array +DotArray ==================== From 67bb5c018d27f18f98d51eb1abdf889b1c0ccee5 Mon Sep 17 00:00:00 2001 From: microThread Date: Tue, 18 Jun 2019 21:56:15 +0300 Subject: [PATCH 31/37] Create overview.rst --- docs/overview.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/overview.rst diff --git a/docs/overview.rst b/docs/overview.rst new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/docs/overview.rst @@ -0,0 +1 @@ + From 8823b58e7fdfae1b24857024425b6702913b0bf5 Mon Sep 17 00:00:00 2001 From: microThread Date: Tue, 18 Jun 2019 21:57:13 +0300 Subject: [PATCH 32/37] Create quickstart.rst --- docs/quickstart.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/quickstart.rst diff --git a/docs/quickstart.rst b/docs/quickstart.rst new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/docs/quickstart.rst @@ -0,0 +1 @@ + From 0c2cb6c982366592a12a2983a995fd835044dfde Mon Sep 17 00:00:00 2001 From: microThread Date: Thu, 20 Jun 2019 09:57:24 +0300 Subject: [PATCH 33/37] [*]: Documentation for Sphinx (part 1) --- docs/conf.py | 16 +++++++ docs/index.rst | 21 +++++++++ docs/overview.rst | 107 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) diff --git a/docs/conf.py b/docs/conf.py index ddc6d67..fe09287 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -30,6 +30,7 @@ # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ + 'sphinxcontrib.phpdomain' ] # Add any paths that contain templates here, relative to this directory. @@ -52,3 +53,18 @@ # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] + + +# -- Custom ------------------------------------------------- +import sphinx_rtd_theme +html_theme = "sphinx_rtd_theme" +html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] + +# Set up PHP syntax highlights +from sphinx.highlighting import lexers +from pygments.lexers.web import PhpLexer + +lexers["php"] = PhpLexer(startinline=True, linenos=1) +lexers["php-annotations"] = PhpLexer(startinline=True, linenos=1) +primary_domain = "php" + diff --git a/docs/index.rst b/docs/index.rst index 77ee93f..272ca40 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -3,3 +3,24 @@ ==================== DotArray ==================== + +DotArray is a simple PHP Library that can access an array in a dotted manner. + +- Easy to use. +- Support to access complex keys that are having dot in the name. +- Fluent access. +- Possibility to search & filter items. +- Dot access can, also, works in the "old school" array way (Vanilla PHP). + +.. code-block:: php + + DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{some.dotted.key}') + +User Guide +========== + +.. toctree:: + :maxdepth: 2 + + overview + quickstart diff --git a/docs/overview.rst b/docs/overview.rst index 8b13789..3915814 100644 --- a/docs/overview.rst +++ b/docs/overview.rst @@ -1 +1,108 @@ +======== +Overview +======== +Requirements +============ + +#. PHP 7.1 + +.. _installation: + + +Installation +============ + +The recommended way to install DotArray is with +`Composer `_. Composer is a dependency management tool +for PHP that allows you to declare the dependencies your project needs and +installs them into your project. + +.. code-block:: bash + + # Install Composer + curl -sS https://getcomposer.org/installer | php + +You can add DotArray as a dependency using the composer.phar CLI: + +.. code-block:: bash + + php composer.phar require binary-cube/dot-array + +Alternatively, you can specify DotArray as a dependency in your project's +existing composer.json file: + +.. code-block:: js + + { + "require": { + "binary-cube/dot-array": "*" + } + } + +After installing, you need to require Composer's autoloader: + +.. code-block:: php + + require 'vendor/autoload.php'; + +You can find out more on how to install Composer, configure autoloading, and +other best-practices for defining dependencies at `getcomposer.org `_. + + +License +======= + +Licensed using the `MIT license `_. + + Copyright (c) 2018 Binary Cube + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + + +Bugs and feature requests +========================= + +Have a bug or a feature request? +Please first read the issue guidelines and search for existing and closed issues. +If your problem or idea is not addressed yet, `please open a new issue `_. + + +Contributing +============ + +All contributions are more than welcomed. +Contributions may close an issue, fix a bug (reported or not reported), add new design blocks, +improve the existing code, add new feature, and so on. +In the interest of fostering an open and welcoming environment, +we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, +regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, +personal appearance, race, religion, or sexual identity and orientation. +`Read the full Code of Conduct `_. + + +Versioning +=========== + +Through the development of new versions, we're going use the `Semantic Versioning `_. + +Example: `1.0.0`. +- Major release: increment the first digit and reset middle and last digits to zero. Introduces major changes that might break backward compatibility. E.g. 2.0.0 +- Minor release: increment the middle digit and reset last digit to zero. It would fix bugs and also add new features without breaking backward compatibility. E.g. 1.1.0 +- Patch release: increment the third digit. It would fix bugs and keep backward compatibility. E.g. 1.0.1 From 36aaa13c5a5cc82ac2517530e9e4f4a76ee1d302 Mon Sep 17 00:00:00 2001 From: microThread Date: Thu, 20 Jun 2019 10:02:59 +0300 Subject: [PATCH 34/37] [*]: Sphinx added sphinxcontrib-phpdomain>=0.6.1 on requirements --- docs/requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/requirements.txt b/docs/requirements.txt index 57880fa..855d66c 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,2 +1,3 @@ Sphinx>=2.1.0 sphinx_rtd_theme>=0.4.0 +sphinxcontrib-phpdomain>=0.6.1 From 5059f0de9afdb3783c8afab7720342b778442bb8 Mon Sep 17 00:00:00 2001 From: microThread Date: Thu, 20 Jun 2019 18:50:02 +0300 Subject: [PATCH 35/37] Update index.rst --- docs/index.rst | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/index.rst b/docs/index.rst index 272ca40..6e62723 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -4,6 +4,36 @@ DotArray ==================== +.. raw:: html + + + + + + + + + + + + + + + + + + + + + + + + + + + + + DotArray is a simple PHP Library that can access an array in a dotted manner. - Easy to use. From e9226970c9350681c8841178da4f80defd941a7f Mon Sep 17 00:00:00 2001 From: microThread Date: Thu, 20 Jun 2019 18:57:17 +0300 Subject: [PATCH 36/37] Update index.rst --- docs/index.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/index.rst b/docs/index.rst index 6e62723..d7fc8be 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -6,6 +6,10 @@ DotArray .. raw:: html + + + + @@ -34,6 +38,8 @@ DotArray +

+ DotArray is a simple PHP Library that can access an array in a dotted manner. - Easy to use. From 32da333bfc7f532d241a501a5dc18304d88052f5 Mon Sep 17 00:00:00 2001 From: microThread Date: Thu, 20 Jun 2019 18:58:47 +0300 Subject: [PATCH 37/37] Update index.rst --- docs/index.rst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index d7fc8be..662b240 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -6,35 +6,35 @@ DotArray .. raw:: html - + - + - + - + - + - + - + - +