Permalink
Browse files

Added Yaml\Dumper::setIndentation() method to allow a custom indentat…

…ion level of nested nodes.

YAML does not specify an absolute indentation level, but a consistent indentation of nested nodes only: http://www.yaml.org/spec/1.2/spec.html#space/indentation/

Projects that are generally using 2 spaces for indentation should be able to retain consistency with their coding standards by supplying a custom value for the new $indent parameter added to Yaml::dump(), or the new Dumper::setIndentation() method.

The new parameter is a backwards-compatible API addition and defaults to the previous default of 4 (which was changed from 2 via PR #2242 only recently).
  • Loading branch information...
1 parent 5e7bcc7 commit a12975569e2583d67b370cdc85c86f9169b1bdac @sun sun committed Aug 4, 2012
Showing with 21 additions and 2 deletions.
  1. +18 −1 Dumper.php
  2. +3 −1 Yaml.php
View
19 Dumper.php
@@ -19,6 +19,23 @@
class Dumper
{
/**
+ * The amount of spaces to use for indentation of nested nodes.
+ *
+ * @var integer
+ */
+ protected $indentation = 4;
+
+ /**
+ * Sets the indentation.
+ *
+ * @param integer $num The amount of spaces to use for intendation of nested nodes.
+ */
+ public function setIndentation($num)
+ {
+ $this->indentation = $num;
+ }
+
+ /**
* Dumps a PHP value to YAML.
*
* @param mixed $input The PHP value
@@ -44,7 +61,7 @@ public function dump($input, $inline = 0, $indent = 0)
$prefix,
$isAHash ? Inline::dump($key).':' : '-',
$willBeInlined ? ' ' : "\n",
- $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + 4)
+ $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation)
).($willBeInlined ? "\n" : '');
}
}
View
4 Yaml.php
@@ -97,14 +97,16 @@ public static function parse($input)
*
* @param array $array PHP array
* @param integer $inline The level where you switch to inline YAML
+ * @param integer $indent The amount of spaces to use for indentation of nested nodes.
*
* @return string A YAML string representing the original PHP array
*
* @api
*/
- public static function dump($array, $inline = 2)
+ public static function dump($array, $inline = 2, $indent = 4)
{
$yaml = new Dumper();
+ $yaml->setIndentation($indent);
return $yaml->dump($array, $inline);
}

0 comments on commit a129755

Please sign in to comment.