You are here

public static function Inline::dump in Service Container 7

Same name and namespace in other branches
  1. 7.2 modules/providers/service_container_symfony/lib/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:

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

bool $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 modules/providers/service_container_symfony/lib/Symfony/Component/Yaml/Dumper.php
Dumps a PHP value to YAML.
Inline::dumpArray in modules/providers/service_container_symfony/lib/Symfony/Component/Yaml/Inline.php
Dumps a PHP array to a YAML string.
InlineTest::testDump in modules/providers/service_container_symfony/lib/Symfony/Component/Yaml/Tests/InlineTest.php
@dataProvider getTestsForDump
InlineTest::testDumpNumericValueWithLocale in modules/providers/service_container_symfony/lib/Symfony/Component/Yaml/Tests/InlineTest.php
InlineTest::testHashStringsResemblingExponentialNumericsShouldNotBeChangedToINF in modules/providers/service_container_symfony/lib/Symfony/Component/Yaml/Tests/InlineTest.php

File

modules/providers/service_container_symfony/lib/Symfony/Component/Yaml/Inline.php, line 97

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');
      }
      if (is_float($value)) {
        $repr = (string) $value;
        if (is_infinite($value)) {
          $repr = str_ireplace('INF', '.Inf', $repr);
        }
        elseif (floor($value) == $value && $repr == $value) {

          // Preserve float data type since storing a whole number will result in integer value.
          $repr = '!!float ' . $repr;
        }
      }
      else {
        $repr = is_string($value) ? "'{$value}'" : (string) $value;
      }
      if (false !== $locale) {
        setlocale(LC_NUMERIC, $locale);
      }
      return $repr;
    case '' == $value:
      return "''";
    case Escaper::requiresDoubleQuoting($value):
      return Escaper::escapeWithDoubleQuotes($value);
    case Escaper::requiresSingleQuoting($value):
    case preg_match(self::getHexRegex(), $value):
    case preg_match(self::getTimestampRegex(), $value):
      return Escaper::escapeWithSingleQuotes($value);
    default:
      return $value;
  }
}