Skip to content
Snippets Groups Projects
Commit ab6f14bb authored by Viacheslav Poturaev's avatar Viacheslav Poturaev
Browse files

WIP

parent dadb65fb
No related merge requests found
......@@ -2,6 +2,7 @@ language: php
php:
- nightly
- hhvm
- 7.3
- 7.2
- 7.1
- 7.0
......
......@@ -35,6 +35,11 @@ class JsonDiff
*/
const SKIP_JSON_MERGE_PATCH = 16;
/**
* TOLERATE_ASSOCIATIVE_ARRAYS is an option to allow associative arrays to mimic JSON objects (not recommended)
*/
const TOLERATE_ASSOCIATIVE_ARRAYS = 32;
private $options = 0;
private $original;
private $new;
......@@ -236,6 +241,16 @@ class JsonDiff
{
$merge = !($this->options & self::SKIP_JSON_MERGE_PATCH);
if ($this->options & self::TOLERATE_ASSOCIATIVE_ARRAYS) {
if (is_array($original) && !empty($original) && !array_key_exists(0, $original)) {
$original = (object)$original;
}
if (is_array($new) && !empty($new) && !array_key_exists(0, $new)) {
$new = (object)$new;
}
}
if (
(!$original instanceof \stdClass && !is_array($original))
|| (!$new instanceof \stdClass && !is_array($new))
......
......@@ -25,6 +25,12 @@ class JsonPatch implements \JsonSerializable
*/
const STRICT_MODE = 2;
/**
* Allow associative arrays to mimic JSON objects (not recommended)
*/
const TOLERATE_ASSOCIATIVE_ARRAYS = 8;
private $flags = 0;
/**
......
......@@ -20,6 +20,11 @@ class JsonPointer
*/
const SKIP_IF_ISSET = 4;
/**
* Allow associative arrays to mimic JSON objects (not recommended)
*/
const TOLERATE_ASSOCIATIVE_ARRAYS = 8;
/**
* @param string $key
* @param bool $isURIFragmentId
......@@ -135,12 +140,16 @@ class JsonPointer
array_splice($ref, $key, 0, array($value));
}
if (false === $intKey) {
throw new Exception('Invalid key for array operation');
if (0 === ($flags & self::TOLERATE_ASSOCIATIVE_ARRAYS)) {
throw new Exception('Invalid key for array operation');
}
}
if ($intKey > count($ref) && 0 === ($flags & self::RECURSIVE_KEY_CREATION)) {
throw new Exception('Index is greater than number of items in array');
} elseif ($intKey < 0) {
throw new Exception('Negative index');
if (0 === ($flags & self::TOLERATE_ASSOCIATIVE_ARRAYS)) {
if ($intKey > count($ref) && 0 === ($flags & self::RECURSIVE_KEY_CREATION)) {
throw new Exception('Index is greater than number of items in array');
} elseif ($intKey < 0) {
throw new Exception('Negative index');
}
}
$ref = &$ref[$intKey];
......
<?php
namespace Swaggest\JsonDiff\Tests;
use Swaggest\JsonDiff\JsonDiff;
use Swaggest\JsonDiff\JsonPatch;
class AssociativeTest extends \PHPUnit_Framework_TestCase
{
/**
* @throws \Swaggest\JsonDiff\Exception
*/
public function testDiffAssociative()
{
$originalJson = <<<'JSON'
{
"key1": [4, 1, 2, 3],
"key2": 2,
"key3": {
"sub0": 0,
"sub1": "a",
"sub2": "b"
},
"key4": [
{"a":1, "b":true, "subs": [{"s":1}, {"s":2}, {"s":3}]}, {"a":2, "b":false}, {"a":3}
]
}
JSON;
$newJson = <<<'JSON'
{
"key5": "wat",
"key1": [5, 1, 2, 3],
"key4": [
{"c":false, "a":2}, {"a":1, "b":true, "subs": [{"s":3, "add": true}, {"s":2}, {"s":1}]}, {"c":1, "a":3}
],
"key3": {
"sub3": 0,
"sub2": false,
"sub1": "c"
}
}
JSON;
$diff = new JsonDiff(json_decode($originalJson), json_decode($newJson));
$expected = json_encode($diff->getPatch()->jsonSerialize(), JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES);
$diff = new JsonDiff(json_decode($originalJson, true), json_decode($newJson, true),
JsonDiff::TOLERATE_ASSOCIATIVE_ARRAYS);
$actual = json_encode($diff->getPatch()->jsonSerialize(), JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES);
$this->assertEquals($expected, $actual);
$original = json_decode($originalJson, true);
$newJson = json_decode($newJson, true);
$patch = JsonPatch::import(json_decode($actual));
$patch->setFlags(JsonPatch::TOLERATE_ASSOCIATIVE_ARRAYS);
$patch->apply($original);
$this->assertEquals($newJson, $original);
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment