-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rearrange equal items for non-homogeneous arrays, fixes #33
- Loading branch information
Showing
5 changed files
with
182 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Swaggest\JsonDiff; | ||
|
||
class JsonHash | ||
{ | ||
private $options = 0; | ||
|
||
public function __construct($options = 0) | ||
{ | ||
$this->options = $options; | ||
} | ||
|
||
/** | ||
* @param mixed $data | ||
* @param string $path | ||
* @return string | ||
*/ | ||
public function xorHash($data, $path = '') | ||
{ | ||
$xorHash = ''; | ||
|
||
if (!$data instanceof \stdClass && !is_array($data)) { | ||
$s = $path . (string)$data; | ||
if (strlen($xorHash) < strlen($s)) { | ||
$xorHash = str_pad($xorHash, strlen($s)); | ||
} | ||
$xorHash ^= $s; | ||
|
||
return $xorHash; | ||
} | ||
|
||
if ($this->options & JsonDiff::TOLERATE_ASSOCIATIVE_ARRAYS) { | ||
if (is_array($data) && !empty($data) && !array_key_exists(0, $data)) { | ||
$data = (object)$data; | ||
} | ||
} | ||
|
||
if (is_array($data)) { | ||
if ($this->options & JsonDiff::REARRANGE_ARRAYS) { | ||
foreach ($data as $key => $item) { | ||
$itemPath = $path . '/' . $key; | ||
$itemHash = $path . $this->xorHash($item, $itemPath); | ||
if (strlen($xorHash) < strlen($itemHash)) { | ||
$xorHash = str_pad($xorHash, strlen($itemHash)); | ||
} | ||
$xorHash ^= $itemHash; | ||
} | ||
} else { | ||
foreach ($data as $key => $item) { | ||
$itemPath = $path . '/' . $key; | ||
$itemHash = md5($itemPath . $this->xorHash($item, $itemPath), true); | ||
if (strlen($xorHash) < strlen($itemHash)) { | ||
$xorHash = str_pad($xorHash, strlen($itemHash)); | ||
} | ||
$xorHash ^= $itemHash; | ||
} | ||
} | ||
|
||
return $xorHash; | ||
} | ||
|
||
$dataKeys = get_object_vars($data); | ||
foreach ($dataKeys as $key => $value) { | ||
$propertyPath = $path . '/' . | ||
JsonPointer::escapeSegment($key, (bool)($this->options & JsonDiff::JSON_URI_FRAGMENT_ID)); | ||
$propertyHash = $propertyPath . $this->xorHash($value, $propertyPath); | ||
if (strlen($xorHash) < strlen($propertyHash)) { | ||
$xorHash = str_pad($xorHash, strlen($propertyHash)); | ||
} | ||
$xorHash ^= $propertyHash; | ||
} | ||
|
||
return $xorHash; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Swaggest\JsonDiff\Tests; | ||
|
||
use Swaggest\JsonDiff\JsonDiff; | ||
use Swaggest\JsonDiff\JsonHash; | ||
|
||
class JsonHashTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testHash() | ||
{ | ||
$h1 = (new JsonHash())->xorHash(json_decode('{"data": [{"A": 1},{"B": 2}]}')); | ||
$h2 = (new JsonHash())->xorHash(json_decode('{"data": [{"B": 2},{"A": 1}]}')); | ||
$h3 = (new JsonHash())->xorHash(json_decode('{"data": [{"B": 3},{"A": 2}]}')); | ||
|
||
$this->assertNotEmpty($h1); | ||
$this->assertNotEmpty($h2); | ||
$this->assertNotEmpty($h3); | ||
$this->assertNotEquals($h1, $h2); | ||
$this->assertNotEquals($h1, $h3); | ||
} | ||
|
||
public function testHashRearrange() | ||
{ | ||
$h1 = (new JsonHash(JsonDiff::REARRANGE_ARRAYS)) | ||
->xorHash(json_decode('{"data": [{"A": 1},{"B": 2}]}')); | ||
$h2 = (new JsonHash(JsonDiff::REARRANGE_ARRAYS)) | ||
->xorHash(json_decode('{"data": [{"B": 2},{"A": 1}]}')); | ||
$h3 = (new JsonHash(JsonDiff::REARRANGE_ARRAYS)) | ||
->xorHash(json_decode('{"data": [{"B": 3},{"A": 2}]}')); | ||
|
||
$this->assertNotEmpty($h1); | ||
$this->assertNotEmpty($h2); | ||
$this->assertNotEmpty($h3); | ||
$this->assertEquals($h1, $h2); | ||
$this->assertNotEquals($h1, $h3); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters