Permalink
Browse files

Fix handling of arrays in json manipulator

  • Loading branch information...
1 parent ef68125 commit 24963fb4c8648ba7bfdedf3eca2b56cb03cf3314 @Seldaek Seldaek committed Nov 13, 2012
Showing with 42 additions and 3 deletions.
  1. +7 −3 src/Composer/Json/JsonManipulator.php
  2. +35 −0 tests/Composer/Test/Json/JsonManipulatorTest.php
View
10 src/Composer/Json/JsonManipulator.php
@@ -119,8 +119,8 @@ public function addSubNode($mainNode, $name, $value)
}
// 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 @@ public function removeSubNode($mainNode, $name)
}
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 @@ protected function format($data, $depth = 0)
reset($data);
if (is_numeric(key($data))) {
+ foreach ($data as $key => $val) {
+ $data[$key] = $this->format($val, $depth + 1);
+ }
+
return '['.implode(', ', $data).']';
}
View
35 tests/Composer/Test/Json/JsonManipulatorTest.php
@@ -446,4 +446,39 @@ public function testAddConfigSettingCanOverwriteNumbers()
}
', $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());
+ }
}

0 comments on commit 24963fb

Please sign in to comment.