function YamlPecl::decode

Same name in other branches
  1. 9 core/lib/Drupal/Component/Serialization/YamlPecl.php \Drupal\Component\Serialization\YamlPecl::decode()
  2. 8.9.x core/lib/Drupal/Component/Serialization/YamlPecl.php \Drupal\Component\Serialization\YamlPecl::decode()
  3. 11.x core/lib/Drupal/Component/Serialization/YamlPecl.php \Drupal\Component\Serialization\YamlPecl::decode()

Overrides SerializationInterface::decode

4 calls to YamlPecl::decode()
YamlPeclTest::testDecode in core/tests/Drupal/Tests/Component/Serialization/YamlPeclTest.php
Tests decoding YAML node anchors.
YamlPeclTest::testEncodeDecode in core/tests/Drupal/Tests/Component/Serialization/YamlPeclTest.php
Tests encoding and decoding basic data structures.
YamlPeclTest::testError in core/tests/Drupal/Tests/Component/Serialization/YamlPeclTest.php
Tests that invalid YAML throws an exception.
YamlPeclTest::testObjectSupportDisabled in core/tests/Drupal/Tests/Component/Serialization/YamlPeclTest.php
Ensures that php object support is disabled.

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.