[ Index ]

PHP Cross Reference of phpBB-3.3.9-deutsch

title

Body

[close]

/vendor/symfony/yaml/ -> Dumper.php (source)

   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 since version 3.4
  22   */
  23  class Dumper
  24  {
  25      /**
  26       * The amount of spaces to use for indentation of nested nodes.
  27       *
  28       * @var int
  29       */
  30      protected $indentation;
  31  
  32      /**
  33       * @param int $indentation
  34       */
  35      public function __construct($indentation = 4)
  36      {
  37          if ($indentation < 1) {
  38              throw new \InvalidArgumentException('The indentation must be greater than zero.');
  39          }
  40  
  41          $this->indentation = $indentation;
  42      }
  43  
  44      /**
  45       * Sets the indentation.
  46       *
  47       * @param int $num The amount of spaces to use for indentation of nested nodes
  48       *
  49       * @deprecated since version 3.1, to be removed in 4.0. Pass the indentation to the constructor instead.
  50       */
  51      public function setIndentation($num)
  52      {
  53          @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);
  54  
  55          $this->indentation = (int) $num;
  56      }
  57  
  58      /**
  59       * Dumps a PHP value to YAML.
  60       *
  61       * @param mixed $input  The PHP value
  62       * @param int   $inline The level where you switch to inline YAML
  63       * @param int   $indent The level of indentation (used internally)
  64       * @param int   $flags  A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
  65       *
  66       * @return string The YAML representation of the PHP value
  67       */
  68      public function dump($input, $inline = 0, $indent = 0, $flags = 0)
  69      {
  70          if (\is_bool($flags)) {
  71              @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);
  72  
  73              if ($flags) {
  74                  $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
  75              } else {
  76                  $flags = 0;
  77              }
  78          }
  79  
  80          if (\func_num_args() >= 5) {
  81              @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);
  82  
  83              if (func_get_arg(4)) {
  84                  $flags |= Yaml::DUMP_OBJECT;
  85              }
  86          }
  87  
  88          $output = '';
  89          $prefix = $indent ? str_repeat(' ', $indent) : '';
  90          $dumpObjectAsInlineMap = true;
  91  
  92          if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
  93              $dumpObjectAsInlineMap = empty((array) $input);
  94          }
  95  
  96          if ($inline <= 0 || (!\is_array($input) && !$input instanceof TaggedValue && $dumpObjectAsInlineMap) || empty($input)) {
  97              $output .= $prefix.Inline::dump($input, $flags);
  98          } else {
  99              $dumpAsMap = Inline::isHash($input);
 100  
 101              foreach ($input as $key => $value) {
 102                  if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r")) {
 103                      // If the first line starts with a space character, the spec requires a blockIndicationIndicator
 104                      // http://www.yaml.org/spec/1.2/spec.html#id2793979
 105                      $blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';
 106                      $output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);
 107  
 108                      foreach (explode("\n", $value) as $row) {
 109                          $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
 110                      }
 111  
 112                      continue;
 113                  }
 114  
 115                  if ($value instanceof TaggedValue) {
 116                      $output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());
 117  
 118                      if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && false !== strpos($value->getValue(), "\n") && false === strpos($value->getValue(), "\r\n")) {
 119                          // If the first line starts with a space character, the spec requires a blockIndicationIndicator
 120                          // http://www.yaml.org/spec/1.2/spec.html#id2793979
 121                          $blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
 122                          $output .= sprintf(" |%s\n", $blockIndentationIndicator);
 123  
 124                          foreach (explode("\n", $value->getValue()) as $row) {
 125                              $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
 126                          }
 127  
 128                          continue;
 129                      }
 130  
 131                      if ($inline - 1 <= 0 || null === $value->getValue() || is_scalar($value->getValue())) {
 132                          $output .= ' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
 133                      } else {
 134                          $output .= "\n";
 135                          $output .= $this->dump($value->getValue(), $inline - 1, $dumpAsMap ? $indent + $this->indentation : $indent + 2, $flags);
 136                      }
 137  
 138                      continue;
 139                  }
 140  
 141                  $dumpObjectAsInlineMap = true;
 142  
 143                  if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
 144                      $dumpObjectAsInlineMap = empty((array) $value);
 145                  }
 146  
 147                  $willBeInlined = $inline - 1 <= 0 || !\is_array($value) && $dumpObjectAsInlineMap || empty($value);
 148  
 149                  $output .= sprintf('%s%s%s%s',
 150                      $prefix,
 151                      $dumpAsMap ? Inline::dump($key, $flags).':' : '-',
 152                      $willBeInlined ? ' ' : "\n",
 153                      $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
 154                  ).($willBeInlined ? "\n" : '');
 155              }
 156          }
 157  
 158          return $output;
 159      }
 160  }


Generated: Wed Dec 7 15:09:22 2022 Cross-referenced by PHPXref 0.7.1