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

Breadcrumb

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

function YamlPecl::decode

Overrides SerializationInterface::decode

File

core/lib/Drupal/Component/Serialization/YamlPecl.php, line 29

Class

YamlPecl
Provides default serialization for YAML using the PECL extension.

Namespace

Drupal\Component\Serialization

Code

public static function decode($raw) {
    static $init;
    if (!isset($init)) {
        // Decode binary, since Symfony YAML parser encodes binary from 3.1
        // onwards.
        ini_set('yaml.decode_binary', 1);
        // We never want to unserialize !php/object.
        ini_set('yaml.decode_php', 0);
        $init = TRUE;
    }
    // yaml_parse() will error with an empty value.
    if (!trim($raw)) {
        return NULL;
    }
    // @todo Use ErrorExceptions when https://drupal.org/node/1247666 is in.
    // yaml_parse() will throw errors instead of raising an exception. Until
    // such time as Drupal supports native PHP ErrorExceptions as the error
    // handler, we need to temporarily set the error handler as ::errorHandler()
    // and then restore it after decoding has occurred. This allows us to turn
    // parsing errors into a throwable exception.
    // @see Drupal\Component\Serialization\Exception\InvalidDataTypeException
    // @see http://php.net/manual/class.errorexception.php
    set_error_handler([
        __CLASS__,
        'errorHandler',
    ]);
    $ndocs = 0;
    $data = yaml_parse($raw, 0, $ndocs, [
        YAML_BOOL_TAG => '\\Drupal\\Component\\Serialization\\YamlPecl::applyBooleanCallbacks',
    ]);
    restore_error_handler();
    return $data;
}

API Navigation

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