3 namespace Drupal\Component\Serialization;
5 use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
8 * Provides default serialization for YAML using the PECL extension.
10 class YamlPecl implements SerializationInterface {
15 public static function encode($data) {
18 ini_set('yaml.output_indent', 2);
19 // Do not break lines at 80 characters.
20 ini_set('yaml.output_width', -1);
23 return yaml_emit($data, YAML_UTF8_ENCODING, YAML_LN_BREAK);
29 public static function decode($raw) {
32 // We never want to unserialize !php/object.
33 ini_set('yaml.decode_php', 0);
36 // yaml_parse() will error with an empty value.
40 // @todo Use ErrorExceptions when https://drupal.org/node/1247666 is in.
41 // yaml_parse() will throw errors instead of raising an exception. Until
42 // such time as Drupal supports native PHP ErrorExceptions as the error
43 // handler, we need to temporarily set the error handler as ::errorHandler()
44 // and then restore it after decoding has occurred. This allows us to turn
45 // parsing errors into a throwable exception.
46 // @see Drupal\Component\Serialization\Exception\InvalidDataTypeException
47 // @see http://php.net/manual/en/class.errorexception.php
48 set_error_handler([__CLASS__, 'errorHandler']);
50 $data = yaml_parse($raw, 0, $ndocs, [
51 YAML_BOOL_TAG => '\Drupal\Component\Serialization\YamlPecl::applyBooleanCallbacks',
53 restore_error_handler();
58 * Handles errors for \Drupal\Component\Serialization\YamlPecl::decode().
60 * @param int $severity
61 * The severity level of the error.
62 * @param string $message
63 * The error message to display.
65 * @see \Drupal\Component\Serialization\YamlPecl::decode()
67 public static function errorHandler($severity, $message) {
68 restore_error_handler();
69 throw new InvalidDataTypeException($message, $severity);
75 public static function getFileExtension() {
80 * Applies callbacks after parsing to ignore 1.1 style booleans.
83 * Value from YAML file.
85 * Tag that triggered the callback.
87 * Scalar entity style flags.
90 * FALSE, false, TRUE and true are returned as booleans, everything else is
91 * returned as a string.
93 public static function applyBooleanCallbacks($value, $tag, $flags) {
94 // YAML 1.1 spec dictates that 'Y', 'N', 'y' and 'n' are booleans. But, we
95 // want the 1.2 behavior, so we only consider 'false', 'FALSE', 'true' and
96 // 'TRUE' as booleans.
97 if (!in_array(strtolower($value), ['false', 'true'], TRUE)) {
104 return $map[strtolower($value)];