Mercurial > hg > isophonics-drupal-site
annotate core/lib/Drupal/Component/Serialization/YamlSymfony.php @ 19:fa3358dc1485 tip
Add ndrum files
author | Chris Cannam |
---|---|
date | Wed, 28 Aug 2019 13:14:47 +0100 |
parents | 129ea1e6d783 |
children |
rev | line source |
---|---|
Chris@0 | 1 <?php |
Chris@0 | 2 |
Chris@0 | 3 namespace Drupal\Component\Serialization; |
Chris@0 | 4 |
Chris@0 | 5 use Drupal\Component\Serialization\Exception\InvalidDataTypeException; |
Chris@0 | 6 use Symfony\Component\Yaml\Parser; |
Chris@0 | 7 use Symfony\Component\Yaml\Dumper; |
Chris@0 | 8 use Symfony\Component\Yaml\Yaml as SymfonyYaml; |
Chris@0 | 9 |
Chris@0 | 10 /** |
Chris@0 | 11 * Default serialization for YAML using the Symfony component. |
Chris@0 | 12 */ |
Chris@0 | 13 class YamlSymfony implements SerializationInterface { |
Chris@0 | 14 |
Chris@0 | 15 /** |
Chris@0 | 16 * {@inheritdoc} |
Chris@0 | 17 */ |
Chris@0 | 18 public static function encode($data) { |
Chris@0 | 19 try { |
Chris@0 | 20 // Set the indentation to 2 to match Drupal's coding standards. |
Chris@0 | 21 $yaml = new Dumper(2); |
Chris@0 | 22 return $yaml->dump($data, PHP_INT_MAX, 0, SymfonyYaml::DUMP_EXCEPTION_ON_INVALID_TYPE); |
Chris@0 | 23 } |
Chris@0 | 24 catch (\Exception $e) { |
Chris@0 | 25 throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e); |
Chris@0 | 26 } |
Chris@0 | 27 } |
Chris@0 | 28 |
Chris@0 | 29 /** |
Chris@0 | 30 * {@inheritdoc} |
Chris@0 | 31 */ |
Chris@0 | 32 public static function decode($raw) { |
Chris@0 | 33 try { |
Chris@0 | 34 $yaml = new Parser(); |
Chris@0 | 35 // Make sure we have a single trailing newline. A very simple config like |
Chris@0 | 36 // 'foo: bar' with no newline will fail to parse otherwise. |
Chris@17 | 37 return $yaml->parse($raw, SymfonyYaml::PARSE_EXCEPTION_ON_INVALID_TYPE); |
Chris@0 | 38 } |
Chris@0 | 39 catch (\Exception $e) { |
Chris@0 | 40 throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e); |
Chris@0 | 41 } |
Chris@0 | 42 } |
Chris@0 | 43 |
Chris@0 | 44 /** |
Chris@0 | 45 * {@inheritdoc} |
Chris@0 | 46 */ |
Chris@0 | 47 public static function getFileExtension() { |
Chris@0 | 48 return 'yml'; |
Chris@0 | 49 } |
Chris@0 | 50 |
Chris@0 | 51 } |