PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Yaml/Dumper.php

http://github.com/symfony/symfony
PHP | 125 lines | 68 code | 23 blank | 34 comment | 31 complexity | bf96bb555ec4cc86247156d4cba2b1e9 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Yaml;
  11. use Symfony\Component\Yaml\Tag\TaggedValue;
  12. /**
  13. * Dumper dumps PHP variables to YAML strings.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @final
  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. public function __construct(int $indentation = 4)
  28. {
  29. if ($indentation < 1) {
  30. throw new \InvalidArgumentException('The indentation must be greater than zero.');
  31. }
  32. $this->indentation = $indentation;
  33. }
  34. /**
  35. * Dumps a PHP value to YAML.
  36. *
  37. * @param mixed $input The PHP value
  38. * @param int $inline The level where you switch to inline YAML
  39. * @param int $indent The level of indentation (used internally)
  40. * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
  41. *
  42. * @return string The YAML representation of the PHP value
  43. */
  44. public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0): string
  45. {
  46. $output = '';
  47. $prefix = $indent ? str_repeat(' ', $indent) : '';
  48. $dumpObjectAsInlineMap = true;
  49. if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
  50. $dumpObjectAsInlineMap = empty((array) $input);
  51. }
  52. if ($inline <= 0 || (!\is_array($input) && !$input instanceof TaggedValue && $dumpObjectAsInlineMap) || empty($input)) {
  53. $output .= $prefix.Inline::dump($input, $flags);
  54. } else {
  55. $dumpAsMap = Inline::isHash($input);
  56. foreach ($input as $key => $value) {
  57. if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r")) {
  58. // If the first line starts with a space character, the spec requires a blockIndicationIndicator
  59. // http://www.yaml.org/spec/1.2/spec.html#id2793979
  60. $blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';
  61. $output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);
  62. foreach (explode("\n", $value) as $row) {
  63. $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
  64. }
  65. continue;
  66. }
  67. if ($value instanceof TaggedValue) {
  68. $output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());
  69. 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")) {
  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 = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
  73. $output .= sprintf(" |%s\n", $blockIndentationIndicator);
  74. foreach (explode("\n", $value->getValue()) as $row) {
  75. $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
  76. }
  77. continue;
  78. }
  79. if ($inline - 1 <= 0 || null === $value->getValue() || is_scalar($value->getValue())) {
  80. $output .= ' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
  81. } else {
  82. $output .= "\n";
  83. $output .= $this->dump($value->getValue(), $inline - 1, $dumpAsMap ? $indent + $this->indentation : $indent + 2, $flags);
  84. }
  85. continue;
  86. }
  87. $dumpObjectAsInlineMap = true;
  88. if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
  89. $dumpObjectAsInlineMap = empty((array) $value);
  90. }
  91. $willBeInlined = $inline - 1 <= 0 || !\is_array($value) && $dumpObjectAsInlineMap || empty($value);
  92. $output .= sprintf('%s%s%s%s',
  93. $prefix,
  94. $dumpAsMap ? Inline::dump($key, $flags).':' : '-',
  95. $willBeInlined ? ' ' : "\n",
  96. $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
  97. ).($willBeInlined ? "\n" : '');
  98. }
  99. }
  100. return $output;
  101. }
  102. }