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

View File

@ -444,6 +444,41 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase
"foo": 50 "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()); ', $manipulator->getContents());
} }
} }