Skip to content

Commit c1d6b3c

Browse files
committed
fixing get by numeric key
1 parent d9b70ae commit c1d6b3c

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

src/Cli/Diff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Diff extends Command
2929
static function setUpDefinition(Definition $definition, $options)
3030
{
3131
$definition->name = 'json-diff';
32-
$definition->version = 'v1.1.0';
32+
$definition->version = 'v1.1.1';
3333
$definition->description = 'JSON diff and rearrange tool for PHP, https://github.com/swaggest/json-diff';
3434

3535
$options->action = Command\Option::create()->setIsUnnamed()->setIsRequired()

src/JsonProcessor.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,19 @@ public static function getByPath(&$holder, $path)
3737
if ('#' === $pathItems[0]) {
3838
array_shift($pathItems);
3939
}
40-
$ref = &$holder;
40+
$ref = $holder;
4141
while (null !== $key = array_shift($pathItems)) {
4242
$key = urldecode($key);
4343
if ($ref instanceof \stdClass) {
44-
if (property_exists($ref, $key)) {
45-
$ref = &$ref->$key;
44+
$vars = get_object_vars($ref);
45+
if (array_key_exists($key, $vars)) {
46+
$ref = $vars[$key];
4647
} else {
4748
throw new Exception('Key not found: ' . $key);
4849
}
4950
} else {
5051
if (array_key_exists($key, $ref)) {
51-
$ref = &$ref[$key];
52+
$ref = $ref[$key];
5253
} else {
5354
throw new Exception('Key not found: ' . $key);
5455
}

tests/src/ProcessorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,7 @@ public function testProcess()
2929
JsonProcessor::pushByPath($json, '#/l1/l2/1/1', 1);
3030

3131
$this->assertSame('{"l1":{"l2":[[0],{"1":1}]}}', json_encode($json));
32+
33+
$this->assertSame(1, JsonProcessor::getByPath($json, '#/l1/l2/1/1'));
3234
}
3335
}

0 commit comments

Comments
 (0)