Dumper.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /**
  12. * Dumper dumps PHP variables to YAML strings.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. *
  16. * @final
  17. */
  18. class Dumper
  19. {
  20. /**
  21. * The amount of spaces to use for indentation of nested nodes.
  22. *
  23. * @var int
  24. */
  25. protected $indentation;
  26. public function __construct(int $indentation = 4)
  27. {
  28. if ($indentation < 1) {
  29. throw new \InvalidArgumentException('The indentation must be greater than zero.');
  30. }
  31. $this->indentation = $indentation;
  32. }
  33. /**
  34. * Dumps a PHP value to YAML.
  35. *
  36. * @param mixed $input The PHP value
  37. * @param int $inline The level where you switch to inline YAML
  38. * @param int $indent The level of indentation (used internally)
  39. * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
  40. *
  41. * @return string The YAML representation of the PHP value
  42. */
  43. public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0): string
  44. {
  45. $output = '';
  46. $prefix = $indent ? str_repeat(' ', $indent) : '';
  47. $dumpObjectAsInlineMap = true;
  48. if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
  49. $dumpObjectAsInlineMap = empty((array) $input);
  50. }
  51. if ($inline <= 0 || (!\is_array($input) && $dumpObjectAsInlineMap) || empty($input)) {
  52. $output .= $prefix.Inline::dump($input, $flags);
  53. } else {
  54. $dumpAsMap = Inline::isHash($input);
  55. foreach ($input as $key => $value) {
  56. if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r\n")) {
  57. // If the first line starts with a space character, the spec requires a blockIndicationIndicator
  58. // http://www.yaml.org/spec/1.2/spec.html#id2793979
  59. $blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';
  60. $output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);
  61. foreach (preg_split('/\n|\r\n/', $value) as $row) {
  62. $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
  63. }
  64. continue;
  65. }
  66. $dumpObjectAsInlineMap = true;
  67. if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
  68. $dumpObjectAsInlineMap = empty((array) $value);
  69. }
  70. $willBeInlined = $inline - 1 <= 0 || !\is_array($value) && $dumpObjectAsInlineMap || empty($value);
  71. $output .= sprintf('%s%s%s%s',
  72. $prefix,
  73. $dumpAsMap ? Inline::dump($key, $flags).':' : '-',
  74. $willBeInlined ? ' ' : "\n",
  75. $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
  76. ).($willBeInlined ? "\n" : '');
  77. }
  78. }
  79. return $output;
  80. }
  81. }