1
0
Fork 0

Fix subkey manipulation when the main key does not exist yet

pull/3982/head
Jordi Boggiano 2015-04-29 22:38:33 +01:00
parent a57c51e8d7
commit 32a479a1e7
2 changed files with 27 additions and 7 deletions

View File

@ -126,18 +126,22 @@ class JsonManipulator
{
$decoded = JsonFile::parseJson($this->contents);
// no main node yet
if (!isset($decoded[$mainNode])) {
$this->addMainKey($mainNode, array($name => $value));
return true;
}
$subName = null;
if (in_array($mainNode, array('config', 'repositories')) && false !== strpos($name, '.')) {
list($name, $subName) = explode('.', $name, 2);
}
// no main node yet
if (!isset($decoded[$mainNode])) {
if ($subName !== null) {
$this->addMainKey($mainNode, array($name => array($subName => $value)));
} else {
$this->addMainKey($mainNode, array($name => $value));
}
return true;
}
// main node content not match-able
$nodeRegex = '{^(\s*\{\s*(?:'.self::$JSON_STRING.'\s*:\s*'.self::$JSON_VALUE.'\s*,\s*)*?)'.
'('.preg_quote(JsonFile::encode($mainNode)).'\s*:\s*\{)('.self::$RECURSE_BLOCKS.')(\})(.*)}s';

View File

@ -803,6 +803,22 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase
', $manipulator->getContents());
}
public function testAddConfigSettingWorksFromScratch()
{
$manipulator = new JsonManipulator('{
}');
$this->assertTrue($manipulator->addConfigSetting('foo.bar', 'baz'));
$this->assertEquals('{
"config": {
"foo": {
"bar": "baz"
}
}
}
', $manipulator->getContents());
}
public function testAddConfigSettingCanAdd()
{
$manipulator = new JsonManipulator('{