sfYamlDumper.class.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. require_once(dirname(__FILE__).'/sfYamlInline.class.php');
  10. /**
  11. * sfYamlDumper class.
  12. *
  13. * @package symfony
  14. * @subpackage util
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @version SVN: $Id: sfYamlDumper.class.php 10574 2008-08-01 13:08:08Z nicolas $
  17. */
  18. class sfYamlDumper
  19. {
  20. /**
  21. * Dumps a PHP value to YAML.
  22. *
  23. * @param mixed The PHP value
  24. * @param integer The level where you switch to inline YAML
  25. * @param integer The level o indentation indentation (used internally)
  26. *
  27. * @return string The YAML representation of the PHP value
  28. */
  29. public function dump($input, $inline = 0, $indent = 0)
  30. {
  31. $output = '';
  32. $prefix = $indent ? str_repeat(' ', $indent) : '';
  33. if ($inline <= 0 || !is_array($input) || empty($input))
  34. {
  35. $output .= $prefix.sfYamlInline::dump($input);
  36. }
  37. else
  38. {
  39. $isAHash = array_keys($input) !== range(0, count($input) - 1);
  40. foreach ($input as $key => $value)
  41. {
  42. $willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);
  43. $output .= sprintf('%s%s%s%s',
  44. $prefix,
  45. $isAHash ? sfYamlInline::dump($key).':' : '-',
  46. $willBeInlined ? ' ' : "\n",
  47. $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + 2)
  48. ).($willBeInlined ? "\n" : '');
  49. }
  50. }
  51. return $output;
  52. }
  53. }