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>
|
2017-08-29 21:51:11 +01:00
|
|
|
*
|
|
|
|
* @final since version 3.4
|
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.
|
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @var int
|
2012-08-04 23:46:53 +01:00
|
|
|
*/
|
2016-02-09 22:58:46 +00:00
|
|
|
protected $indentation;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $indentation
|
|
|
|
*/
|
|
|
|
public function __construct($indentation = 4)
|
|
|
|
{
|
2016-03-02 10:23:16 +00:00
|
|
|
if ($indentation < 1) {
|
|
|
|
throw new \InvalidArgumentException('The indentation must be greater than zero.');
|
|
|
|
}
|
|
|
|
|
2016-02-09 22:58:46 +00:00
|
|
|
$this->indentation = $indentation;
|
|
|
|
}
|
2012-08-04 23:46:53 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Dumps a PHP value to YAML.
|
|
|
|
*
|
2016-02-09 19:56:50 +00:00
|
|
|
* @param mixed $input The PHP value
|
|
|
|
* @param int $inline The level where you switch to inline YAML
|
|
|
|
* @param int $indent The level of indentation (used internally)
|
|
|
|
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2014-11-30 13:33:44 +00:00
|
|
|
* @return string The YAML representation of the PHP value
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2016-02-09 19:56:50 +00:00
|
|
|
public function dump($input, $inline = 0, $indent = 0, $flags = 0)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$output = '';
|
|
|
|
$prefix = $indent ? str_repeat(' ', $indent) : '';
|
2017-04-12 20:41:43 +01:00
|
|
|
$dumpObjectAsInlineMap = true;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2017-04-12 20:41:43 +01:00
|
|
|
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
|
|
|
|
$dumpObjectAsInlineMap = empty((array) $input);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($inline <= 0 || (!is_array($input) && $dumpObjectAsInlineMap) || empty($input)) {
|
2016-02-09 19:56:50 +00:00
|
|
|
$output .= $prefix.Inline::dump($input, $flags);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2017-04-12 20:41:43 +01:00
|
|
|
$dumpAsMap = Inline::isHash($input);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($input as $key => $value) {
|
2016-12-03 09:52:31 +00:00
|
|
|
if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && is_string($value) && false !== strpos($value, "\n")) {
|
2017-04-12 20:41:43 +01:00
|
|
|
$output .= sprintf("%s%s%s |\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '');
|
2016-02-27 09:37:11 +00:00
|
|
|
|
|
|
|
foreach (preg_split('/\n|\r\n/', $value) as $row) {
|
|
|
|
$output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-04-12 20:41:43 +01:00
|
|
|
$dumpObjectAsInlineMap = true;
|
|
|
|
|
|
|
|
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
|
|
|
|
$dumpObjectAsInlineMap = empty((array) $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$willBeInlined = $inline - 1 <= 0 || !is_array($value) && $dumpObjectAsInlineMap || empty($value);
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
$output .= sprintf('%s%s%s%s',
|
|
|
|
$prefix,
|
2017-04-12 20:41:43 +01:00
|
|
|
$dumpAsMap ? Inline::dump($key, $flags).':' : '-',
|
2010-05-06 12:25:53 +01:00
|
|
|
$willBeInlined ? ' ' : "\n",
|
2016-02-09 19:56:50 +00:00
|
|
|
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
|
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
|
|
|
}
|