4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Yaml;
15 * Dumper dumps PHP variables to YAML strings.
17 * @author Fabien Potencier <fabien@symfony.com>
19 * @final since version 3.4
24 * The amount of spaces to use for indentation of nested nodes.
28 protected $indentation;
31 * @param int $indentation
33 public function __construct($indentation = 4)
35 if ($indentation < 1) {
36 throw new \InvalidArgumentException('The indentation must be greater than zero.');
39 $this->indentation = $indentation;
43 * Sets the indentation.
45 * @param int $num The amount of spaces to use for indentation of nested nodes
47 * @deprecated since version 3.1, to be removed in 4.0. Pass the indentation to the constructor instead.
49 public function setIndentation($num)
51 @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 3.1 and will be removed in 4.0. Pass the indentation to the constructor instead.', E_USER_DEPRECATED);
53 $this->indentation = (int) $num;
57 * Dumps a PHP value to YAML.
59 * @param mixed $input The PHP value
60 * @param int $inline The level where you switch to inline YAML
61 * @param int $indent The level of indentation (used internally)
62 * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
64 * @return string The YAML representation of the PHP value
66 public function dump($input, $inline = 0, $indent = 0, $flags = 0)
68 if (\is_bool($flags)) {
69 @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
72 $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
78 if (\func_num_args() >= 5) {
79 @trigger_error('Passing a boolean flag to toggle object support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
81 if (func_get_arg(4)) {
82 $flags |= Yaml::DUMP_OBJECT;
87 $prefix = $indent ? str_repeat(' ', $indent) : '';
88 $dumpObjectAsInlineMap = true;
90 if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
91 $dumpObjectAsInlineMap = empty((array) $input);
94 if ($inline <= 0 || (!\is_array($input) && $dumpObjectAsInlineMap) || empty($input)) {
95 $output .= $prefix.Inline::dump($input, $flags);
97 $dumpAsMap = Inline::isHash($input);
99 foreach ($input as $key => $value) {
100 if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r\n")) {
101 // If the first line starts with a space character, the spec requires a blockIndicationIndicator
102 // http://www.yaml.org/spec/1.2/spec.html#id2793979
103 $blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';
104 $output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);
106 foreach (preg_split('/\n|\r\n/', $value) as $row) {
107 $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
113 $dumpObjectAsInlineMap = true;
115 if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
116 $dumpObjectAsInlineMap = empty((array) $value);
119 $willBeInlined = $inline - 1 <= 0 || !\is_array($value) && $dumpObjectAsInlineMap || empty($value);
121 $output .= sprintf('%s%s%s%s',
123 $dumpAsMap ? Inline::dump($key, $flags).':' : '-',
124 $willBeInlined ? ' ' : "\n",
125 $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
126 ).($willBeInlined ? "\n" : '');