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

Breadcrumb

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

class YamlEncoder

Encodes YAML data.

@author Kévin Dunglas <dunglas@gmail.com>

Hierarchy

  • class \Symfony\Component\Serializer\Encoder\YamlEncoder implements \Symfony\Component\Serializer\Encoder\EncoderInterface, \Symfony\Component\Serializer\Encoder\DecoderInterface

Expanded class hierarchy of YamlEncoder

1 file declares its use of YamlEncoder
YamlEncoderContextBuilder.php in vendor/symfony/serializer/Context/Encoder/YamlEncoderContextBuilder.php

File

vendor/symfony/serializer/Encoder/YamlEncoder.php, line 24

Namespace

Symfony\Component\Serializer\Encoder
View source
class YamlEncoder implements EncoderInterface, DecoderInterface {
    public const FORMAT = 'yaml';
    private const ALTERNATIVE_FORMAT = 'yml';
    public const PRESERVE_EMPTY_OBJECTS = 'preserve_empty_objects';
    
    /**
     * Override the amount of spaces to use for indentation of nested nodes.
     *
     * This option only works in the constructor, not in calls to `encode`.
     */
    public const YAML_INDENTATION = 'yaml_indentation';
    public const YAML_INLINE = 'yaml_inline';
    
    /**
     * Initial indentation for root element.
     */
    public const YAML_INDENT = 'yaml_indent';
    public const YAML_FLAGS = 'yaml_flags';
    private readonly Dumper $dumper;
    private readonly Parser $parser;
    private array $defaultContext = [
        self::YAML_INLINE => 0,
        self::YAML_INDENT => 0,
        self::YAML_FLAGS => 0,
    ];
    public function __construct(?Dumper $dumper = null, ?Parser $parser = null, array $defaultContext = []) {
        if (!class_exists(Dumper::class)) {
            throw new RuntimeException('The YamlEncoder class requires the "Yaml" component. Try running "composer require symfony/yaml".');
        }
        if (!$dumper) {
            $dumper = \array_key_exists(self::YAML_INDENTATION, $defaultContext) ? new Dumper($defaultContext[self::YAML_INDENTATION]) : new Dumper();
        }
        $this->dumper = $dumper;
        $this->parser = $parser ?? new Parser();
        unset($defaultContext[self::YAML_INDENTATION]);
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
    }
    public function encode(mixed $data, string $format, array $context = []) : string {
        $context = array_merge($this->defaultContext, $context);
        if ($context[self::PRESERVE_EMPTY_OBJECTS] ?? false) {
            $context[self::YAML_FLAGS] |= Yaml::DUMP_OBJECT_AS_MAP;
        }
        return $this->dumper
            ->dump($data, $context[self::YAML_INLINE], $context[self::YAML_INDENT], $context[self::YAML_FLAGS]);
    }
    public function supportsEncoding(string $format) : bool {
        return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
    }
    public function decode(string $data, string $format, array $context = []) : mixed {
        $context = array_merge($this->defaultContext, $context);
        return $this->parser
            ->parse($data, $context[self::YAML_FLAGS]);
    }
    public function supportsDecoding(string $format) : bool {
        return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
YamlEncoder::$defaultContext private property
YamlEncoder::$dumper private property
YamlEncoder::$parser private property
YamlEncoder::ALTERNATIVE_FORMAT private constant
YamlEncoder::decode public function Decodes a string into PHP data. Overrides DecoderInterface::decode
YamlEncoder::encode public function Encodes data into the given format. Overrides EncoderInterface::encode
YamlEncoder::FORMAT public constant
YamlEncoder::PRESERVE_EMPTY_OBJECTS public constant
YamlEncoder::supportsDecoding public function Checks whether the deserializer can decode from given format. Overrides DecoderInterface::supportsDecoding
YamlEncoder::supportsEncoding public function Checks whether the serializer can encode to given format. Overrides EncoderInterface::supportsEncoding
YamlEncoder::YAML_FLAGS public constant
YamlEncoder::YAML_INDENT public constant Initial indentation for root element.
YamlEncoder::YAML_INDENTATION public constant Override the amount of spaces to use for indentation of nested nodes.
YamlEncoder::YAML_INLINE public constant
YamlEncoder::__construct public function
RSS feed
Powered by Drupal