Development

/branches/1.1/lib/yaml/sfYamlDumper.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/yaml/sfYamlDumper.class.php

Revision 10574, 1.5 kB (checked in by nicolas, 6 years ago)

[1.1] fixed sfYaml::dump misunderstands hash as array (closes #4025)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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
11 require_once(dirname(__FILE__).'/sfYamlInline.class.php');
12
13 /**
14  * sfYamlDumper class.
15  *
16  * @package    symfony
17  * @subpackage util
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @version    SVN: $Id$
20  */
21 class sfYamlDumper
22 {
23   /**
24    * Dumps a PHP value to YAML.
25    *
26    * @param  mixed   The PHP value
27    * @param  integer The level where you switch to inline YAML
28    * @param  integer 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
Note: See TracBrowser for help on using the browser.