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