8000 Added Yaml\Dumper::setIndentation() method to allow a custom indentat… · symfony/symfony@3c87e2e · GitHub
[go: up one dir, main page]

Skip to content

Commit 3c87e2e

Browse files
sunfabpot
authored andcommitted
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). Conflicts: src/Symfony/Component/Yaml/Dumper.php src/Symfony/Component/Yaml/Yaml.php
1 parent ba6e315 commit 3c87e2e

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Symfony/Component/Yaml/Dumper.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@
1818
*/
1919
class Dumper
2020
{
21+
/**
22+
* The amount of spaces to use for indentation of nested nodes.
23+
*
24+
* @var integer
25+
*/
26+
protected $indentation = 2;
27+
28+
/**
29+
* Sets the indentation.
30+
*
31+
* @param integer $num The amount of spaces to use for intendation of nested nodes.
32+
*/
33+
public function setIndentation($num)
34+
{
35+
$this->indentation = $num;
36+
}
37+
2138
/**
2239
* Dumps a PHP value to YAML.
2340
*
@@ -46,7 +63,7 @@ public function dump($input, $inline = 0, $exceptionOnInvalidType = false, $obje
4663
$prefix,
4764
$isAHash ? Inline::dump($key, $exceptionOnInvalidType, $objectSupport).':' : '-',
4865
$willBeInlined ? ' ' : "\n",
49-
$this->dump($value, $inline - 1, $exceptionOnInvalidType, $objectSupport, $willBeInlined ? 0 : $indent + 2)
66+
$this->dump($value, $inline - 1, $exceptionOnInvalidType, $objectSupport, $willBeInlined ? 0 : $indent + $this->indentation)
5067
).($willBeInlined ? "\n" : '');
5168
}
5269
}

src/Symfony/Component/Yaml/Yaml.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,18 @@ public static function parse($input, $exceptionOnInvalidType = false, $objectSup
137137
*
138138
* @param array $array PHP array
139139
* @param integer $inline The level where you switch to inline YAML
140+
* @param integer $indent The amount of spaces to use for indentation of nested nodes.
140141
* @param Boolean $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
141142
* @param Boolean $objectSupport true if object support is enabled, false otherwise
142143
*
143144
* @return string A YAML string representing the original PHP array
144145
*
145146
* @api
146147
*/
147-
public static function dump($array, $inline = 2, $exceptionOnInvalidType = false, $objectSupport = false)
148+
public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false)
148149
{
149150
$yaml = new Dumper();
151+
$yaml->setIndentation($indent);
150152

151153
return $yaml->dump($array, $inline, $exceptionOnInvalidType, $objectSupport);
152154
}

0 commit comments

Comments
 (0)
0