Dumper.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. class Dumper
  17. {
  18. /**
  19. * The amount of spaces to use for indentation of nested nodes.
  20. *
  21. * @var int
  22. */
  23. protected $indentation;
  24. /**
  25. * @param int $indentation
  26. */
  27. public function __construct($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. * Sets the indentation.
  36. *
  37. * @param int $num The amount of spaces to use for indentation of nested nodes
  38. *
  39. * @deprecated since version 3.1, to be removed in 4.0. Pass the indentation to the constructor instead.
  40. */
  41. public function setIndentation($num)
  42. {
  43. @trigger_error('The '.__METHOD__.' method is deprecated since version 3.1 and will be removed in 4.0. Pass the indentation to the constructor instead.', E_USER_DEPRECATED);
  44. $this->indentation = (int) $num;
  45. }
  46. /**
  47. * Dumps a PHP value to YAML.
  48. *
  49. * @param mixed $input The PHP value
  50. * @param int $inline The level where you switch to inline YAML
  51. * @param int $indent The level of indentation (used internally)
  52. * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
  53. *
  54. * @return string The YAML representation of the PHP value
  55. */
  56. public function dump($input, $inline = 0, $indent = 0, $flags = 0)
  57. {
  58. if (is_bool($flags)) {
  59. @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
  60. if ($flags) {
  61. $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
  62. } else {
  63. $flags = 0;
  64. }
  65. }
  66. if (func_num_args() >= 5) {
  67. @trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
  68. if (func_get_arg(4)) {
  69. $flags |= Yaml::DUMP_OBJECT;
  70. }
  71. }
  72. $output = '';
  73. $prefix = $indent ? str_repeat(' ', $indent) : '';
  74. $dumpObjectAsInlineMap = true;
  75. if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
  76. $dumpObjectAsInlineMap = empty((array) $input);
  77. }
  78. if ($inline <= 0 || (!is_array($input) && $dumpObjectAsInlineMap) || empty($input)) {
  79. $output .= $prefix.Inline::dump($input, $flags);
  80. } else {
  81. $dumpAsMap = Inline::isHash($input);
  82. foreach ($input as $key => $value) {
  83. if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && is_string($value) && false !== strpos($value, "\n")) {
  84. $output .= sprintf("%s%s%s |\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '');
  85. foreach (preg_split('/\n|\r\n/', $value) as $row) {
  86. $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
  87. }
  88. continue;
  89. }
  90. $dumpObjectAsInlineMap = true;
  91. if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
  92. $dumpObjectAsInlineMap = empty((array) $value);
  93. }
  94. $willBeInlined = $inline - 1 <= 0 || !is_array($value) && $dumpObjectAsInlineMap || empty($value);
  95. $output .= sprintf('%s%s%s%s',
  96. $prefix,
  97. $dumpAsMap ? Inline::dump($key, $flags).':' : '-',
  98. $willBeInlined ? ' ' : "\n",
  99. $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
  100. ).($willBeInlined ? "\n" : '');
  101. }
  102. }
  103. return $output;
  104. }
  105. }