Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 688 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
621 lars 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
use Symfony\Component\Yaml\Tag\TaggedValue;
15
 
16
/**
17
 * Dumper dumps PHP variables to YAML strings.
18
 *
19
 * @author Fabien Potencier <fabien@symfony.com>
20
 *
21
 * @final
22
 */
23
class Dumper
24
{
25
    /**
26
     * The amount of spaces to use for indentation of nested nodes.
27
     */
28
    private int $indentation;
29
 
30
    public function __construct(int $indentation = 4)
31
    {
32
        if ($indentation < 1) {
33
            throw new \InvalidArgumentException('The indentation must be greater than zero.');
34
        }
35
 
36
        $this->indentation = $indentation;
37
    }
38
 
39
    /**
40
     * Dumps a PHP value to YAML.
41
     *
42
     * @param mixed $input  The PHP value
43
     * @param int   $inline The level where you switch to inline YAML
44
     * @param int   $indent The level of indentation (used internally)
45
     * @param int   $flags  A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
46
     */
47
    public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags = 0): string
48
    {
49
        $output = '';
50
        $prefix = $indent ? str_repeat(' ', $indent) : '';
51
        $dumpObjectAsInlineMap = true;
52
 
53
        if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
54
            $dumpObjectAsInlineMap = empty((array) $input);
55
        }
56
 
57
        if ($inline <= 0 || (!\is_array($input) && !$input instanceof TaggedValue && $dumpObjectAsInlineMap) || empty($input)) {
58
            $output .= $prefix.Inline::dump($input, $flags);
59
        } elseif ($input instanceof TaggedValue) {
60
            $output .= $this->dumpTaggedValue($input, $inline, $indent, $flags, $prefix);
61
        } else {
62
            $dumpAsMap = Inline::isHash($input);
63
 
64
            foreach ($input as $key => $value) {
65
                if ('' !== $output && "\n" !== $output[-1]) {
66
                    $output .= "\n";
67
                }
68
 
69
                if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && str_contains($value, "\n") && !str_contains($value, "\r")) {
1663 lars 70
                    $blockIndentationIndicator = $this->getBlockIndentationIndicator($value);
621 lars 71
 
72
                    if (isset($value[-2]) && "\n" === $value[-2] && "\n" === $value[-1]) {
73
                        $blockChompingIndicator = '+';
74
                    } elseif ("\n" === $value[-1]) {
75
                        $blockChompingIndicator = '';
76
                    } else {
77
                        $blockChompingIndicator = '-';
78
                    }
79
 
80
                    $output .= sprintf('%s%s%s |%s%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator, $blockChompingIndicator);
81
 
82
                    foreach (explode("\n", $value) as $row) {
83
                        if ('' === $row) {
84
                            $output .= "\n";
85
                        } else {
86
                            $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
87
                        }
88
                    }
89
 
90
                    continue;
91
                }
92
 
93
                if ($value instanceof TaggedValue) {
94
                    $output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());
95
 
96
                    if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && str_contains($value->getValue(), "\n") && !str_contains($value->getValue(), "\r\n")) {
1663 lars 97
                        $blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue());
621 lars 98
                        $output .= sprintf(' |%s', $blockIndentationIndicator);
99
 
100
                        foreach (explode("\n", $value->getValue()) as $row) {
101
                            $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
102
                        }
103
 
104
                        continue;
105
                    }
106
 
107
                    if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) {
108
                        $output .= ' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
109
                    } else {
110
                        $output .= "\n";
111
                        $output .= $this->dump($value->getValue(), $inline - 1, $dumpAsMap ? $indent + $this->indentation : $indent + 2, $flags);
112
                    }
113
 
114
                    continue;
115
                }
116
 
117
                $dumpObjectAsInlineMap = true;
118
 
119
                if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
120
                    $dumpObjectAsInlineMap = empty((array) $value);
121
                }
122
 
123
                $willBeInlined = $inline - 1 <= 0 || !\is_array($value) && $dumpObjectAsInlineMap || empty($value);
124
 
125
                $output .= sprintf('%s%s%s%s',
126
                    $prefix,
127
                    $dumpAsMap ? Inline::dump($key, $flags).':' : '-',
128
                    $willBeInlined ? ' ' : "\n",
129
                    $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
130
                ).($willBeInlined ? "\n" : '');
131
            }
132
        }
133
 
134
        return $output;
135
    }
136
 
137
    private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, int $flags, string $prefix): string
138
    {
139
        $output = sprintf('%s!%s', $prefix ? $prefix.' ' : '', $value->getTag());
140
 
688 lars 141
        if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && str_contains($value->getValue(), "\n") && !str_contains($value->getValue(), "\r\n")) {
1663 lars 142
            $blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue());
621 lars 143
            $output .= sprintf(' |%s', $blockIndentationIndicator);
144
 
145
            foreach (explode("\n", $value->getValue()) as $row) {
146
                $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
147
            }
148
 
149
            return $output;
150
        }
151
 
152
        if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) {
153
            return $output.' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
154
        }
155
 
156
        return $output."\n".$this->dump($value->getValue(), $inline - 1, $indent, $flags);
157
    }
1663 lars 158
 
159
    private function getBlockIndentationIndicator(string $value): string
160
    {
161
        $lines = explode("\n", $value);
162
 
163
        // If the first line (that is neither empty nor contains only spaces)
164
        // starts with a space character, the spec requires a block indentation indicator
165
        // http://www.yaml.org/spec/1.2/spec.html#id2793979
166
        foreach ($lines as $line) {
167
            if ('' !== trim($line, ' ')) {
168
                return (' ' === substr($line, 0, 1)) ? (string) $this->indentation : '';
169
            }
170
        }
171
 
172
        return '';
173
    }
621 lars 174
}