Geen omschrijving

Dumper.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. public function setIndentation($num)
  40. {
  41. @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);
  42. $this->indentation = (int) $num;
  43. }
  44. /**
  45. * Dumps a PHP value to YAML.
  46. *
  47. * @param mixed $input The PHP value
  48. * @param int $inline The level where you switch to inline YAML
  49. * @param int $indent The level of indentation (used internally)
  50. * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
  51. *
  52. * @return string The YAML representation of the PHP value
  53. */
  54. public function dump($input, $inline = 0, $indent = 0, $flags = 0)
  55. {
  56. if (is_bool($flags)) {
  57. @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);
  58. if ($flags) {
  59. $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
  60. } else {
  61. $flags = 0;
  62. }
  63. }
  64. if (func_num_args() >= 5) {
  65. @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);
  66. if (func_get_arg(4)) {
  67. $flags |= Yaml::DUMP_OBJECT;
  68. }
  69. }
  70. $output = '';
  71. $prefix = $indent ? str_repeat(' ', $indent) : '';
  72. if ($inline <= 0 || !is_array($input) || empty($input)) {
  73. $output .= $prefix.Inline::dump($input, $flags);
  74. } else {
  75. $isAHash = Inline::isHash($input);
  76. foreach ($input as $key => $value) {
  77. if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && is_string($value) && false !== strpos($value, "\n")) {
  78. $output .= sprintf("%s%s%s |\n", $prefix, $isAHash ? Inline::dump($key, $flags).':' : '-', '');
  79. foreach (preg_split('/\n|\r\n/', $value) as $row) {
  80. $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
  81. }
  82. continue;
  83. }
  84. $willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);
  85. $output .= sprintf('%s%s%s%s',
  86. $prefix,
  87. $isAHash ? Inline::dump($key, $flags).':' : '-',
  88. $willBeInlined ? ' ' : "\n",
  89. $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
  90. ).($willBeInlined ? "\n" : '');
  91. }
  92. }
  93. return $output;
  94. }
  95. }