You are here

public static function Inline::dump in Translation template extractor 6.3

Same name and namespace in other branches
  1. 7.3 vendor/Symfony/Component/Yaml/Inline.php \Symfony\Component\Yaml\Inline::dump()
  2. 7.2 vendor/Symfony/Component/Yaml/Inline.php \Symfony\Component\Yaml\Inline::dump()

Dumps a given PHP variable to a YAML string.

Parameters

mixed $value The PHP variable to convert:

Boolean $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise:

Boolean $objectSupport true if object support is enabled, false otherwise:

Return value

string The YAML string representing the PHP array

Throws

DumpException When trying to dump PHP resource

5 calls to Inline::dump()
Dumper::dump in vendor/Symfony/Component/Yaml/Dumper.php
Dumps a PHP value to YAML.
Inline::dumpArray in vendor/Symfony/Component/Yaml/Inline.php
Dumps a PHP array to a YAML string.
InlineTest::testDump in vendor/Symfony/Component/Yaml/Tests/InlineTest.php
InlineTest::testDumpNumericValueWithLocale in vendor/Symfony/Component/Yaml/Tests/InlineTest.php
InlineTest::testHashStringsResemblingExponentialNumericsShouldNotBeChangedToINF in vendor/Symfony/Component/Yaml/Tests/InlineTest.php

File

vendor/Symfony/Component/Yaml/Inline.php, line 92

Class

Inline
Inline implements a YAML parser/dumper for the YAML inline syntax.

Namespace

Symfony\Component\Yaml

Code

public static function dump($value, $exceptionOnInvalidType = false, $objectSupport = false) {
  switch (true) {
    case is_resource($value):
      if ($exceptionOnInvalidType) {
        throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
      }
      return 'null';
    case is_object($value):
      if ($objectSupport) {
        return '!!php/object:' . serialize($value);
      }
      if ($exceptionOnInvalidType) {
        throw new DumpException('Object support when dumping a YAML file has been disabled.');
      }
      return 'null';
    case is_array($value):
      return self::dumpArray($value, $exceptionOnInvalidType, $objectSupport);
    case null === $value:
      return 'null';
    case true === $value:
      return 'true';
    case false === $value:
      return 'false';
    case ctype_digit($value):
      return is_string($value) ? "'{$value}'" : (int) $value;
    case is_numeric($value):
      $locale = setlocale(LC_NUMERIC, 0);
      if (false !== $locale) {
        setlocale(LC_NUMERIC, 'C');
      }
      $repr = is_string($value) ? "'{$value}'" : (is_infinite($value) ? str_ireplace('INF', '.Inf', strval($value)) : strval($value));
      if (false !== $locale) {
        setlocale(LC_NUMERIC, $locale);
      }
      return $repr;
    case Escaper::requiresDoubleQuoting($value):
      return Escaper::escapeWithDoubleQuotes($value);
    case Escaper::requiresSingleQuoting($value):
      return Escaper::escapeWithSingleQuotes($value);
    case '' == $value:
      return "''";
    case preg_match(self::getTimestampRegex(), $value):
    case in_array(strtolower($value), array(
      'null',
      '~',
      'true',
      'false',
    )):
      return "'{$value}'";
    default:
      return $value;
  }
}