2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony package.
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-01-04 14:26:20 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Yaml;
|
|
|
|
|
2010-01-04 14:26:20 +00:00
|
|
|
/**
|
|
|
|
* Dumper dumps PHP variables to YAML strings.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-01-04 14:26:20 +00:00
|
|
|
*/
|
|
|
|
class Dumper
|
|
|
|
{
|
2012-08-04 23:46:53 +01:00
|
|
|
/**
|
|
|
|
* The amount of spaces to use for indentation of nested nodes.
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $indentation = 4;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the indentation.
|
|
|
|
*
|
2012-12-14 22:27:02 +00:00
|
|
|
* @param integer $num The amount of spaces to use for indentation of nested nodes.
|
2012-08-04 23:46:53 +01:00
|
|
|
*/
|
|
|
|
public function setIndentation($num)
|
|
|
|
{
|
2012-08-07 08:40:56 +01:00
|
|
|
$this->indentation = (int) $num;
|
2012-08-04 23:46:53 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Dumps a PHP value to YAML.
|
|
|
|
*
|
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
|
|
|
* @param mixed $input The PHP value
|
|
|
|
* @param integer $inline The level where you switch to inline YAML
|
2013-01-17 21:16:19 +00:00
|
|
|
* @param integer $indent The level of indentation (used internally)
|
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
|
|
|
* @param Boolean $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
|
|
|
|
* @param Boolean $objectSupport true if object support is enabled, false otherwise
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return string The YAML representation of the PHP value
|
|
|
|
*/
|
2013-01-17 21:16:19 +00:00
|
|
|
public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = false, $objectSupport = false)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$output = '';
|
|
|
|
$prefix = $indent ? str_repeat(' ', $indent) : '';
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($inline <= 0 || !is_array($input) || empty($input)) {
|
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
|
|
|
$output .= $prefix.Inline::dump($input, $exceptionOnInvalidType, $objectSupport);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$isAHash = array_keys($input) !== range(0, count($input) - 1);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($input as $key => $value) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);
|
|
|
|
|
|
|
|
$output .= sprintf('%s%s%s%s',
|
|
|
|
$prefix,
|
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
|
|
|
$isAHash ? Inline::dump($key, $exceptionOnInvalidType, $objectSupport).':' : '-',
|
2010-05-06 12:25:53 +01:00
|
|
|
$willBeInlined ? ' ' : "\n",
|
2013-01-17 21:16:19 +00:00
|
|
|
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $exceptionOnInvalidType, $objectSupport)
|
2010-05-06 12:25:53 +01:00
|
|
|
).($willBeInlined ? "\n" : '');
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $output;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|