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

Skip to content

Commit 5be7237

Browse files
committed
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).
1 parent c99f9d2 commit 5be7237

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 = 4;
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
*
@@ -44,7 +61,7 @@ public function dump($input, $inline = 0, $indent = 0)
4461
$prefix,
4562
$isAHash ? Inline::dump($key).':' : '-',
4663
$willBeInlined ? ' ' : "\n",
47-
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + 4)
64+
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation)
4865
).($willBeInlined ? "\n" : '');
4966
}
5067
}

src/Symfony/Component/Yaml/Yaml.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,16 @@ public static function parse($input)
9797
*
9898
* @param array $array PHP array
9999
* @param integer $inline The level where you switch to inline YAML
100+
* @param integer $indent The amount of spaces to use for indentation of nested nodes.
100101
*
101102
* @return string A YAML string representing the original PHP array
102103
*
103104
* @api
104105
*/
105-
public static function dump($array, $inline = 2)
106+
public static function dump($array, $inline = 2, $indent = 4)
106107
{
107108
$yaml = new Dumper();
109+
$yaml->setIndentation($indent);
108110

109111
return $yaml->dump($array, $inline);
110112
}

0 commit comments

Comments
 (0)
0