From aeddccf13d825ef5b3ca0e1ba014a04dbc323c94 Mon Sep 17 00:00:00 2001 From: Fukuball Lin Date: Mon, 27 Apr 2015 17:33:38 +0800 Subject: [PATCH 1/4] setValue method bug fix setValue method will get different result according to the order of keys, the short key will overwrite the long keys, should check the long key is set, and push the short key. --- src/MultiArray.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/MultiArray.php b/src/MultiArray.php index 99b770c..fd2b518 100644 --- a/src/MultiArray.php +++ b/src/MultiArray.php @@ -215,7 +215,11 @@ private function setValue(array &$keys, &$element, &$value) throw new InvalidArgumentException('Expected array, got ' . gettype($element)); } - $element[$checkKey] = $value; + if (!isset($element[$checkKey])) { + $element[$checkKey] = $value; + } else { + array_push($element[$checkKey], $value); + } return $element[$checkKey]; } From de6e0c8768de99e757d164317ba433359f6e276e Mon Sep 17 00:00:00 2001 From: Fukuball Lin Date: Tue, 28 Apr 2015 04:29:12 +0800 Subject: [PATCH 2/4] add test --- tests/MultiArrayTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/MultiArrayTest.php b/tests/MultiArrayTest.php index 80385d5..a95b7be 100644 --- a/tests/MultiArrayTest.php +++ b/tests/MultiArrayTest.php @@ -180,6 +180,15 @@ public function testSetKeyTwoLevels() $this->assertEquals('value1', $array['key1']['key1-1']); } + public function testSetKeysInDiffOrder() + { + $array = $this->getMultiArray(); + $jsonObject = new MultiArray($array); + $jsonObject->set('key1.key1-1', 'test'); + $this->assertEquals('test', $jsonObject->get('key1.key1-1')); + $this->assertEquals('value1', $array['key1']['key1-1']); + } + public function testSetKeyAddedToCache() { $array = $this->getMultiArray(); From e9dd9f4141bd22a4b94585c7c4eaa4701c011c82 Mon Sep 17 00:00:00 2001 From: Fukuball Lin Date: Tue, 28 Apr 2015 16:21:46 +0800 Subject: [PATCH 3/4] setValue method bug fix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit setValue method will get InvalidArgumentException when set string value to keys according to the order of set value order. Fix this bug then we don’t get InvalidArgumentException anymore. --- composer.lock | 247 +++++++++++++++++++++++++++------------ src/MultiArray.php | 17 ++- tests/MultiArrayTest.php | 31 +---- 3 files changed, 189 insertions(+), 106 deletions(-) diff --git a/composer.lock b/composer.lock index 04e4a2c..34a59b8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "4030841f5d531d52283b20bba007dc45", + "hash": "ab18eab1e2ae23a43df911ebb73db4b5", "packages": [], "packages-dev": [ { @@ -61,28 +61,72 @@ ], "time": "2014-10-13 12:58:55" }, + { + "name": "hamcrest/hamcrest-php", + "version": "v1.2.1", + "source": { + "type": "git", + "url": "https://github.com/hamcrest/hamcrest-php.git", + "reference": "ac50c470531243944f977b8de75be0b684a9cb51" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/hamcrest/hamcrest-php/zipball/ac50c470531243944f977b8de75be0b684a9cb51", + "reference": "ac50c470531243944f977b8de75be0b684a9cb51", + "shasum": "" + }, + "require": { + "php": ">=5.3.2" + }, + "replace": { + "cordoval/hamcrest-php": "*", + "davedevelopment/hamcrest-php": "*", + "kodova/hamcrest-php": "*" + }, + "require-dev": { + "phpunit/php-file-iterator": "1.3.3", + "satooshi/php-coveralls": "dev-master" + }, + "type": "library", + "autoload": { + "classmap": [ + "hamcrest" + ], + "files": [ + "hamcrest/Hamcrest.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD" + ], + "description": "This is the PHP port of Hamcrest Matchers", + "keywords": [ + "test" + ], + "time": "2015-01-20 19:34:09" + }, { "name": "mockery/mockery", "version": "dev-master", "source": { "type": "git", "url": "https://github.com/padraic/mockery.git", - "reference": "4055c0e8577dfbc841fd384797487ff3fc75afff" + "reference": "dbe3cfe736bb1d3e319c4cd0c310c140737028a0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/padraic/mockery/zipball/4055c0e8577dfbc841fd384797487ff3fc75afff", - "reference": "4055c0e8577dfbc841fd384797487ff3fc75afff", + "url": "https://api.github.com/repos/padraic/mockery/zipball/dbe3cfe736bb1d3e319c4cd0c310c140737028a0", + "reference": "dbe3cfe736bb1d3e319c4cd0c310c140737028a0", "shasum": "" }, "require": { + "hamcrest/hamcrest-php": "~1.1", "lib-pcre": ">=7.0", - "php": ">=5.3.2" + "php": ">=5.4.0" }, "require-dev": { - "hamcrest/hamcrest-php": "~1.1", - "phpunit/phpunit": "~4.0", - "satooshi/php-coveralls": "~0.7@dev" + "phpunit/phpunit": "~4.0" }, "type": "library", "extra": { @@ -111,7 +155,7 @@ "homepage": "http://davedevelopment.co.uk" } ], - "description": "Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its core goal is to offer a test double framework with a succint API capable of clearly defining all possible object operations and interactions using a human readable Domain Specific Language (DSL). Designed as a drop in alternative to PHPUnit's phpunit-mock-objects library, Mockery is easy to integrate with PHPUnit and can operate alongside phpunit-mock-objects without the World ending.", + "description": "Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its core goal is to offer a test double framework with a succinct API capable of clearly defining all possible object operations and interactions using a human readable Domain Specific Language (DSL). Designed as a drop in alternative to PHPUnit's phpunit-mock-objects library, Mockery is easy to integrate with PHPUnit and can operate alongside phpunit-mock-objects without the World ending.", "homepage": "http://github.com/padraic/mockery", "keywords": [ "BDD", @@ -125,20 +169,20 @@ "test double", "testing" ], - "time": "2014-11-18 13:37:25" + "time": "2015-04-02 20:16:47" }, { "name": "phpunit/php-code-coverage", - "version": "2.0.11", + "version": "2.0.16", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "53603b3c995f5aab6b59c8e08c3a663d2cc810b7" + "reference": "934fd03eb6840508231a7f73eb8940cf32c3b66c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/53603b3c995f5aab6b59c8e08c3a663d2cc810b7", - "reference": "53603b3c995f5aab6b59c8e08c3a663d2cc810b7", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/934fd03eb6840508231a7f73eb8940cf32c3b66c", + "reference": "934fd03eb6840508231a7f73eb8940cf32c3b66c", "shasum": "" }, "require": { @@ -151,7 +195,7 @@ }, "require-dev": { "ext-xdebug": ">=2.1.4", - "phpunit/phpunit": "~4.1" + "phpunit/phpunit": "~4" }, "suggest": { "ext-dom": "*", @@ -170,9 +214,6 @@ ] }, "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "" - ], "license": [ "BSD-3-Clause" ], @@ -190,7 +231,7 @@ "testing", "xunit" ], - "time": "2014-08-31 06:33:04" + "time": "2015-04-11 04:35:00" }, { "name": "phpunit/php-file-iterator", @@ -327,16 +368,16 @@ }, { "name": "phpunit/php-token-stream", - "version": "1.3.0", + "version": "1.4.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-token-stream.git", - "reference": "f8d5d08c56de5cfd592b3340424a81733259a876" + "reference": "eab81d02569310739373308137284e0158424330" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/f8d5d08c56de5cfd592b3340424a81733259a876", - "reference": "f8d5d08c56de5cfd592b3340424a81733259a876", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/eab81d02569310739373308137284e0158424330", + "reference": "eab81d02569310739373308137284e0158424330", "shasum": "" }, "require": { @@ -349,7 +390,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.3-dev" + "dev-master": "1.4-dev" } }, "autoload": { @@ -372,7 +413,7 @@ "keywords": [ "tokenizer" ], - "time": "2014-08-31 06:12:13" + "time": "2015-04-08 04:46:07" }, { "name": "phpunit/phpunit", @@ -450,25 +491,25 @@ }, { "name": "phpunit/phpunit-mock-objects", - "version": "2.3.0", + "version": "2.3.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", - "reference": "c63d2367247365f688544f0d500af90a11a44c65" + "reference": "74ffb87f527f24616f72460e54b595f508dccb5c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/c63d2367247365f688544f0d500af90a11a44c65", - "reference": "c63d2367247365f688544f0d500af90a11a44c65", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/74ffb87f527f24616f72460e54b595f508dccb5c", + "reference": "74ffb87f527f24616f72460e54b595f508dccb5c", "shasum": "" }, "require": { - "doctrine/instantiator": "~1.0,>=1.0.1", + "doctrine/instantiator": "~1.0,>=1.0.2", "php": ">=5.3.3", "phpunit/php-text-template": "~1.2" }, "require-dev": { - "phpunit/phpunit": "~4.3" + "phpunit/phpunit": "~4.4" }, "suggest": { "ext-soap": "*" @@ -501,34 +542,34 @@ "mock", "xunit" ], - "time": "2014-10-03 05:12:11" + "time": "2015-04-02 05:36:41" }, { "name": "sebastian/comparator", - "version": "1.0.1", + "version": "1.1.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/comparator.git", - "reference": "e54a01c0da1b87db3c5a3c4c5277ddf331da4aef" + "reference": "1dd8869519a225f7f2b9eb663e225298fade819e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/e54a01c0da1b87db3c5a3c4c5277ddf331da4aef", - "reference": "e54a01c0da1b87db3c5a3c4c5277ddf331da4aef", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/1dd8869519a225f7f2b9eb663e225298fade819e", + "reference": "1dd8869519a225f7f2b9eb663e225298fade819e", "shasum": "" }, "require": { "php": ">=5.3.3", - "sebastian/diff": "~1.1", - "sebastian/exporter": "~1.0" + "sebastian/diff": "~1.2", + "sebastian/exporter": "~1.2" }, "require-dev": { - "phpunit/phpunit": "~4.1" + "phpunit/phpunit": "~4.4" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "1.1.x-dev" } }, "autoload": { @@ -565,20 +606,20 @@ "compare", "equality" ], - "time": "2014-05-11 23:00:21" + "time": "2015-01-29 16:28:08" }, { "name": "sebastian/diff", - "version": "1.2.0", + "version": "1.3.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/diff.git", - "reference": "5843509fed39dee4b356a306401e9dd1a931fec7" + "reference": "863df9687835c62aa423a22412d26fa2ebde3fd3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/5843509fed39dee4b356a306401e9dd1a931fec7", - "reference": "5843509fed39dee4b356a306401e9dd1a931fec7", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/863df9687835c62aa423a22412d26fa2ebde3fd3", + "reference": "863df9687835c62aa423a22412d26fa2ebde3fd3", "shasum": "" }, "require": { @@ -590,7 +631,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.2-dev" + "dev-master": "1.3-dev" } }, "autoload": { @@ -617,32 +658,32 @@ "keywords": [ "diff" ], - "time": "2014-08-15 10:29:00" + "time": "2015-02-22 15:13:53" }, { "name": "sebastian/environment", - "version": "1.2.0", + "version": "1.2.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/environment.git", - "reference": "0d9bf79554d2a999da194a60416c15cf461eb67d" + "reference": "5a8c7d31914337b69923db26c4221b81ff5a196e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/0d9bf79554d2a999da194a60416c15cf461eb67d", - "reference": "0d9bf79554d2a999da194a60416c15cf461eb67d", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/5a8c7d31914337b69923db26c4221b81ff5a196e", + "reference": "5a8c7d31914337b69923db26c4221b81ff5a196e", "shasum": "" }, "require": { "php": ">=5.3.3" }, "require-dev": { - "phpunit/phpunit": "~4.3" + "phpunit/phpunit": "~4.4" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.2.x-dev" + "dev-master": "1.3.x-dev" } }, "autoload": { @@ -667,32 +708,33 @@ "environment", "hhvm" ], - "time": "2014-10-22 06:38:05" + "time": "2015-01-01 10:01:08" }, { "name": "sebastian/exporter", - "version": "1.0.2", + "version": "1.2.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/exporter.git", - "reference": "c7d59948d6e82818e1bdff7cadb6c34710eb7dc0" + "reference": "84839970d05254c73cde183a721c7af13aede943" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/c7d59948d6e82818e1bdff7cadb6c34710eb7dc0", - "reference": "c7d59948d6e82818e1bdff7cadb6c34710eb7dc0", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/84839970d05254c73cde183a721c7af13aede943", + "reference": "84839970d05254c73cde183a721c7af13aede943", "shasum": "" }, "require": { - "php": ">=5.3.3" + "php": ">=5.3.3", + "sebastian/recursion-context": "~1.0" }, "require-dev": { - "phpunit/phpunit": "~4.0" + "phpunit/phpunit": "~4.4" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "1.2.x-dev" } }, "autoload": { @@ -732,20 +774,73 @@ "export", "exporter" ], - "time": "2014-09-10 00:51:36" + "time": "2015-01-27 07:23:06" + }, + { + "name": "sebastian/recursion-context", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/recursion-context.git", + "reference": "3989662bbb30a29d20d9faa04a846af79b276252" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/3989662bbb30a29d20d9faa04a846af79b276252", + "reference": "3989662bbb30a29d20d9faa04a846af79b276252", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides functionality to recursively process PHP variables", + "homepage": "http://www.github.com/sebastianbergmann/recursion-context", + "time": "2015-01-24 09:48:32" }, { "name": "sebastian/version", - "version": "1.0.3", + "version": "1.0.5", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/version.git", - "reference": "b6e1f0cf6b9e1ec409a0d3e2f2a5fb0998e36b43" + "reference": "ab931d46cd0d3204a91e1b9a40c4bc13032b58e4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/b6e1f0cf6b9e1ec409a0d3e2f2a5fb0998e36b43", - "reference": "b6e1f0cf6b9e1ec409a0d3e2f2a5fb0998e36b43", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/ab931d46cd0d3204a91e1b9a40c4bc13032b58e4", + "reference": "ab931d46cd0d3204a91e1b9a40c4bc13032b58e4", "shasum": "" }, "type": "library", @@ -767,30 +862,33 @@ ], "description": "Library that helps with managing the version number of Git-hosted PHP projects", "homepage": "https://github.com/sebastianbergmann/version", - "time": "2014-03-07 15:35:33" + "time": "2015-02-24 06:35:25" }, { "name": "symfony/yaml", - "version": "v2.5.7", + "version": "v2.6.6", "target-dir": "Symfony/Component/Yaml", "source": { "type": "git", "url": "https://github.com/symfony/Yaml.git", - "reference": "900d38bc8f74a50343ce65dd1c1e9819658ee56b" + "reference": "174f009ed36379a801109955fc5a71a49fe62dd4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Yaml/zipball/900d38bc8f74a50343ce65dd1c1e9819658ee56b", - "reference": "900d38bc8f74a50343ce65dd1c1e9819658ee56b", + "url": "https://api.github.com/repos/symfony/Yaml/zipball/174f009ed36379a801109955fc5a71a49fe62dd4", + "reference": "174f009ed36379a801109955fc5a71a49fe62dd4", "shasum": "" }, "require": { "php": ">=5.3.3" }, + "require-dev": { + "symfony/phpunit-bridge": "~2.7" + }, "type": "library", "extra": { "branch-alias": { - "dev-master": "2.5-dev" + "dev-master": "2.6-dev" } }, "autoload": { @@ -814,7 +912,7 @@ ], "description": "Symfony Yaml Component", "homepage": "http://symfony.com", - "time": "2014-11-20 13:22:25" + "time": "2015-03-30 15:54:10" } ], "aliases": [], @@ -823,10 +921,11 @@ "mockery/mockery": 20 }, "prefer-stable": false, + "prefer-lowest": false, "platform": { "php": ">=5.4" }, "platform-dev": { - "php": ">=5.5" + "php": ">=5.4" } } diff --git a/src/MultiArray.php b/src/MultiArray.php index fd2b518..3fc1083 100644 --- a/src/MultiArray.php +++ b/src/MultiArray.php @@ -218,7 +218,15 @@ private function setValue(array &$keys, &$element, &$value) if (!isset($element[$checkKey])) { $element[$checkKey] = $value; } else { - array_push($element[$checkKey], $value); + if (!is_array($element[$checkKey])) { + $temp_value = $element[$checkKey]; + $element[$checkKey] = array(); + array_push($element[$checkKey], $temp_value); + array_push($element[$checkKey], $value); + } else { + array_push($element[$checkKey], $value); + } + } return $element[$checkKey]; @@ -228,7 +236,12 @@ private function setValue(array &$keys, &$element, &$value) $element[$checkKey] = []; } - return $this->setValue($keys, $element[$checkKey], $value); + if (!is_array($element[$checkKey])) { + return $this->setValue($keys, $element, $value); + } else { + return $this->setValue($keys, $element[$checkKey], $value); + } + } /** diff --git a/tests/MultiArrayTest.php b/tests/MultiArrayTest.php index a95b7be..a70298d 100644 --- a/tests/MultiArrayTest.php +++ b/tests/MultiArrayTest.php @@ -180,15 +180,6 @@ public function testSetKeyTwoLevels() $this->assertEquals('value1', $array['key1']['key1-1']); } - public function testSetKeysInDiffOrder() - { - $array = $this->getMultiArray(); - $jsonObject = new MultiArray($array); - $jsonObject->set('key1.key1-1', 'test'); - $this->assertEquals('test', $jsonObject->get('key1.key1-1')); - $this->assertEquals('value1', $array['key1']['key1-1']); - } - public function testSetKeyAddedToCache() { $array = $this->getMultiArray(); @@ -235,26 +226,6 @@ public function testNewKeysPhp() $this->assertEquals('test', $jsonObject['test.test2.test3']); } - /** - * @expectedException InvalidArgumentException - */ - public function testNewKeyThrowsException() - { - $array = $this->getMultiArray(); - $jsonObject = new MultiArray($array); - $jsonObject->set('key1.key1-1.test', 'test'); - } - - /** - * @expectedException InvalidArgumentException - */ - public function testNewKeyPhpThrowsException() - { - $array = $this->getMultiArray(); - $jsonObject = new MultiArray($array); - $jsonObject['key1.key1-1.test'] = 'test'; - } - public function testNewKeyAddedToCache() { $array = $this->getMultiArray(); @@ -335,4 +306,4 @@ private function getMultiArray($encode = false) return $array; } -} +} \ No newline at end of file From 7a751d2c7fa71301c0b66b469bda9f632d3eff70 Mon Sep 17 00:00:00 2001 From: Fukuball Lin Date: Tue, 28 Apr 2015 16:29:54 +0800 Subject: [PATCH 4/4] add test case testSetKeyInDiffOrder --- tests/MultiArrayTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/MultiArrayTest.php b/tests/MultiArrayTest.php index a70298d..a6594ee 100644 --- a/tests/MultiArrayTest.php +++ b/tests/MultiArrayTest.php @@ -286,6 +286,25 @@ public function testUnsetPhpWillThrowException() unset($jsonObject['key']); } + public function testSetKeyInDiffOrder() + { + + $multiArray1 = new MultiArray(array()); + $multiArray1->set('key1.key2.key3.key4', 'value1'); + $multiArray1->set('key1.key2.key5', 'value2'); + $multiArray1->set('key1.key2', 'value3'); + + $multiArray2 = new MultiArray(array()); + $multiArray2->set('key1.key2', 'value3'); + $multiArray2->set('key1.key2.key5', 'value2'); + $multiArray2->set('key1.key2.key3.key4', 'value1'); + + $this->assertEquals($multiArray1->get('key1.key2'), $multiArray2->get('key1.key2')); + $this->assertEquals($multiArray1->get('key1.key2.key5'), $multiArray2->get('key1.key2.key5')); + $this->assertEquals($multiArray1->get('key1.key2.key3.key4'), $multiArray2->get('key1.key2.key3.key4')); + + } + private function getMultiArray($encode = false) { $array =[