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 | |
---|
12 | namespace Symfony\Component\Yaml; |
---|
13 | |
---|
14 | /** |
---|
15 | * Dumper dumps PHP variables to YAML strings. |
---|
16 | * |
---|
17 | * @author Fabien Potencier <fabien@symfony.com> |
---|
18 | */ |
---|
19 | class Dumper |
---|
20 | { |
---|
21 | /** |
---|
22 | * Dumps a PHP value to YAML. |
---|
23 | * |
---|
24 | * @param mixed $input The PHP value |
---|
25 | * @param integer $inline The level where you switch to inline YAML |
---|
26 | * @param integer $indent The level of indentation (used internally) |
---|
27 | * |
---|
28 | * @return string The YAML representation of the PHP value |
---|
29 | */ |
---|
30 | public function dump($input, $inline = 0, $indent = 0) |
---|
31 | { |
---|
32 | $output = ''; |
---|
33 | $prefix = $indent ? str_repeat(' ', $indent) : ''; |
---|
34 | |
---|
35 | if ($inline <= 0 || !is_array($input) || empty($input)) { |
---|
36 | $output .= $prefix.Inline::dump($input); |
---|
37 | } else { |
---|
38 | $isAHash = array_keys($input) !== range(0, count($input) - 1); |
---|
39 | |
---|
40 | foreach ($input as $key => $value) { |
---|
41 | $willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value); |
---|
42 | |
---|
43 | $output .= sprintf('%s%s%s%s', |
---|
44 | $prefix, |
---|
45 | $isAHash ? Inline::dump($key).':' : '-', |
---|
46 | $willBeInlined ? ' ' : "\n", |
---|
47 | $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + 2) |
---|
48 | ).($willBeInlined ? "\n" : ''); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | return $output; |
---|
53 | } |
---|
54 | } |
---|