comparison vendor/symfony/yaml/Dumper.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 * The amount of spaces to use for indentation of nested nodes.
23 *
24 * @var int
25 */
26 protected $indentation;
27
28 /**
29 * @param int $indentation
30 */
31 public function __construct($indentation = 4)
32 {
33 if ($indentation < 1) {
34 throw new \InvalidArgumentException('The indentation must be greater than zero.');
35 }
36
37 $this->indentation = $indentation;
38 }
39
40 /**
41 * Sets the indentation.
42 *
43 * @param int $num The amount of spaces to use for indentation of nested nodes
44 */
45 public function setIndentation($num)
46 {
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);
48
49 $this->indentation = (int) $num;
50 }
51
52 /**
53 * Dumps a PHP value to YAML.
54 *
55 * @param mixed $input The PHP value
56 * @param int $inline The level where you switch to inline YAML
57 * @param int $indent The level of indentation (used internally)
58 * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
59 *
60 * @return string The YAML representation of the PHP value
61 */
62 public function dump($input, $inline = 0, $indent = 0, $flags = 0)
63 {
64 if (is_bool($flags)) {
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);
66
67 if ($flags) {
68 $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
69 } else {
70 $flags = 0;
71 }
72 }
73
74 if (func_num_args() >= 5) {
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);
76
77 if (func_get_arg(4)) {
78 $flags |= Yaml::DUMP_OBJECT;
79 }
80 }
81
82 $output = '';
83 $prefix = $indent ? str_repeat(' ', $indent) : '';
84
85 if ($inline <= 0 || !is_array($input) || empty($input)) {
86 $output .= $prefix.Inline::dump($input, $flags);
87 } else {
88 $isAHash = Inline::isHash($input);
89
90 foreach ($input as $key => $value) {
91 if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && is_string($value) && false !== strpos($value, "\n")) {
92 $output .= sprintf("%s%s%s |\n", $prefix, $isAHash ? Inline::dump($key, $flags).':' : '-', '');
93
94 foreach (preg_split('/\n|\r\n/', $value) as $row) {
95 $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
96 }
97
98 continue;
99 }
100
101 $willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);
102
103 $output .= sprintf('%s%s%s%s',
104 $prefix,
105 $isAHash ? Inline::dump($key, $flags).':' : '-',
106 $willBeInlined ? ' ' : "\n",
107 $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
108 ).($willBeInlined ? "\n" : '');
109 }
110 }
111
112 return $output;
113 }
114 }