You are here

public static function YamlPecl::decode in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Component/Serialization/YamlPecl.php \Drupal\Component\Serialization\YamlPecl::decode()

Decodes data from the serialization format.

Parameters

string $raw: The raw data string to decode.

Return value

mixed The decoded data.

Throws

\Drupal\Component\Serialization\Exception\InvalidDataTypeException

Overrides SerializationInterface::decode

6 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.
YamlTest::testObjectSupportDisabledPecl in core/tests/Drupal/Tests/Component/Serialization/YamlTest.php
Ensures that decoding php objects does not work in PECL.

... See full list

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