Mercurial > hg > cmmr2012-drupal-site
comparison vendor/symfony/yaml/Dumper.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
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 * @final since version 3.4 | |
20 */ | |
21 class 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 /** | |
31 * @param int $indentation | |
32 */ | |
33 public function __construct($indentation = 4) | |
34 { | |
35 if ($indentation < 1) { | |
36 throw new \InvalidArgumentException('The indentation must be greater than zero.'); | |
37 } | |
38 | |
39 $this->indentation = $indentation; | |
40 } | |
41 | |
42 /** | |
43 * Sets the indentation. | |
44 * | |
45 * @param int $num The amount of spaces to use for indentation of nested nodes | |
46 * | |
47 * @deprecated since version 3.1, to be removed in 4.0. Pass the indentation to the constructor instead. | |
48 */ | |
49 public function setIndentation($num) | |
50 { | |
51 @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 3.1 and will be removed in 4.0. Pass the indentation to the constructor instead.', E_USER_DEPRECATED); | |
52 | |
53 $this->indentation = (int) $num; | |
54 } | |
55 | |
56 /** | |
57 * Dumps a PHP value to YAML. | |
58 * | |
59 * @param mixed $input The PHP value | |
60 * @param int $inline The level where you switch to inline YAML | |
61 * @param int $indent The level of indentation (used internally) | |
62 * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string | |
63 * | |
64 * @return string The YAML representation of the PHP value | |
65 */ | |
66 public function dump($input, $inline = 0, $indent = 0, $flags = 0) | |
67 { | |
68 if (is_bool($flags)) { | |
69 @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED); | |
70 | |
71 if ($flags) { | |
72 $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE; | |
73 } else { | |
74 $flags = 0; | |
75 } | |
76 } | |
77 | |
78 if (func_num_args() >= 5) { | |
79 @trigger_error('Passing a boolean flag to toggle object support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED); | |
80 | |
81 if (func_get_arg(4)) { | |
82 $flags |= Yaml::DUMP_OBJECT; | |
83 } | |
84 } | |
85 | |
86 $output = ''; | |
87 $prefix = $indent ? str_repeat(' ', $indent) : ''; | |
88 $dumpObjectAsInlineMap = true; | |
89 | |
90 if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) { | |
91 $dumpObjectAsInlineMap = empty((array) $input); | |
92 } | |
93 | |
94 if ($inline <= 0 || (!is_array($input) && $dumpObjectAsInlineMap) || empty($input)) { | |
95 $output .= $prefix.Inline::dump($input, $flags); | |
96 } else { | |
97 $dumpAsMap = Inline::isHash($input); | |
98 | |
99 foreach ($input as $key => $value) { | |
100 if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r\n")) { | |
101 // If the first line starts with a space character, the spec requires a blockIndicationIndicator | |
102 // http://www.yaml.org/spec/1.2/spec.html#id2793979 | |
103 $blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : ''; | |
104 $output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator); | |
105 | |
106 foreach (preg_split('/\n|\r\n/', $value) as $row) { | |
107 $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row); | |
108 } | |
109 | |
110 continue; | |
111 } | |
112 | |
113 $dumpObjectAsInlineMap = true; | |
114 | |
115 if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) { | |
116 $dumpObjectAsInlineMap = empty((array) $value); | |
117 } | |
118 | |
119 $willBeInlined = $inline - 1 <= 0 || !is_array($value) && $dumpObjectAsInlineMap || empty($value); | |
120 | |
121 $output .= sprintf('%s%s%s%s', | |
122 $prefix, | |
123 $dumpAsMap ? Inline::dump($key, $flags).':' : '-', | |
124 $willBeInlined ? ' ' : "\n", | |
125 $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags) | |
126 ).($willBeInlined ? "\n" : ''); | |
127 } | |
128 } | |
129 | |
130 return $output; | |
131 } | |
132 } |