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 = 4;
29 * Sets the indentation.
31 * @param int $num The amount of spaces to use for indentation of nested nodes
33 public function setIndentation($num)
36 throw new \
InvalidArgumentException('The indentation must be greater than zero.');
39 $this->indentation
= (int) $num;
43 * Dumps a PHP value to YAML.
45 * @param mixed $input The PHP value
46 * @param int $inline The level where you switch to inline YAML
47 * @param int $indent The level of indentation (used internally)
48 * @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
49 * @param bool $objectSupport True if object support is enabled, false otherwise
51 * @return string The YAML representation of the PHP value
53 public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = false, $objectSupport = false)
56 $prefix = $indent ?
str_repeat(' ', $indent) : '';
58 if ($inline <= 0 ||
!is_array($input) ||
empty($input)) {
59 $output .= $prefix.Inline
::dump($input, $exceptionOnInvalidType, $objectSupport);
61 $isAHash = Inline
::isHash($input);
63 foreach ($input as $key => $value) {
64 $willBeInlined = $inline - 1 <= 0 ||
!is_array($value) ||
empty($value);
66 $output .= sprintf('%s%s%s%s',
68 $isAHash ? Inline
::dump($key, $exceptionOnInvalidType, $objectSupport).':' : '-',
69 $willBeInlined ?
' ' : "\n",
70 $this->dump($value, $inline - 1, $willBeInlined ?
0 : $indent +
$this->indentation
, $exceptionOnInvalidType, $objectSupport)
71 ).($willBeInlined ?
"\n" : '');