1
0
Fork 0

Fix handling of arrays in json manipulator

pull/1328/head
Jordi Boggiano 2012-11-13 12:49:23 +01:00
parent ef68125b3b
commit 24963fb4c8
2 changed files with 42 additions and 3 deletions

View File

@ -119,8 +119,8 @@ class JsonManipulator
}
// child exists
if (preg_match('{("'.preg_quote($name).'"\s*:\s*)([0-9.]+|null|true|false|"[^"]+"|\{'.self::$RECURSE_BLOCKS.'\})(,?)}', $children, $matches)) {
$children = preg_replace('{("'.preg_quote($name).'"\s*:\s*)([0-9.]+|null|true|false|"[^"]+"|\{'.self::$RECURSE_BLOCKS.'\})(,?)}', '${1}'.$this->format($value, 1).'$3', $children);
if (preg_match('{("'.preg_quote($name).'"\s*:\s*)([0-9.]+|null|true|false|"[^"]+"|\[[^\]]*\]|\{'.self::$RECURSE_BLOCKS.'\})(,?)}', $children, $matches)) {
$children = preg_replace('{("'.preg_quote($name).'"\s*:\s*)([0-9.]+|null|true|false|"[^"]+"|\[[^\]]*\]|\{'.self::$RECURSE_BLOCKS.'\})(,?)}', '${1}'.$this->format($value, 1).'$3', $children);
} elseif (preg_match('#[^\s](\s*)$#', $children, $match)) {
// child missing but non empty children
$children = preg_replace(
@ -164,7 +164,7 @@ class JsonManipulator
}
if (preg_match('{"'.preg_quote($name).'"\s*:}i', $children)) {
if (preg_match_all('{"'.preg_quote($name).'"\s*:\s*(?:[0-9.]+|null|true|false|"[^"]+"|\{'.self::$RECURSE_BLOCKS.'\})}', $children, $matches)) {
if (preg_match_all('{"'.preg_quote($name).'"\s*:\s*(?:[0-9.]+|null|true|false|"[^"]+"|\[[^\]]*\]|\{'.self::$RECURSE_BLOCKS.'\})}', $children, $matches)) {
$bestMatch = '';
foreach ($matches[0] as $match) {
if (strlen($bestMatch) < strlen($match)) {
@ -215,6 +215,10 @@ class JsonManipulator
reset($data);
if (is_numeric(key($data))) {
foreach ($data as $key => $val) {
$data[$key] = $this->format($val, $depth + 1);
}
return '['.implode(', ', $data).']';
}

View File

@ -444,6 +444,41 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase
"foo": 50
}
}
', $manipulator->getContents());
}
public function testAddConfigSettingCanOverwriteArrays()
{
$manipulator = new JsonManipulator('{
"config": {
"github-oauth": {
"github.com": "foo"
},
"github-protocols": ["https"]
}
}');
$this->assertTrue($manipulator->addConfigSetting('github-protocols', array('https', 'http')));
$this->assertEquals('{
"config": {
"github-oauth": {
"github.com": "foo"
},
"github-protocols": ["https", "http"]
}
}
', $manipulator->getContents());
$this->assertTrue($manipulator->addConfigSetting('github-oauth', array('github.com' => 'bar', 'alt.example.org' => 'baz')));
$this->assertEquals('{
"config": {
"github-oauth": {
"github.com": "bar",
"alt.example.org": "baz"
},
"github-protocols": ["https", "http"]
}
}
', $manipulator->getContents());
}
}