1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Yaml;
13
14/**
15 * Dumper dumps PHP variables to YAML strings.
16 *
17 * @author Fabien Potencier <fabien@symfony.com>
18 *
19 * @final
20 */
21class Dumper
22{
23    /**
24     * The amount of spaces to use for indentation of nested nodes.
25     *
26     * @var int
27     */
28    protected $indentation;
29
30    public function __construct(int $indentation = 4)
31    {
32        if ($indentation < 1) {
33            throw new \InvalidArgumentException('The indentation must be greater than zero.');
34        }
35
36        $this->indentation = $indentation;
37    }
38
39    /**
40     * Dumps a PHP value to YAML.
41     *
42     * @param mixed $input  The PHP value
43     * @param int   $inline The level where you switch to inline YAML
44     * @param int   $indent The level of indentation (used internally)
45     * @param int   $flags  A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
46     *
47     * @return string The YAML representation of the PHP value
48     */
49    public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0): string
50    {
51        $output = '';
52        $prefix = $indent ? str_repeat(' ', $indent) : '';
53        $dumpObjectAsInlineMap = true;
54
55        if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
56            $dumpObjectAsInlineMap = empty((array) $input);
57        }
58
59        if ($inline <= 0 || (!\is_array($input) && $dumpObjectAsInlineMap) || empty($input)) {
60            $output .= $prefix.Inline::dump($input, $flags);
61        } else {
62            $dumpAsMap = Inline::isHash($input);
63
64            foreach ($input as $key => $value) {
65                if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r\n")) {
66                    // If the first line starts with a space character, the spec requires a blockIndicationIndicator
67                    // http://www.yaml.org/spec/1.2/spec.html#id2793979
68                    $blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';
69                    $output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);
70
71                    foreach (preg_split('/\n|\r\n/', $value) as $row) {
72                        $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
73                    }
74
75                    continue;
76                }
77
78                $dumpObjectAsInlineMap = true;
79
80                if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
81                    $dumpObjectAsInlineMap = empty((array) $value);
82                }
83
84                $willBeInlined = $inline - 1 <= 0 || !\is_array($value) && $dumpObjectAsInlineMap || empty($value);
85
86                $output .= sprintf('%s%s%s%s',
87                    $prefix,
88                    $dumpAsMap ? Inline::dump($key, $flags).':' : '-',
89                    $willBeInlined ? ' ' : "\n",
90                    $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
91                ).($willBeInlined ? "\n" : '');
92            }
93        }
94
95        return $output;
96    }
97}
98