8818e525345d3a47eb7c701a4e0b61e0b2c5a92a
[yaffs-website] / web / core / lib / Drupal / Component / Serialization / YamlSymfony.php
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
9 /**
10  * Default serialization for YAML using the Symfony component.
11  */
12 class YamlSymfony implements SerializationInterface {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static function encode($data) {
18     try {
19       $yaml = new Dumper();
20       $yaml->setIndentation(2);
21       return $yaml->dump($data, PHP_INT_MAX, 0, TRUE, FALSE);
22     }
23     catch (\Exception $e) {
24       throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
25     }
26   }
27
28   /**
29    * {@inheritdoc}
30    */
31   public static function decode($raw) {
32     try {
33       $yaml = new Parser();
34       // Make sure we have a single trailing newline. A very simple config like
35       // 'foo: bar' with no newline will fail to parse otherwise.
36       return $yaml->parse($raw, TRUE, FALSE);
37     }
38     catch (\Exception $e) {
39       throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
40     }
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public static function getFileExtension() {
47     return 'yml';
48   }
49
50 }