Skip to content

Commit

Permalink
Added Yaml\Dumper::setIndentation() method to allow a custom indentat…
Browse files Browse the repository at this point in the history
…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).

Conflicts:
	src/Symfony/Component/Yaml/Dumper.php
	src/Symfony/Component/Yaml/Yaml.php
  • Loading branch information
sun authored and fabpot committed Jan 17, 2013
1 parent ba6e315 commit 3c87e2e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/Symfony/Component/Yaml/Dumper.php
Expand Up @@ -18,6 +18,23 @@
*/
class Dumper
{
/**
* The amount of spaces to use for indentation of nested nodes.
*
* @var integer
*/
protected $indentation = 2;

/**
* 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.
*
Expand Down Expand Up @@ -46,7 +63,7 @@ public function dump($input, $inline = 0, $exceptionOnInvalidType = false, $obje
$prefix,
$isAHash ? Inline::dump($key, $exceptionOnInvalidType, $objectSupport).':' : '-',
$willBeInlined ? ' ' : "\n",
$this->dump($value, $inline - 1, $exceptionOnInvalidType, $objectSupport, $willBeInlined ? 0 : $indent + 2)
$this->dump($value, $inline - 1, $exceptionOnInvalidType, $objectSupport, $willBeInlined ? 0 : $indent + $this->indentation)
).($willBeInlined ? "\n" : '');
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Yaml/Yaml.php
Expand Up @@ -137,16 +137,18 @@ public static function parse($input, $exceptionOnInvalidType = false, $objectSup
*
* @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.
* @param Boolean $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
* @param Boolean $objectSupport true if object support is enabled, false otherwise
*
* @return string A YAML string representing the original PHP array
*
* @api
*/
public static function dump($array, $inline = 2, $exceptionOnInvalidType = false, $objectSupport = false)
public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false)
{
$yaml = new Dumper();
$yaml->setIndentation($indent);

return $yaml->dump($array, $inline, $exceptionOnInvalidType, $objectSupport);
}
Expand Down

0 comments on commit 3c87e2e

Please sign in to comment.