annotate vendor/symfony/yaml/Dumper.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Yaml;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Dumper dumps PHP variables to YAML strings.
Chris@0 16 *
Chris@0 17 * @author Fabien Potencier <fabien@symfony.com>
Chris@14 18 *
Chris@14 19 * @final since version 3.4
Chris@0 20 */
Chris@0 21 class Dumper
Chris@0 22 {
Chris@0 23 /**
Chris@0 24 * The amount of spaces to use for indentation of nested nodes.
Chris@0 25 *
Chris@0 26 * @var int
Chris@0 27 */
Chris@0 28 protected $indentation;
Chris@0 29
Chris@0 30 /**
Chris@0 31 * @param int $indentation
Chris@0 32 */
Chris@0 33 public function __construct($indentation = 4)
Chris@0 34 {
Chris@0 35 if ($indentation < 1) {
Chris@0 36 throw new \InvalidArgumentException('The indentation must be greater than zero.');
Chris@0 37 }
Chris@0 38
Chris@0 39 $this->indentation = $indentation;
Chris@0 40 }
Chris@0 41
Chris@0 42 /**
Chris@0 43 * Sets the indentation.
Chris@0 44 *
Chris@0 45 * @param int $num The amount of spaces to use for indentation of nested nodes
Chris@12 46 *
Chris@12 47 * @deprecated since version 3.1, to be removed in 4.0. Pass the indentation to the constructor instead.
Chris@0 48 */
Chris@0 49 public function setIndentation($num)
Chris@0 50 {
Chris@14 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);
Chris@0 52
Chris@0 53 $this->indentation = (int) $num;
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Dumps a PHP value to YAML.
Chris@0 58 *
Chris@0 59 * @param mixed $input The PHP value
Chris@0 60 * @param int $inline The level where you switch to inline YAML
Chris@0 61 * @param int $indent The level of indentation (used internally)
Chris@0 62 * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
Chris@0 63 *
Chris@0 64 * @return string The YAML representation of the PHP value
Chris@0 65 */
Chris@0 66 public function dump($input, $inline = 0, $indent = 0, $flags = 0)
Chris@0 67 {
Chris@17 68 if (\is_bool($flags)) {
Chris@14 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);
Chris@0 70
Chris@0 71 if ($flags) {
Chris@0 72 $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
Chris@0 73 } else {
Chris@0 74 $flags = 0;
Chris@0 75 }
Chris@0 76 }
Chris@0 77
Chris@17 78 if (\func_num_args() >= 5) {
Chris@14 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);
Chris@0 80
Chris@0 81 if (func_get_arg(4)) {
Chris@0 82 $flags |= Yaml::DUMP_OBJECT;
Chris@0 83 }
Chris@0 84 }
Chris@0 85
Chris@0 86 $output = '';
Chris@0 87 $prefix = $indent ? str_repeat(' ', $indent) : '';
Chris@12 88 $dumpObjectAsInlineMap = true;
Chris@0 89
Chris@12 90 if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
Chris@12 91 $dumpObjectAsInlineMap = empty((array) $input);
Chris@12 92 }
Chris@12 93
Chris@17 94 if ($inline <= 0 || (!\is_array($input) && $dumpObjectAsInlineMap) || empty($input)) {
Chris@0 95 $output .= $prefix.Inline::dump($input, $flags);
Chris@0 96 } else {
Chris@12 97 $dumpAsMap = Inline::isHash($input);
Chris@0 98
Chris@0 99 foreach ($input as $key => $value) {
Chris@17 100 if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r\n")) {
Chris@14 101 // If the first line starts with a space character, the spec requires a blockIndicationIndicator
Chris@14 102 // http://www.yaml.org/spec/1.2/spec.html#id2793979
Chris@14 103 $blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';
Chris@14 104 $output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);
Chris@0 105
Chris@0 106 foreach (preg_split('/\n|\r\n/', $value) as $row) {
Chris@0 107 $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
Chris@0 108 }
Chris@0 109
Chris@0 110 continue;
Chris@0 111 }
Chris@0 112
Chris@12 113 $dumpObjectAsInlineMap = true;
Chris@12 114
Chris@12 115 if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
Chris@12 116 $dumpObjectAsInlineMap = empty((array) $value);
Chris@12 117 }
Chris@12 118
Chris@17 119 $willBeInlined = $inline - 1 <= 0 || !\is_array($value) && $dumpObjectAsInlineMap || empty($value);
Chris@0 120
Chris@0 121 $output .= sprintf('%s%s%s%s',
Chris@0 122 $prefix,
Chris@12 123 $dumpAsMap ? Inline::dump($key, $flags).':' : '-',
Chris@0 124 $willBeInlined ? ' ' : "\n",
Chris@0 125 $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
Chris@0 126 ).($willBeInlined ? "\n" : '');
Chris@0 127 }
Chris@0 128 }
Chris@0 129
Chris@0 130 return $output;
Chris@0 131 }
Chris@0 132 }