Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Yaml; Chris@0: Chris@0: /** Chris@0: * Dumper dumps PHP variables to YAML strings. Chris@0: * Chris@0: * @author Fabien Potencier Chris@14: * Chris@14: * @final since version 3.4 Chris@0: */ Chris@0: class Dumper Chris@0: { Chris@0: /** Chris@0: * The amount of spaces to use for indentation of nested nodes. Chris@0: * Chris@0: * @var int Chris@0: */ Chris@0: protected $indentation; Chris@0: Chris@0: /** Chris@0: * @param int $indentation Chris@0: */ Chris@0: public function __construct($indentation = 4) Chris@0: { Chris@0: if ($indentation < 1) { Chris@0: throw new \InvalidArgumentException('The indentation must be greater than zero.'); Chris@0: } Chris@0: Chris@0: $this->indentation = $indentation; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the indentation. Chris@0: * Chris@0: * @param int $num The amount of spaces to use for indentation of nested nodes Chris@12: * Chris@12: * @deprecated since version 3.1, to be removed in 4.0. Pass the indentation to the constructor instead. Chris@0: */ Chris@0: public function setIndentation($num) Chris@0: { Chris@14: @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 3.1 and will be removed in 4.0. Pass the indentation to the constructor instead.', E_USER_DEPRECATED); Chris@0: Chris@0: $this->indentation = (int) $num; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Dumps a PHP value to YAML. Chris@0: * Chris@0: * @param mixed $input The PHP value Chris@0: * @param int $inline The level where you switch to inline YAML Chris@0: * @param int $indent The level of indentation (used internally) Chris@0: * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string Chris@0: * Chris@0: * @return string The YAML representation of the PHP value Chris@0: */ Chris@0: public function dump($input, $inline = 0, $indent = 0, $flags = 0) Chris@0: { Chris@17: if (\is_bool($flags)) { Chris@14: @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED); Chris@0: Chris@0: if ($flags) { Chris@0: $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE; Chris@0: } else { Chris@0: $flags = 0; Chris@0: } Chris@0: } Chris@0: Chris@17: if (\func_num_args() >= 5) { Chris@14: @trigger_error('Passing a boolean flag to toggle object support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED); Chris@0: Chris@0: if (func_get_arg(4)) { Chris@0: $flags |= Yaml::DUMP_OBJECT; Chris@0: } Chris@0: } Chris@0: Chris@0: $output = ''; Chris@0: $prefix = $indent ? str_repeat(' ', $indent) : ''; Chris@12: $dumpObjectAsInlineMap = true; Chris@0: Chris@12: if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) { Chris@12: $dumpObjectAsInlineMap = empty((array) $input); Chris@12: } Chris@12: Chris@17: if ($inline <= 0 || (!\is_array($input) && $dumpObjectAsInlineMap) || empty($input)) { Chris@0: $output .= $prefix.Inline::dump($input, $flags); Chris@0: } else { Chris@12: $dumpAsMap = Inline::isHash($input); Chris@0: Chris@0: foreach ($input as $key => $value) { Chris@17: if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r\n")) { Chris@14: // If the first line starts with a space character, the spec requires a blockIndicationIndicator Chris@14: // http://www.yaml.org/spec/1.2/spec.html#id2793979 Chris@14: $blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : ''; Chris@14: $output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator); Chris@0: Chris@0: foreach (preg_split('/\n|\r\n/', $value) as $row) { Chris@0: $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row); Chris@0: } Chris@0: Chris@0: continue; Chris@0: } Chris@0: Chris@12: $dumpObjectAsInlineMap = true; Chris@12: Chris@12: if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) { Chris@12: $dumpObjectAsInlineMap = empty((array) $value); Chris@12: } Chris@12: Chris@17: $willBeInlined = $inline - 1 <= 0 || !\is_array($value) && $dumpObjectAsInlineMap || empty($value); Chris@0: Chris@0: $output .= sprintf('%s%s%s%s', Chris@0: $prefix, Chris@12: $dumpAsMap ? Inline::dump($key, $flags).':' : '-', Chris@0: $willBeInlined ? ' ' : "\n", Chris@0: $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags) Chris@0: ).($willBeInlined ? "\n" : ''); Chris@0: } Chris@0: } Chris@0: Chris@0: return $output; Chris@0: } Chris@0: }