Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*~
\#*
.idea
61 changes: 22 additions & 39 deletions JsonPatch.inc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?
<?php

/*
Produce and apply json-patch objects.
Expand Down Expand Up @@ -89,24 +89,19 @@ class JsonPatch

foreach ($patches as $patch)
{
$found = false;
foreach (array('add', 'remove', 'replace', 'move', 'copy', 'test')
as $op)
{
if (isset($patch[$op])) {
$location = $patch[$op];
$found = true;
break;
}
$op = trim($patch['op']); //one test in tests.json defines "op":" add"

if (empty($op)) {
throw new JsonPatchException("Operation member 'op' missing in "
. json_encode($patch));
}

if (!$found)
{
if (!in_array($op, array('add', 'remove', 'replace', 'move', 'copy', 'test'))) {
throw new JsonPatchException("Unrecognized operation $op in "
. json_encode($patch));
. json_encode($patch));
}

$parts = self::decompose_pointer($location);
$parts = self::decompose_pointer($patch['path']);
if (in_array($op, Array('test', 'add', 'replace')))
{
$value = $patch['value'];
Expand Down Expand Up @@ -392,7 +387,7 @@ class JsonPatch
{
throw new JsonPatchException("'test' target value different - expected "
. json_encode($value) . ", found "
. json_encode($found));;
. json_encode($found));
}
}

Expand Down Expand Up @@ -510,18 +505,11 @@ class JsonPatch
throw new JsonPatchException('Target must be array or associative array');
}

if (self::is_associative($doc)) // N.B. returns false for empty arrays
{
if (is_numeric($part))
{
throw new JsonPatchException('Array operation on object target');
}
}
else
if (!self::is_associative($doc)) // N.B. returns false for empty arrays
{
if (count($doc) && !is_numeric($part))
{
throw new JsonPatchException('Non-array operation on array target');
throw new JsonPatchException('Non-array operation on array target');
}
else
{
Expand All @@ -537,13 +525,13 @@ class JsonPatch

if ($op == 'add')
{
if (is_numeric($part))
if (!self::is_associative($doc) && is_numeric($part))
{
array_splice($doc, $part, 0, Array($value));
}
else
{
if (array_key_exists($part, $doc))
if (array_key_exists($part, $doc) && !is_null($doc[$part]))
{
throw new JsonPatchException("'add' target '$part' already set");
}
Expand Down Expand Up @@ -600,24 +588,19 @@ class JsonPatch

foreach ($patches as $patch)
{
$found = false;
foreach (array('add', 'remove', 'replace', 'move', 'copy', 'test')
as $op)
{
if (isset($patch[$op])) {
$location = $patch[$op];
$found = true;
break;
}
$op = trim($patch['op']); //one test in tests.json defines "op":" add"

if (empty($op)) {
throw new JsonPatchException("Operation member 'op' missing in "
. json_encode($patch));
}

if (!$found)
{
if (!in_array($op, array('add', 'remove', 'replace', 'move', 'copy', 'test'))) {
throw new JsonPatchException("Unrecognized operation $op in "
. json_encode($patch));
. json_encode($patch));
}

$parts = self::decompose_pointer($location);
$parts = self::decompose_pointer($patch['path']);
if (in_array($op, Array('test', 'add', 'replace')))
{
$value = $patch['value'];
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Produce and apply json-patch objects.

Implements the IETF json-patch and json-pointer drafts:

http://tools.ietf.org/html/draft-ietf-appsawg-json-patch-02
http://tools.ietf.org/html/draft-ietf-appsawg-json-patch-10

http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-02
http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-09

Entry points
------------
Expand Down
2 changes: 1 addition & 1 deletion json-patch-tests
2 changes: 1 addition & 1 deletion run_tests.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?
<?php

// This is a simple test jig for testing JsonPatch.inc against
// the tests in json-patch-tests.
Expand Down