Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 621 | Zur aktuellen Revision | 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")) {
70
                    // If the first line starts with a space character, the spec requires a blockIndicationIndicator
71
                    // http://www.yaml.org/spec/1.2/spec.html#id2793979
72
                    $blockIndentationIndicator = str_starts_with($value, ' ') ? (string) $this->indentation : '';
73
 
74
                    if (isset($value[-2]) && "\n" === $value[-2] && "\n" === $value[-1]) {
75
                        $blockChompingIndicator = '+';
76
                    } elseif ("\n" === $value[-1]) {
77
                        $blockChompingIndicator = '';
78
                    } else {
79
                        $blockChompingIndicator = '-';
80
                    }
81
 
82
                    $output .= sprintf('%s%s%s |%s%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator, $blockChompingIndicator);
83
 
84
                    foreach (explode("\n", $value) as $row) {
85
                        if ('' === $row) {
86
                            $output .= "\n";
87
                        } else {
88
                            $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
89
                        }
90
                    }
91
 
92
                    continue;
93
                }
94
 
95
                if ($value instanceof TaggedValue) {
96
                    $output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());
97
 
98
                    if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && str_contains($value->getValue(), "\n") && !str_contains($value->getValue(), "\r\n")) {
99
                        // If the first line starts with a space character, the spec requires a blockIndicationIndicator
100
                        // http://www.yaml.org/spec/1.2/spec.html#id2793979
101
                        $blockIndentationIndicator = str_starts_with($value->getValue(), ' ') ? (string) $this->indentation : '';
102
                        $output .= sprintf(' |%s', $blockIndentationIndicator);
103
 
104
                        foreach (explode("\n", $value->getValue()) as $row) {
105
                            $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
106
                        }
107
 
108
                        continue;
109
                    }
110
 
111
                    if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) {
112
                        $output .= ' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
113
                    } else {
114
                        $output .= "\n";
115
                        $output .= $this->dump($value->getValue(), $inline - 1, $dumpAsMap ? $indent + $this->indentation : $indent + 2, $flags);
116
                    }
117
 
118
                    continue;
119
                }
120
 
121
                $dumpObjectAsInlineMap = true;
122
 
123
                if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
124
                    $dumpObjectAsInlineMap = empty((array) $value);
125
                }
126
 
127
                $willBeInlined = $inline - 1 <= 0 || !\is_array($value) && $dumpObjectAsInlineMap || empty($value);
128
 
129
                $output .= sprintf('%s%s%s%s',
130
                    $prefix,
131
                    $dumpAsMap ? Inline::dump($key, $flags).':' : '-',
132
                    $willBeInlined ? ' ' : "\n",
133
                    $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
134
                ).($willBeInlined ? "\n" : '');
135
            }
136
        }
137
 
138
        return $output;
139
    }
140
 
141
    private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, int $flags, string $prefix): string
142
    {
143
        $output = sprintf('%s!%s', $prefix ? $prefix.' ' : '', $value->getTag());
144
 
688 lars 145
        if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && str_contains($value->getValue(), "\n") && !str_contains($value->getValue(), "\r\n")) {
621 lars 146
            // If the first line starts with a space character, the spec requires a blockIndicationIndicator
147
            // http://www.yaml.org/spec/1.2/spec.html#id2793979
148
            $blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
149
            $output .= sprintf(' |%s', $blockIndentationIndicator);
150
 
151
            foreach (explode("\n", $value->getValue()) as $row) {
152
                $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
153
            }
154
 
155
            return $output;
156
        }
157
 
158
        if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) {
159
            return $output.' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
160
        }
161
 
162
        return $output."\n".$this->dump($value->getValue(), $inline - 1, $indent, $flags);
163
    }
164
}