Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Inline.php

function Inline::dump

Dumps a given PHP variable to a YAML string.

Parameters

mixed $value The PHP variable to convert:

int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string:

Throws

DumpException When trying to dump PHP resource

3 calls to Inline::dump()
Dumper::dump in vendor/symfony/yaml/Dumper.php
Dumps a PHP value to YAML.
Inline::dumpArray in vendor/symfony/yaml/Inline.php
Dumps a PHP array to a YAML string.
Inline::dumpHashArray in vendor/symfony/yaml/Inline.php
Dumps hash array to a YAML string.

File

vendor/symfony/yaml/Inline.php, line 103

Class

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

Namespace

Symfony\Component\Yaml

Code

public static function dump(mixed $value, int $flags = 0) : string {
    switch (true) {
        case \is_resource($value):
            if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
                throw new DumpException(\sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
            }
            return self::dumpNull($flags);
        case $value instanceof \DateTimeInterface:
            return $value->format(match (true) {    !($length = \strlen(rtrim($value->format('u'), '0'))) => 'c',
                $length < 4 => 'Y-m-d\\TH:i:s.vP',
                default => 'Y-m-d\\TH:i:s.uP',
            
            });
        case $value instanceof \UnitEnum:
            return \sprintf('!php/enum %s::%s', $value::class, $value->name);
        case \is_object($value):
            if ($value instanceof TaggedValue) {
                return '!' . $value->getTag() . ' ' . self::dump($value->getValue(), $flags);
            }
            if (Yaml::DUMP_OBJECT & $flags) {
                return '!php/object ' . self::dump(serialize($value));
            }
            if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
                return self::dumpHashArray($value, $flags);
            }
            if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
                throw new DumpException('Object support when dumping a YAML file has been disabled.');
            }
            return self::dumpNull($flags);
        case \is_array($value):
            return self::dumpArray($value, $flags);
        case null === $value:
            return self::dumpNull($flags);
        case true === $value:
            return 'true';
        case false === $value:
            return 'false';
        case \is_int($value):
            return $value;
        case is_numeric($value) && false === strpbrk($value, "\f\n\r\t\v"):
            $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.
                    if (!str_contains($repr, 'E')) {
                        $repr .= '.0';
                    }
                }
            }
            else {
                $repr = \is_string($value) ? "'{$value}'" : (string) $value;
            }
            if (false !== $locale) {
                setlocale(\LC_NUMERIC, $locale);
            }
            return $repr;
        case '' == $value:
            return "''";
        case self::isBinaryString($value):
            return '!!binary ' . base64_encode($value);
        case Escaper::requiresDoubleQuoting($value):
            return Escaper::escapeWithDoubleQuotes($value);
        case Escaper::requiresSingleQuoting($value):
            $singleQuoted = Escaper::escapeWithSingleQuotes($value);
            if (!str_contains($value, "'")) {
                return $singleQuoted;
            }
            // Attempt double-quoting the string instead to see if it's more efficient.
            $doubleQuoted = Escaper::escapeWithDoubleQuotes($value);
            return \strlen($doubleQuoted) < \strlen($singleQuoted) ? $doubleQuoted : $singleQuoted;
        case Parser::preg_match('{^[0-9]+[_0-9]*$}', $value):
        case Parser::preg_match(self::getHexRegex(), $value):
        case Parser::preg_match(self::getTimestampRegex(), $value):
            return Escaper::escapeWithSingleQuotes($value);
        default:
            return $value;
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal