3 namespace Drupal\Component\Serialization;
5 use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
6 use Symfony\Component\Yaml\Parser;
7 use Symfony\Component\Yaml\Dumper;
8 use Symfony\Component\Yaml\Yaml as SymfonyYaml;
11 * Default serialization for YAML using the Symfony component.
13 class YamlSymfony implements SerializationInterface {
18 public static function encode($data) {
20 // Set the indentation to 2 to match Drupal's coding standards.
21 $yaml = new Dumper(2);
22 return $yaml->dump($data, PHP_INT_MAX, 0, SymfonyYaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
24 catch (\Exception $e) {
25 throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
32 public static function decode($raw) {
35 // Make sure we have a single trailing newline. A very simple config like
36 // 'foo: bar' with no newline will fail to parse otherwise.
37 return $yaml->parse($raw, SymfonyYaml::PARSE_EXCEPTION_ON_INVALID_TYPE | SymfonyYaml::PARSE_KEYS_AS_STRINGS);
39 catch (\Exception $e) {
40 throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
47 public static function getFileExtension() {