1<?php
2
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
11require_once(dirname(__FILE__).'/sfYamlInline.php');
12
13/**
14 * sfYamlDumper dumps PHP variables to YAML strings.
15 *
16 * @package    symfony
17 * @subpackage yaml
18 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19 * @version    SVN: $Id: sfYamlDumper.class.php 10575 2008-08-01 13:08:42Z nicolas $
20 */
21class sfYamlDumper
22{
23  /**
24   * Dumps a PHP value to YAML.
25   *
26   * @param  mixed   $input  The PHP value
27   * @param  integer $inline The level where you switch to inline YAML
28   * @param  integer $indent The level o indentation indentation (used internally)
29   *
30   * @return string  The YAML representation of the PHP value
31   */
32  public function dump($input, $inline = 0, $indent = 0)
33  {
34    $output = '';
35    $prefix = $indent ? str_repeat(' ', $indent) : '';
36
37    if ($inline <= 0 || !is_array($input) || empty($input))
38    {
39      $output .= $prefix.sfYamlInline::dump($input);
40    }
41    else
42    {
43      $isAHash = array_keys($input) !== range(0, count($input) - 1);
44
45      foreach ($input as $key => $value)
46      {
47        $willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);
48
49        $output .= sprintf('%s%s%s%s',
50          $prefix,
51          $isAHash ? sfYamlInline::dump($key).':' : '-',
52          $willBeInlined ? ' ' : "\n",
53          $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + 2)
54        ).($willBeInlined ? "\n" : '');
55      }
56    }
57
58    return $output;
59  }
60}
61