Projekt

Obecné

Profil

Stáhnout (4.4 KB) Statistiky
| Větev: | Revize:
1 cb15593b Cajova-Houba
<?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
     * @deprecated since version 3.1, to be removed in 4.0. Pass the indentation to the constructor instead.
46
     */
47
    public function setIndentation($num)
48
    {
49
        @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);
50
51
        $this->indentation = (int) $num;
52
    }
53
54
    /**
55
     * Dumps a PHP value to YAML.
56
     *
57
     * @param mixed $input  The PHP value
58
     * @param int   $inline The level where you switch to inline YAML
59
     * @param int   $indent The level of indentation (used internally)
60
     * @param int   $flags  A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
61
     *
62
     * @return string The YAML representation of the PHP value
63
     */
64
    public function dump($input, $inline = 0, $indent = 0, $flags = 0)
65
    {
66
        if (is_bool($flags)) {
67
            @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);
68
69
            if ($flags) {
70
                $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
71
            } else {
72
                $flags = 0;
73
            }
74
        }
75
76
        if (func_num_args() >= 5) {
77
            @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);
78
79
            if (func_get_arg(4)) {
80
                $flags |= Yaml::DUMP_OBJECT;
81
            }
82
        }
83
84
        $output = '';
85
        $prefix = $indent ? str_repeat(' ', $indent) : '';
86
        $dumpObjectAsInlineMap = true;
87
88
        if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
89
            $dumpObjectAsInlineMap = empty((array) $input);
90
        }
91
92
        if ($inline <= 0 || (!is_array($input) && $dumpObjectAsInlineMap) || empty($input)) {
93
            $output .= $prefix.Inline::dump($input, $flags);
94
        } else {
95
            $dumpAsMap = Inline::isHash($input);
96
97
            foreach ($input as $key => $value) {
98
                if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r\n")) {
99
                    $output .= sprintf("%s%s%s |\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '');
100
101
                    foreach (preg_split('/\n|\r\n/', $value) as $row) {
102
                        $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
103
                    }
104
105
                    continue;
106
                }
107
108
                $dumpObjectAsInlineMap = true;
109
110
                if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
111
                    $dumpObjectAsInlineMap = empty((array) $value);
112
                }
113
114
                $willBeInlined = $inline - 1 <= 0 || !is_array($value) && $dumpObjectAsInlineMap || empty($value);
115
116
                $output .= sprintf('%s%s%s%s',
117
                    $prefix,
118
                    $dumpAsMap ? Inline::dump($key, $flags).':' : '-',
119
                    $willBeInlined ? ' ' : "\n",
120
                    $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
121
                ).($willBeInlined ? "\n" : '');
122
            }
123
        }
124
125
        return $output;
126
    }
127
}