Dumper.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace Symfony\Component\Yaml;
  3. /*
  4. * This file is part of the Symfony package.
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. /**
  11. * Dumper dumps PHP variables to YAML strings.
  12. *
  13. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  14. */
  15. class Dumper
  16. {
  17. /**
  18. * Dumps a PHP value to YAML.
  19. *
  20. * @param mixed $input The PHP value
  21. * @param integer $inline The level where you switch to inline YAML
  22. * @param integer $indent The level o indentation indentation (used internally)
  23. *
  24. * @return string The YAML representation of the PHP value
  25. */
  26. public function dump($input, $inline = 0, $indent = 0)
  27. {
  28. $output = '';
  29. $prefix = $indent ? str_repeat(' ', $indent) : '';
  30. if ($inline <= 0 || !is_array($input) || empty($input)) {
  31. $output .= $prefix.Inline::dump($input);
  32. } else {
  33. $isAHash = array_keys($input) !== range(0, count($input) - 1);
  34. foreach ($input as $key => $value) {
  35. $willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);
  36. $output .= sprintf('%s%s%s%s',
  37. $prefix,
  38. $isAHash ? Inline::dump($key).':' : '-',
  39. $willBeInlined ? ' ' : "\n",
  40. $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + 2)
  41. ).($willBeInlined ? "\n" : '');
  42. }
  43. }
  44. return $output;
  45. }
  46. }