1
0
Fork 0

Add support for nested arrays in the json manipulator, fixes #3296

pull/3294/merge
Jordi Boggiano 2014-09-24 18:30:53 +01:00
parent eba04dc211
commit 55a6a1c3d4
2 changed files with 70 additions and 2 deletions

View File

@ -18,6 +18,7 @@ namespace Composer\Json;
class JsonManipulator class JsonManipulator
{ {
private static $RECURSE_BLOCKS; private static $RECURSE_BLOCKS;
private static $RECURSE_ARRAYS;
private static $JSON_VALUE; private static $JSON_VALUE;
private static $JSON_STRING; private static $JSON_STRING;
@ -29,8 +30,9 @@ class JsonManipulator
{ {
if (!self::$RECURSE_BLOCKS) { if (!self::$RECURSE_BLOCKS) {
self::$RECURSE_BLOCKS = '(?:[^{}]*|\{(?:[^{}]*|\{(?:[^{}]*|\{(?:[^{}]*|\{[^{}]*\})*\})*\})*\})*'; self::$RECURSE_BLOCKS = '(?:[^{}]*|\{(?:[^{}]*|\{(?:[^{}]*|\{(?:[^{}]*|\{[^{}]*\})*\})*\})*\})*';
self::$JSON_STRING = '"(?:\\\\["bfnrt/\\\\]|\\\\u[a-fA-F0-9]{4}|[^\0-\x09\x0a-\x1f\\\\"])*"'; self::$RECURSE_ARRAYS = '(?:[^\]]*|\[(?:[^\]]*|\[(?:[^\]]*|\[(?:[^\]]*|\[[^\]]*\])*\])*\])*\])*';
self::$JSON_VALUE = '(?:[0-9.]+|null|true|false|'.self::$JSON_STRING.'|\[[^\]]*\]|\{'.self::$RECURSE_BLOCKS.'\})'; self::$JSON_STRING = '"(?:\\\\["bfnrt/\\\\]|\\\\u[a-fA-F0-9]{4}|[^\0-\x09\x0a-\x1f\\\\"])+"';
self::$JSON_VALUE = '(?:[0-9.]+|null|true|false|'.self::$JSON_STRING.'|\['.self::$RECURSE_ARRAYS.'\]|\{'.self::$RECURSE_BLOCKS.'\})';
} }
$contents = trim($contents); $contents = trim($contents);

View File

@ -228,6 +228,54 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase
"foo": "qux" "foo": "qux"
} }
} }
'
),
array(
'{
"repositories": [{
"type": "package",
"package": {
"bar": "ba[z",
"dist": {
"url": "http...",
"type": "zip"
},
"autoload": {
"classmap": [ "foo/bar" ]
}
}
}],
"require": {
"php": "5.*"
},
"require-dev": {
"foo": "bar"
}
}',
'require-dev',
'foo',
'qux',
'{
"repositories": [{
"type": "package",
"package": {
"bar": "ba[z",
"dist": {
"url": "http...",
"type": "zip"
},
"autoload": {
"classmap": [ "foo/bar" ]
}
}
}],
"require": {
"php": "5.*"
},
"require-dev": {
"foo": "qux"
}
}
' '
), ),
); );
@ -468,6 +516,24 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase
"package": { "bar": "ba}z" } "package": { "bar": "ba}z" }
} }
} }
}',
'bar',
false
),
'fails on deep arrays with borked texts' => array(
'{
"repositories": [{
"package": { "bar": "ba[z" }
}]
}',
'bar',
false
),
'fails on deep arrays with borked texts2' => array(
'{
"repositories": [{
"package": { "bar": "ba]z" }
}]
}', }',
'bar', 'bar',
false false