wetddump.deb/usr/share/wetddump/sfYamlDumper.class.php

54 lines
1.6 KiB
PHP
Raw Normal View History

2015-03-02 21:42:41 +01:00
<?php
/*
* This file is part of the symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfYamlDumper dumps PHP variables to YAML strings.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
2017-01-09 15:22:00 +01:00
*
2015-03-02 21:42:41 +01:00
* @version SVN: $Id: sfYamlDumper.class.php 10575 2008-08-01 13:08:42Z nicolas $
*/
class sfYamlDumper
{
2017-01-09 15:22:00 +01:00
/**
2015-03-02 21:42:41 +01:00
* Dumps a PHP value to YAML.
*
* @param mixed $input The PHP value
2017-01-09 15:22:00 +01:00
* @param int $inline The level where you switch to inline YAML
* @param int $indent The level o indentation indentation (used internally)
2015-03-02 21:42:41 +01:00
*
* @return string The YAML representation of the PHP value
*/
public function dump($input, $inline = 0, $indent = 0)
{
2017-01-09 15:22:00 +01:00
$output = '';
$prefix = $indent ? str_repeat(' ', $indent) : '';
2015-03-02 21:42:41 +01:00
2017-01-09 15:22:00 +01:00
if ($inline <= 0 || !is_array($input) || empty($input)) {
$output .= $prefix.sfYamlInline::dump($input);
} else {
$isAHash = array_keys($input) !== range(0, count($input) - 1);
2015-03-02 21:42:41 +01:00
2017-01-09 15:22:00 +01:00
foreach ($input as $key => $value) {
$willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);
2015-03-02 21:42:41 +01:00
2017-01-09 15:22:00 +01:00
$output .= sprintf('%s%s%s%s',
2015-03-02 21:42:41 +01:00
$prefix,
$isAHash ? sfYamlInline::dump($key).':' : '-',
$willBeInlined ? ' ' : "\n",
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + 2)
).($willBeInlined ? "\n" : '');
2017-01-09 15:22:00 +01:00
}
2015-03-02 21:42:41 +01:00
}
2017-01-09 15:22:00 +01:00
return $output;
2015-03-02 21:42:41 +01:00
}
}