|
Revision 10574, 1.5 kB
(checked in by nicolas, 5 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 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
require_once(dirname(__FILE__).'/sfYamlInline.class.php'); |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 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 |
|
|---|