Skip to content

Commit c06cd77

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

File tree

6 files changed

+51
-9
lines changed

6 files changed

+51
-9
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
/.idea
33
/json-diff
44
/json-diff.tar.gz
5-
/composer.lock
5+
/composer.lock
6+
/composer.phar

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ cache:
1919
# execute any number of scripts before the test run, custom env's are available as variables
2020
before_script:
2121
- composer install --dev --no-interaction --prefer-dist
22+
- cat composer.lock
2223

2324
matrix:
2425
allow_failures:

Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
phar:
22
composer require php-yaoi/php-yaoi:^1;composer install --no-dev;rm -rf tests/;rm ./json-diff;rm ./json-diff.tar.gz;phar-composer build;mv ./json-diff.phar ./json-diff;tar -zcvf ./json-diff.tar.gz ./json-diff;git reset --hard;composer install
3+
4+
docker56-composer-update:
5+
test -f ./composer.phar || wget https://getcomposer.org/composer.phar
6+
docker run -v $$(pwd):/code php:5.6-cli bash -c "apt-get update;apt-get install -y unzip;cd /code;php composer.phar update --prefer-source"
7+
8+
docker56-test:
9+
docker run -v $$(pwd):/code php:5.6-cli bash -c "cd /code;php vendor/bin/phpunit"

src/Cli/Diff.php

+1-1
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.1';
32+
$definition->version = 'v1.1.2';
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

+33-6
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,43 @@ public static function pushByPath(&$holder, $path, $value)
2222
&& !is_int($key)
2323
&& false === filter_var($key, FILTER_VALIDATE_INT)
2424
) {
25+
$key = (string)$key;
2526
$ref = new \stdClass();
26-
$ref = &$ref->$key;
27+
$ref = &$ref->{$key};
2728
} else {
2829
$ref = &$ref[$key];
2930
}
3031
}
3132
$ref = $value;
3233
}
3334

34-
public static function getByPath(&$holder, $path)
35+
private static function arrayKeyExists($key, array $a)
36+
{
37+
if (array_key_exists($key, $a)) {
38+
return true;
39+
}
40+
$key = (string)$key;
41+
foreach ($a as $k => $v) {
42+
if ((string)$k === $key) {
43+
return true;
44+
}
45+
}
46+
return false;
47+
}
48+
49+
private static function arrayGet($key, array $a)
50+
{
51+
$key = (string)$key;
52+
foreach ($a as $k => $v) {
53+
if ((string)$k === $key) {
54+
return $v;
55+
}
56+
}
57+
return false;
58+
}
59+
60+
61+
public static function getByPath($holder, $path)
3562
{
3663
$pathItems = explode('/', $path);
3764
if ('#' === $pathItems[0]) {
@@ -41,14 +68,14 @@ public static function getByPath(&$holder, $path)
4168
while (null !== $key = array_shift($pathItems)) {
4269
$key = urldecode($key);
4370
if ($ref instanceof \stdClass) {
44-
$vars = get_object_vars($ref);
45-
if (array_key_exists($key, $vars)) {
46-
$ref = $vars[$key];
71+
$vars = (array)$ref;
72+
if (self::arrayKeyExists($key, $vars)) {
73+
$ref = self::arrayGet($key, $vars);
4774
} else {
4875
throw new Exception('Key not found: ' . $key);
4976
}
5077
} else {
51-
if (array_key_exists($key, $ref)) {
78+
if (self::arrayKeyExists($key, $ref)) {
5279
$ref = $ref[$key];
5380
} else {
5481
throw new Exception('Key not found: ' . $key);

tests/src/ProcessorTest.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,10 @@ public function testProcess()
3232

3333
$this->assertSame(1, JsonProcessor::getByPath($json, '#/l1/l2/1/1'));
3434
}
35-
}
35+
36+
public function testNumericKey()
37+
{
38+
$json = json_decode('{"l1":{"200":1}}');
39+
$this->assertSame(1, JsonProcessor::getByPath($json, '#/l1/200'));
40+
}
41+
}

0 commit comments

Comments
 (0)