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>
22 * The amount of spaces to use for indentation of nested nodes.
26 protected $indentation;
29 * @param int $indentation
31 public function __construct($indentation = 4)
33 if ($indentation < 1) {
34 throw new \InvalidArgumentException('The indentation must be greater than zero.');
37 $this->indentation = $indentation;
41 * Sets the indentation.
43 * @param int $num The amount of spaces to use for indentation of nested nodes
45 * @deprecated since version 3.1, to be removed in 4.0. Pass the indentation to the constructor instead.
47 public function setIndentation($num)
49 @trigger_error('The '.__METHOD__.' method is deprecated since version 3.1 and will be removed in 4.0. Pass the indentation to the constructor instead.', E_USER_DEPRECATED);
51 $this->indentation = (int) $num;
55 * Dumps a PHP value to YAML.
57 * @param mixed $input The PHP value
58 * @param int $inline The level where you switch to inline YAML
59 * @param int $indent The level of indentation (used internally)
60 * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
62 * @return string The YAML representation of the PHP value
64 public function dump($input, $inline = 0, $indent = 0, $flags = 0)
66 if (is_bool($flags)) {
67 @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
70 $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
76 if (func_num_args() >= 5) {
77 @trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
79 if (func_get_arg(4)) {
80 $flags |= Yaml::DUMP_OBJECT;
85 $prefix = $indent ? str_repeat(' ', $indent) : '';
86 $dumpObjectAsInlineMap = true;
88 if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
89 $dumpObjectAsInlineMap = empty((array) $input);
92 if ($inline <= 0 || (!is_array($input) && $dumpObjectAsInlineMap) || empty($input)) {
93 $output .= $prefix.Inline::dump($input, $flags);
95 $dumpAsMap = Inline::isHash($input);
97 foreach ($input as $key => $value) {
98 if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && is_string($value) && false !== strpos($value, "\n")) {
99 $output .= sprintf("%s%s%s |\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '');
101 foreach (preg_split('/\n|\r\n/', $value) as $row) {
102 $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
108 $dumpObjectAsInlineMap = true;
110 if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
111 $dumpObjectAsInlineMap = empty((array) $value);
114 $willBeInlined = $inline - 1 <= 0 || !is_array($value) && $dumpObjectAsInlineMap || empty($value);
116 $output .= sprintf('%s%s%s%s',
118 $dumpAsMap ? Inline::dump($key, $flags).':' : '-',
119 $willBeInlined ? ' ' : "\n",
120 $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
121 ).($willBeInlined ? "\n" : '');