comparison core/lib/Drupal/Component/Serialization/YamlSymfony.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Component\Serialization;
4
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;
9
10 /**
11 * Default serialization for YAML using the Symfony component.
12 */
13 class YamlSymfony implements SerializationInterface {
14
15 /**
16 * {@inheritdoc}
17 */
18 public static function encode($data) {
19 try {
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);
23 }
24 catch (\Exception $e) {
25 throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
26 }
27 }
28
29 /**
30 * {@inheritdoc}
31 */
32 public static function decode($raw) {
33 try {
34 $yaml = new Parser();
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);
38 }
39 catch (\Exception $e) {
40 throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
41 }
42 }
43
44 /**
45 * {@inheritdoc}
46 */
47 public static function getFileExtension() {
48 return 'yml';
49 }
50
51 }