From 5be7237b69a5157388cd912f8bcbab441caedd20 Mon Sep 17 00:00:00 2001 From: sun Date: Sun, 5 Aug 2012 00:46:53 +0200 Subject: [PATCH 1/2] Added Yaml\Dumper::setIndentation() method to allow a custom indentation 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). --- src/Symfony/Component/Yaml/Dumper.php | 19 ++++++++++++++++++- src/Symfony/Component/Yaml/Yaml.php | 4 +++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Yaml/Dumper.php b/src/Symfony/Component/Yaml/Dumper.php index 2338916985..5e0482fabd 100644 --- a/src/Symfony/Component/Yaml/Dumper.php +++ b/src/Symfony/Component/Yaml/Dumper.php @@ -18,6 +18,23 @@ namespace Symfony\Component\Yaml; */ 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. * @@ -44,7 +61,7 @@ class Dumper $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" : ''); } } diff --git a/src/Symfony/Component/Yaml/Yaml.php b/src/Symfony/Component/Yaml/Yaml.php index d35e67d62c..85cdf3db66 100644 --- a/src/Symfony/Component/Yaml/Yaml.php +++ b/src/Symfony/Component/Yaml/Yaml.php @@ -97,14 +97,16 @@ class Yaml * * @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); } From 3e1a1abd5dda9aa2ae6e34f2237137f83a998f4f Mon Sep 17 00:00:00 2001 From: sun Date: Tue, 7 Aug 2012 09:40:56 +0200 Subject: [PATCH 2/2] Force the value of Dumper::setIndentation($num) to be of type integer. --- src/Symfony/Component/Yaml/Dumper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Yaml/Dumper.php b/src/Symfony/Component/Yaml/Dumper.php index 5e0482fabd..220cb39742 100644 --- a/src/Symfony/Component/Yaml/Dumper.php +++ b/src/Symfony/Component/Yaml/Dumper.php @@ -32,7 +32,7 @@ class Dumper */ public function setIndentation($num) { - $this->indentation = $num; + $this->indentation = (int) $num; } /**