You are here

public static function YamlPecl::applyBooleanCallbacks in Drupal 10

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

Applies callbacks after parsing to ignore 1.1 style booleans.

Parameters

mixed $value: Value from YAML file.

string $tag: Tag that triggered the callback.

int $flags: Scalar entity style flags.

Return value

string|bool FALSE, false, TRUE and true are returned as booleans, everything else is returned as a string.

1 call to YamlPecl::applyBooleanCallbacks()
YamlPeclTest::testApplyBooleanCallbacks in core/tests/Drupal/Tests/Component/Serialization/YamlPeclTest.php
Tests YAML boolean callback.

File

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

Class

YamlPecl
Provides default serialization for YAML using the PECL extension.

Namespace

Drupal\Component\Serialization

Code

public static function applyBooleanCallbacks($value, $tag, $flags) {

  // YAML 1.1 spec dictates that 'Y', 'N', 'y' and 'n' are booleans. But, we
  // want the 1.2 behavior, so we only consider 'false', 'FALSE', 'true' and
  // 'TRUE' as booleans.
  if (!in_array(strtolower($value), [
    'false',
    'true',
  ], TRUE)) {
    return $value;
  }
  $map = [
    'false' => FALSE,
    'true' => TRUE,
  ];
  return $map[strtolower($value)];
}