From 9ca7f17c875ec351f4e02f818b6aa8f17d3aeae5 Mon Sep 17 00:00:00 2001 From: Victor Date: Tue, 6 Jan 2015 23:18:39 +0100 Subject: [PATCH 1/4] Removed dependency from https://github.com/webnium/php-json-pointer Implementation of JsonAccessor using array pointers and array references General refactors Introduced more tests operations --- .gitignore | 1 + composer.json | 3 +- composer.lock | 78 ++--------- src/victuxbb/JsonPatch/ArrayAccessor.php | 46 ------ .../Exception/ExceptionInterface.php | 13 ++ .../JsonPatch/Exception/NoneExistentValue.php | 15 ++ .../JsonPatch/Exception/SyntaxError.php | 15 ++ src/victuxbb/JsonPatch/JsonAccessor.php | 131 +++++++++++++++++ .../{Parser.php => JsonPointerParser.php} | 61 ++++++-- .../JsonPatch/JsonPointerParserInterface.php | 24 ++++ .../JsonPatch/{Entity => Model}/Operation.php | 4 +- src/victuxbb/JsonPatch/PatchOperations.php | 25 ++-- .../JsonPatch/PatchOperationsInterface.php | 2 +- src/victuxbb/JsonPatch/Patcher.php | 4 +- src/victuxbb/JsonPatch/Tests/PatcherTest.php | 132 ++++++++++++++++++ tests/victuxbb/JsonPatch/PatcherTest.php | 62 -------- 16 files changed, 413 insertions(+), 203 deletions(-) delete mode 100644 src/victuxbb/JsonPatch/ArrayAccessor.php create mode 100644 src/victuxbb/JsonPatch/Exception/ExceptionInterface.php create mode 100644 src/victuxbb/JsonPatch/Exception/NoneExistentValue.php create mode 100644 src/victuxbb/JsonPatch/Exception/SyntaxError.php create mode 100644 src/victuxbb/JsonPatch/JsonAccessor.php rename src/victuxbb/JsonPatch/{Parser.php => JsonPointerParser.php} (54%) create mode 100644 src/victuxbb/JsonPatch/JsonPointerParserInterface.php rename src/victuxbb/JsonPatch/{Entity => Model}/Operation.php (90%) create mode 100644 src/victuxbb/JsonPatch/Tests/PatcherTest.php delete mode 100644 tests/victuxbb/JsonPatch/PatcherTest.php diff --git a/.gitignore b/.gitignore index 31e3ac6..e1b5874 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor/ .idea +nbproject/ diff --git a/composer.json b/composer.json index 1c33e74..e024a23 100644 --- a/composer.json +++ b/composer.json @@ -13,8 +13,7 @@ } ], "require": { - "php": ">=5.3.3", - "webnium/json-pointer": "dev-master" + "php": ">=5.3.3" }, "require-dev": { "phpunit/phpunit": "4.3.*" diff --git a/composer.lock b/composer.lock index 8a41f05..0f06356 100644 --- a/composer.lock +++ b/composer.lock @@ -4,58 +4,8 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "ad3dddb373786828c3f6c6d943788c26", - "packages": [ - { - "name": "webnium/json-pointer", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/webnium/php-json-pointer.git", - "reference": "133cbc669c475ad41795a336ab16155ecd182254" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/webnium/php-json-pointer/zipball/133cbc669c475ad41795a336ab16155ecd182254", - "reference": "133cbc669c475ad41795a336ab16155ecd182254", - "shasum": "" - }, - "require": { - "php": ">=5.4.0" - }, - "require-dev": { - "phake/phake": "*", - "satooshi/php-coveralls": "dev-master" - }, - "type": "library", - "autoload": { - "psr-0": { - "Webnium\\JsonPointer\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT License" - ], - "authors": [ - { - "name": "MugeSo", - "email": "mugeso@mugeso.com", - "homepage": "http://muge.so/", - "role": "Developer" - } - ], - "description": "Accessor implement based on JSON Pointer (RFC6901)", - "homepage": "https://github.com/webnium/php-json-pointer", - "keywords": [ - "array", - "array accessor", - "json", - "json pointer" - ], - "time": "2013-11-29 14:31:13" - } - ], + "hash": "72f9ea3e21706a0eed1736c551b421db", + "packages": [], "packages-dev": [ { "name": "doctrine/instantiator", @@ -113,16 +63,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "2.0.13", + "version": "2.0.14", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "0e7d2eec5554f869fa7a4ec2d21e4b37af943ea5" + "reference": "ca158276c1200cc27f5409a5e338486bc0b4fc94" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/0e7d2eec5554f869fa7a4ec2d21e4b37af943ea5", - "reference": "0e7d2eec5554f869fa7a4ec2d21e4b37af943ea5", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/ca158276c1200cc27f5409a5e338486bc0b4fc94", + "reference": "ca158276c1200cc27f5409a5e338486bc0b4fc94", "shasum": "" }, "require": { @@ -174,7 +124,7 @@ "testing", "xunit" ], - "time": "2014-12-03 06:41:44" + "time": "2014-12-26 13:28:33" }, { "name": "phpunit/php-file-iterator", @@ -720,16 +670,16 @@ }, { "name": "sebastian/version", - "version": "1.0.3", + "version": "1.0.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/version.git", - "reference": "b6e1f0cf6b9e1ec409a0d3e2f2a5fb0998e36b43" + "reference": "a77d9123f8e809db3fbdea15038c27a95da4058b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/b6e1f0cf6b9e1ec409a0d3e2f2a5fb0998e36b43", - "reference": "b6e1f0cf6b9e1ec409a0d3e2f2a5fb0998e36b43", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/a77d9123f8e809db3fbdea15038c27a95da4058b", + "reference": "a77d9123f8e809db3fbdea15038c27a95da4058b", "shasum": "" }, "type": "library", @@ -751,7 +701,7 @@ ], "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": "2014-12-15 14:25:24" }, { "name": "symfony/yaml", @@ -803,9 +753,7 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": { - "webnium/json-pointer": 20 - }, + "stability-flags": [], "prefer-stable": false, "platform": { "php": ">=5.3.3" diff --git a/src/victuxbb/JsonPatch/ArrayAccessor.php b/src/victuxbb/JsonPatch/ArrayAccessor.php deleted file mode 100644 index 462ef0a..0000000 --- a/src/victuxbb/JsonPatch/ArrayAccessor.php +++ /dev/null @@ -1,46 +0,0 @@ -parser->parse($pointer); - $total = count($pointerArray); - $location = $pointerArray[$total -1]; - $sourceArray = $pointerArray[$total -2]; - - $res = array_splice($array[$sourceArray],$location,0,$value); - - return $array; - - } - - public function remove($pointer, &$array, $value) - { - - } - - -} \ No newline at end of file diff --git a/src/victuxbb/JsonPatch/Exception/ExceptionInterface.php b/src/victuxbb/JsonPatch/Exception/ExceptionInterface.php new file mode 100644 index 0000000..a92ddc7 --- /dev/null +++ b/src/victuxbb/JsonPatch/Exception/ExceptionInterface.php @@ -0,0 +1,13 @@ +parser = $parser; + } + + /** + * get pointed value + * + * @param string $pointer JSON Pointer string + * @param array $array target array + * + * @throws Exception\SyntaxError + * @throws Exception\NoneExistentValue + */ + public function get($pointerArray, $array) + { + $current = $array; + foreach ($pointerArray as $key) { + if (!is_array($current) || !array_key_exists($key, $current)) { + throw new Exception\NoneExistentValue('references none existent value.'); + } + + $current = $current[$key]; + } + + return $current; + } + + /** + * set pointed value + * + * @param string $pointer JSON Pointer string + * @param array &$array target array + * @param mixed $value value to set + * + * @throws Exception\SyntaxError + * @throws Exception\NoneExistentValue + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @SuppressWarnings(PHPMD.EvalExpression) + */ + + + public function set($pointerArray, &$array, $value) + { + while (list($key, $val) = each($pointerArray)) { + if(is_array($array[$val])){ + $nextArray = &$array[$val]; + $this->set($pointerArray,$nextArray, $value); + break; + }else{ + $array[$val] = $value; + } + + } + + } + + + /** + * set pointed value in the location of an array + * + * @param string $pointer JSON Pointer string + * @param array &$array target array + * @param mixed $value value to set + * + * @throws Exception\SyntaxError + * @throws Exception\NoneExistentValue + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @SuppressWarnings(PHPMD.EvalExpression) + */ + public function insert($pointerArray, &$array, $value) + { + while (list($key, $val) = each($pointerArray)) { + if(isset($array[$val]) && is_array($array[$val])){ + $nextArray = &$array[$val]; + $this->insert($pointerArray,$nextArray, $value); + break; + }else{ + if($this->parser->destinationIsLocationInArray()){ + array_splice($array,$val,0,$value); + }else{ + $array[$val] = $value; + } + + } + + } + + } + + public function remove($pointerArray, &$array) + { + while (list($key, $val) = each($pointerArray)) { + if(isset($array[$val]) && is_array($array[$val])){ + $nextArray = &$array[$val]; + $this->remove($pointerArray,$nextArray); + break; + }else{ + if($this->parser->destinationIsLocationInArray()){ + array_splice($array,$val,1); + }else{ + unset($array[$val]); + } + + } + + } + + } + + +} \ No newline at end of file diff --git a/src/victuxbb/JsonPatch/Parser.php b/src/victuxbb/JsonPatch/JsonPointerParser.php similarity index 54% rename from src/victuxbb/JsonPatch/Parser.php rename to src/victuxbb/JsonPatch/JsonPointerParser.php index 2a46e59..89410a8 100644 --- a/src/victuxbb/JsonPatch/Parser.php +++ b/src/victuxbb/JsonPatch/JsonPointerParser.php @@ -8,11 +8,10 @@ namespace victuxbb\JsonPatch; -use Webnium\JsonPointer\Parser as WebniumParser; -use Webnium\JsonPointer\Exception; -use Webnium\JsonPointer\ParserInterface; +use victuxbb\JsonPatch\Exception\SyntaxError; -class Parser extends WebniumParser implements ParserInterface + +class JsonPointerParser implements JsonPointerParserInterface { /** * The elements of the JsonPointer @@ -47,21 +46,67 @@ public function __construct($pointer,$arrayTarget) } } - $this->length = count($this->elements); - return $this->elements; + $this->length = count($this->elements); } + + /** + * {@inheritdoc} + */ + public function parse($pointer) + { + if ($pointer === '') { + return []; + } + + if ($pointer[0] !== '/') { + throw new SyntaxError(sprintf('pointer start with "%s", "/" expected.', $pointer[0])); + } + + $pointerArray = @array_map(function ($referenceToken) { + return preg_replace_callback('/~./', function ($matches) { + $escaped = $matches[0]; + + if ($escaped === '~0') { + return '~'; + } + if ($escaped === '~1') { + return '/'; + } + + throw new SyntaxError(sprintf('unknown escape sequence "%s" detected.', $escaped)); + }, $referenceToken); + }, explode('/', $pointer)); + + array_shift($pointerArray); + + return $pointerArray; + } + + /* * An element to add to an existing array - whereupon the supplied * value is added to the array at the indicated location. */ public function destinationIsLocationInArray() - { + { if($this->length > 1){ $lastElement = $this->elements[$this->length-1]; - if(is_numeric($lastElement) && $this->isIndex[$this->length-2]) return true; + if(is_numeric($lastElement) && $this->isIndex[$this->length-2]){ + return true; + } } return false; } + + public function getElements() + { + return $this->elements; + } + + public function getLength() + { + return $this->length; + } diff --git a/src/victuxbb/JsonPatch/JsonPointerParserInterface.php b/src/victuxbb/JsonPatch/JsonPointerParserInterface.php new file mode 100644 index 0000000..fa27bbc --- /dev/null +++ b/src/victuxbb/JsonPatch/JsonPointerParserInterface.php @@ -0,0 +1,24 @@ +object = &$object; - $this->parser = new Parser($operation->path,$this->object); - $this->jsonPointer = new ArrayAccessor($this->parser); + $this->parser = new JsonPointerParser($operation->path,$this->object); + $this->jsonPointer = new JsonAccessor($this->parser); } public function add($operation) - { - if($this->parser->destinationIsLocationInArray($operation->path,$this->object)){ - $this->jsonPointer->insert($operation->path,$this->object,$operation->value); - }else { - $this->jsonPointer->set($operation->path, $this->object, $operation->value); - } + { + $this->jsonPointer->insert($this->parser->getElements(),$this->object,$operation->value); } public function remove($operation) { - $this->jsonPointer->set($operation->path,$this->object,null); + $this->jsonPointer->remove($this->parser->getElements(),$this->object); } public function replace($operation) { - $this->jsonPointer->set($operation->path,$this->object,$operation->value); + $this->jsonPointer->set($this->parser->getElements(),$this->object,$operation->value); } public function move($operation) { - $valueFrom = $this->jsonPointer->get($operation->from,$this->object); - $this->jsonPointer->set($operation->path,$this->object,$valueFrom); - - $this->jsonPointer->set($operation->from,$this->object,null); + $valueFrom = $this->jsonPointer->get($this->parser->getElements(),$this->object); + $this->jsonPointer->insert($this->parser->getElements(),$this->object,$valueFrom); + $this->jsonPointer->remove($this->parser->getElements(),$this->object); } diff --git a/src/victuxbb/JsonPatch/PatchOperationsInterface.php b/src/victuxbb/JsonPatch/PatchOperationsInterface.php index 91a50e0..1eea32a 100644 --- a/src/victuxbb/JsonPatch/PatchOperationsInterface.php +++ b/src/victuxbb/JsonPatch/PatchOperationsInterface.php @@ -5,7 +5,7 @@ interface PatchOperationsInterface { - public function add($operation); + public function add($operation); public function remove($operation); public function replace($operation); public function move($operation); diff --git a/src/victuxbb/JsonPatch/Patcher.php b/src/victuxbb/JsonPatch/Patcher.php index efe8ff1..de7957c 100644 --- a/src/victuxbb/JsonPatch/Patcher.php +++ b/src/victuxbb/JsonPatch/Patcher.php @@ -3,7 +3,7 @@ namespace victuxbb\JsonPatch; -use victuxbb\JsonPatch\Entity\Operation; +use victuxbb\JsonPatch\Model\Operation; use victuxbb\JsonPatch\PatchOperations; class Patcher { @@ -24,8 +24,8 @@ public function patch($jsonTargetObject,$jsonOperations) $patchOperation = new PatchOperations($object,$operation); $reflectionMethod = new \ReflectionMethod('victuxbb\JsonPatch\PatchOperations',$method_call); $reflectionMethod->invoke($patchOperation,$operation); - } + return json_encode($object); diff --git a/src/victuxbb/JsonPatch/Tests/PatcherTest.php b/src/victuxbb/JsonPatch/Tests/PatcherTest.php new file mode 100644 index 0000000..ec3a245 --- /dev/null +++ b/src/victuxbb/JsonPatch/Tests/PatcherTest.php @@ -0,0 +1,132 @@ +patch($targetJSON,$patchOperations); + $this->assertEquals($expected, $result); + + + $targetJSON ='{ "foo": [ "bar", "baz" ] }'; + $patchOperations = '[ + { "op": "add", "path": "/foo/1", "value": "qux" } + ]'; + $expected ='{"foo":["bar","qux","baz"]}'; + + $result = $patcher->patch($targetJSON,$patchOperations); + $this->assertEquals($expected, $result); + + + + } + + public function testReplaceOperation() + { + + $targetJSON ='{"baz": "qux","foo": "bar"}'; + $patchOperations = '[ + { "op": "replace", "path": "/baz", "value": "boo" } + ]'; + $expected ='{"baz":"boo","foo":"bar"}'; + + $patcher = new Patcher(); + $result = $patcher->patch($targetJSON,$patchOperations); + $this->assertEquals($expected, $result); + + $targetJSON ='{ + "foo": { + "bar": "baz", + "waldo": "fred" + }, + "qux": { + "corge": "grault" + } + }'; + $patchOperations = '[ + { "op": "replace", "path": "/foo/waldo", "value": "boo" } + ]'; + $expected ='{"foo":{"bar":"baz","waldo":"boo"},"qux":{"corge":"grault"}}'; + + $result = $patcher->patch($targetJSON,$patchOperations); + $this->assertEquals($expected, $result); + } + + public function testRemoveOperation() + { + $targetJSON ='{"baz": "qux","foo": "bar"}'; + $patchOperations = '[ + { "op": "remove", "path": "/baz" } + ]'; + $expected ='{"foo":"bar"}'; + + $patcher = new Patcher(); + $result = $patcher->patch($targetJSON,$patchOperations); + $this->assertEquals($expected, $result); + + $targetJSON ='{"foo":["bar","qux","baz"]}'; + $patchOperations = '[ + { "op": "remove", "path": "/foo/1" } + ]'; + $expected ='{"foo":["bar","baz"]}'; + + $result = $patcher->patch($targetJSON,$patchOperations); + $this->assertEquals($expected, $result); + } + + /*public function testMoveOperation() + { + $targetJSON ='{ + "foo": { + "bar": "baz", + "waldo": "fred" + }, + "qux": { + "corge": "grault" + } + }'; + $patchOperations = '[ + { "op": "move", "from": "/foo/waldo", "path": "/qux/thud" } + ]'; + $expected ='{"foo":{"bar":"baz"},"qux":{"corge":"grault","thud":"fred"}}'; + + $patcher = new Patcher(); + $result = $patcher->patch($targetJSON,$patchOperations); + $this->assertEquals($expected, $result); + + + $targetJSON ='{"foo":["all","grass","cows","eat"]}'; + $patchOperations = '[ + { "op": "move", "from": "/foo/1", "path": "/foo/3" } + ]'; + $expected ='{"foo":["all","cows","eat","grass"]}'; + + $result = $patcher->patch($targetJSON,$patchOperations); + $this->assertEquals($expected, $result); + + + }*/ + +} \ No newline at end of file diff --git a/tests/victuxbb/JsonPatch/PatcherTest.php b/tests/victuxbb/JsonPatch/PatcherTest.php deleted file mode 100644 index b353513..0000000 --- a/tests/victuxbb/JsonPatch/PatcherTest.php +++ /dev/null @@ -1,62 +0,0 @@ -patch($targetJSON,$patchOperations); - $this->assertEquals($expected, $result); - - - $targetJSON ='{ "foo": [ "bar", "baz" ] }'; - $patchOperations = '[ - { "op": "add", "path": "/foo/1", "value": "qux" } - ]'; - $expected ='{"foo":["bar","qux","baz"]}'; - - $result = $patcher->patch($targetJSON,$patchOperations); - $this->assertEquals($expected, $result); - - - - } - - public function testReplaceOperation() - { - - $targetJSON ='{"baz": "qux","foo": "bar"}'; - $patchOperations = '[ - { "op": "replace", "path": "/baz", "value": "boo" } - ]'; - $expected ='{"baz":"boo","foo":"bar"}'; - - $patcher = new Patcher(); - $result = $patcher->patch($targetJSON,$patchOperations); - $this->assertEquals($expected, $result); - - - } - -} \ No newline at end of file From 40b04900685a62c82ea17e8f4997cd9fe4d76c8c Mon Sep 17 00:00:00 2001 From: Victor Date: Sun, 11 Jan 2015 19:48:26 +0100 Subject: [PATCH 2/4] Removed TODO comments Refactor method parse in JsonPointerParser Removed user specific files from .gitignore, moved to global gitignore removed some unused variables and use statements Added end with a newline character in some files for best practices Bugfix in construct from model Operation --- .gitignore | 4 +-- src/victuxbb/JsonPatch/JsonAccessor.php | 6 ++--- src/victuxbb/JsonPatch/JsonPointerParser.php | 26 ++++++++++--------- src/victuxbb/JsonPatch/Model/Operation.php | 7 ++--- src/victuxbb/JsonPatch/PatchOperations.php | 15 ++++++----- .../JsonPatch/PatchOperationsInterface.php | 3 +-- src/victuxbb/JsonPatch/Patcher.php | 3 +-- src/victuxbb/JsonPatch/Tests/PatcherTest.php | 1 + 8 files changed, 30 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index e1b5874..a725465 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1 @@ -vendor/ -.idea -nbproject/ +vendor/ \ No newline at end of file diff --git a/src/victuxbb/JsonPatch/JsonAccessor.php b/src/victuxbb/JsonPatch/JsonAccessor.php index 1dc24da..94db8d0 100644 --- a/src/victuxbb/JsonPatch/JsonAccessor.php +++ b/src/victuxbb/JsonPatch/JsonAccessor.php @@ -61,7 +61,7 @@ public function get($pointerArray, $array) public function set($pointerArray, &$array, $value) { - while (list($key, $val) = each($pointerArray)) { + while (list(,$val) = each($pointerArray)) { if(is_array($array[$val])){ $nextArray = &$array[$val]; $this->set($pointerArray,$nextArray, $value); @@ -126,6 +126,4 @@ public function remove($pointerArray, &$array) } } - - -} \ No newline at end of file +} diff --git a/src/victuxbb/JsonPatch/JsonPointerParser.php b/src/victuxbb/JsonPatch/JsonPointerParser.php index 89410a8..1cf05bc 100644 --- a/src/victuxbb/JsonPatch/JsonPointerParser.php +++ b/src/victuxbb/JsonPatch/JsonPointerParser.php @@ -62,18 +62,23 @@ public function parse($pointer) throw new SyntaxError(sprintf('pointer start with "%s", "/" expected.', $pointer[0])); } - $pointerArray = @array_map(function ($referenceToken) { + $pointerArray = array_map(function ($referenceToken) { return preg_replace_callback('/~./', function ($matches) { $escaped = $matches[0]; + + switch ($escaped) { + case '~0': + return '~'; + break; - if ($escaped === '~0') { - return '~'; + case '~1': + return '/'; + break; + default: + throw new SyntaxError(sprintf('unknown escape sequence "%s" detected.', $escaped)); + break; } - if ($escaped === '~1') { - return '/'; - } - - throw new SyntaxError(sprintf('unknown escape sequence "%s" detected.', $escaped)); + }, $referenceToken); }, explode('/', $pointer)); @@ -107,7 +112,4 @@ public function getLength() { return $this->length; } - - - -} \ No newline at end of file +} diff --git a/src/victuxbb/JsonPatch/Model/Operation.php b/src/victuxbb/JsonPatch/Model/Operation.php index e86e255..90656c7 100644 --- a/src/victuxbb/JsonPatch/Model/Operation.php +++ b/src/victuxbb/JsonPatch/Model/Operation.php @@ -15,7 +15,7 @@ class Operation{ private $from; - public function _construct($op,$path,$value){ + public function __construct($op,$path,$value){ $this->op = $op; $this->path = $path; @@ -42,7 +42,4 @@ public function getFrom() { return $this->from; } - - - -} \ No newline at end of file +} diff --git a/src/victuxbb/JsonPatch/PatchOperations.php b/src/victuxbb/JsonPatch/PatchOperations.php index c968954..15fc029 100644 --- a/src/victuxbb/JsonPatch/PatchOperations.php +++ b/src/victuxbb/JsonPatch/PatchOperations.php @@ -3,8 +3,7 @@ namespace victuxbb\JsonPatch; use victuxbb\JsonPatch\JsonAccessor; -use victuxbb\JsonPatch\Parser; - +use victuxbb\JsonPatch\Exception\NoneExistentValue; class PatchOperations implements PatchOperationsInterface { @@ -48,12 +47,14 @@ public function copy($operation) $valueFrom = $this->jsonPointer->get($operation->from,$this->object); $this->jsonPointer->set($operation->path,$this->object,$valueFrom); } - /** - * TODO implement suggested errors in http://tools.ietf.org/html/rfc5789#section-2.2 - **/ + public function test($operation) { + $valueFrom = $this->jsonPointer->get($operation->path,$this->object); + $value = $operation->value; + if($valueFrom === $value) return true; + + throw new NoneExistentValue('references none existent value.'); } - -} \ No newline at end of file +} diff --git a/src/victuxbb/JsonPatch/PatchOperationsInterface.php b/src/victuxbb/JsonPatch/PatchOperationsInterface.php index 1eea32a..763a0ba 100644 --- a/src/victuxbb/JsonPatch/PatchOperationsInterface.php +++ b/src/victuxbb/JsonPatch/PatchOperationsInterface.php @@ -11,5 +11,4 @@ public function replace($operation); public function move($operation); public function copy($operation); public function test($operation); - -} \ No newline at end of file +} diff --git a/src/victuxbb/JsonPatch/Patcher.php b/src/victuxbb/JsonPatch/Patcher.php index de7957c..e9d1c84 100644 --- a/src/victuxbb/JsonPatch/Patcher.php +++ b/src/victuxbb/JsonPatch/Patcher.php @@ -3,7 +3,6 @@ namespace victuxbb\JsonPatch; -use victuxbb\JsonPatch\Model\Operation; use victuxbb\JsonPatch\PatchOperations; class Patcher { @@ -30,4 +29,4 @@ public function patch($jsonTargetObject,$jsonOperations) return json_encode($object); } -} \ No newline at end of file +} diff --git a/src/victuxbb/JsonPatch/Tests/PatcherTest.php b/src/victuxbb/JsonPatch/Tests/PatcherTest.php index ec3a245..f3226f2 100644 --- a/src/victuxbb/JsonPatch/Tests/PatcherTest.php +++ b/src/victuxbb/JsonPatch/Tests/PatcherTest.php @@ -128,5 +128,6 @@ public function testRemoveOperation() }*/ + } \ No newline at end of file From 3a8cfe1576e89e2ce8a230f6d7025299cef6e84f Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 12 Jan 2015 00:47:37 +0100 Subject: [PATCH 3/4] Removed unused variables from JsonAccessor Added a newline character in gitignore Implementation of move operation and phpunit test --- .gitignore | 2 +- src/victuxbb/JsonPatch/JsonAccessor.php | 14 +++++--------- src/victuxbb/JsonPatch/PatchOperations.php | 5 +++-- src/victuxbb/JsonPatch/Tests/PatcherTest.php | 4 ++-- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index a725465..48b8bf9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -vendor/ \ No newline at end of file +vendor/ diff --git a/src/victuxbb/JsonPatch/JsonAccessor.php b/src/victuxbb/JsonPatch/JsonAccessor.php index 94db8d0..30e73a7 100644 --- a/src/victuxbb/JsonPatch/JsonAccessor.php +++ b/src/victuxbb/JsonPatch/JsonAccessor.php @@ -53,9 +53,7 @@ public function get($pointerArray, $array) * @param mixed $value value to set * * @throws Exception\SyntaxError - * @throws Exception\NoneExistentValue - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - * @SuppressWarnings(PHPMD.EvalExpression) + * @throws Exception\NoneExistentValue */ @@ -83,19 +81,17 @@ public function set($pointerArray, &$array, $value) * @param mixed $value value to set * * @throws Exception\SyntaxError - * @throws Exception\NoneExistentValue - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - * @SuppressWarnings(PHPMD.EvalExpression) + * @throws Exception\NoneExistentValue */ public function insert($pointerArray, &$array, $value) { - while (list($key, $val) = each($pointerArray)) { + while (list(, $val) = each($pointerArray)) { if(isset($array[$val]) && is_array($array[$val])){ $nextArray = &$array[$val]; $this->insert($pointerArray,$nextArray, $value); break; }else{ - if($this->parser->destinationIsLocationInArray()){ + if($this->parser->destinationIsLocationInArray()){ array_splice($array,$val,0,$value); }else{ $array[$val] = $value; @@ -109,7 +105,7 @@ public function insert($pointerArray, &$array, $value) public function remove($pointerArray, &$array) { - while (list($key, $val) = each($pointerArray)) { + while (list(, $val) = each($pointerArray)) { if(isset($array[$val]) && is_array($array[$val])){ $nextArray = &$array[$val]; $this->remove($pointerArray,$nextArray); diff --git a/src/victuxbb/JsonPatch/PatchOperations.php b/src/victuxbb/JsonPatch/PatchOperations.php index 15fc029..937a874 100644 --- a/src/victuxbb/JsonPatch/PatchOperations.php +++ b/src/victuxbb/JsonPatch/PatchOperations.php @@ -36,9 +36,10 @@ public function replace($operation) public function move($operation) { - $valueFrom = $this->jsonPointer->get($this->parser->getElements(),$this->object); + $elementsFrom = $this->parser->parse($operation->from); + $valueFrom = $this->jsonPointer->get($elementsFrom,$this->object); + $this->jsonPointer->remove($elementsFrom,$this->object); $this->jsonPointer->insert($this->parser->getElements(),$this->object,$valueFrom); - $this->jsonPointer->remove($this->parser->getElements(),$this->object); } diff --git a/src/victuxbb/JsonPatch/Tests/PatcherTest.php b/src/victuxbb/JsonPatch/Tests/PatcherTest.php index f3226f2..847b68a 100644 --- a/src/victuxbb/JsonPatch/Tests/PatcherTest.php +++ b/src/victuxbb/JsonPatch/Tests/PatcherTest.php @@ -96,7 +96,7 @@ public function testRemoveOperation() $this->assertEquals($expected, $result); } - /*public function testMoveOperation() + public function testMoveOperation() { $targetJSON ='{ "foo": { @@ -127,7 +127,7 @@ public function testRemoveOperation() $this->assertEquals($expected, $result); - }*/ + } } \ No newline at end of file From 09c809288102f470008db50f8df70e85d9a5fc10 Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 12 Jan 2015 01:29:19 +0100 Subject: [PATCH 4/4] Added documentation --- README.md | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index e4494e7..e9d926f 100644 --- a/README.md +++ b/README.md @@ -1,38 +1,45 @@ jsonpatch ========= -Implementation of JSON Patch (http://tools.ietf.org/html/rfc6902) in PHP using JSON Pointer from: +[![SensioLabsInsight](https://insight.sensiolabs.com/projects/dee95637-0141-4caa-b6bf-1189a848128c/big.png)](https://insight.sensiolabs.com/projects/dee95637-0141-4caa-b6bf-1189a848128c) -https://github.com/webnium/php-json-pointer +Implementation of JSON Patch (http://tools.ietf.org/html/rfc6902) in PHP -Currently still developing, in the next couple of days I will post documentation and some refactors. +Installation +------------- +Require [`victuxbb/jsonpatch`](https://packagist.org/packages/victuxbb/jsonpatch) +into your `composer.json` file: -Example of test.php +``` json +{ + "require": { + "victuxbb/jsonpatch": "@stable" + } +} +``` -patch($targetJSON,$patchOperations); -$targetJSON ='{"a":{"b":["c","d","e"]}}'; +``` -$patchDocument = '[ - {"op":"add", "path":"/a/d", "value":["a","b"]}, - {"op":"move", "path":"/a/c", "from":"/a/d"}, - {"op":"copy", "path":"/a/e", "from":"/a/c"}, - {"op":"replace", "path":"/a/e", "value":["a"]} - ]'; +Thanks to +--------- -$result = $patcher->patch($targetJSON,$patchDocument); +*https://github.com/webnium -print_r($result); +*https://github.com/javadegava -Thanks to: +*http://williamdurand.fr/2014/02/14/please-do-not-patch-like-an-idiot/ -https://github.com/webnium -https://github.com/javadegava -http://williamdurand.fr/2014/02/14/please-do-not-patch-like-an-idiot/ -http://www.groupalia.com/ +*http://www.groupalia.com/