-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathYamlEncoder.php
101 lines (84 loc) · 3.29 KB
/
YamlEncoder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Encoder;
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml;
/**
* Encodes YAML data.
*
* @author Kévin Dunglas <[email protected]>
*/
class YamlEncoder implements EncoderInterface, DecoderInterface
{
public const FORMAT = 'yaml';
private const ALTERNATIVE_FORMAT = 'yml';
public const PRESERVE_EMPTY_OBJECTS = 'preserve_empty_objects';
/**
* Override the amount of spaces to use for indentation of nested nodes.
*
* This option only works in the constructor, not in calls to `encode`.
*/
public const YAML_INDENTATION = 'yaml_indentation';
public const YAML_INLINE = 'yaml_inline';
/**
* Initial indentation for root element.
*/
public const YAML_INDENT = 'yaml_indent';
public const YAML_FLAGS = 'yaml_flags';
private readonly Dumper $dumper;
private readonly Parser $parser;
private array $defaultContext = [
self::YAML_INLINE => 0,
self::YAML_INDENT => 0,
self::YAML_FLAGS => 0,
];
public function __construct(?Dumper $dumper = null, ?Parser $parser = null, array $defaultContext = [])
{
if (!class_exists(Dumper::class)) {
throw new RuntimeException('The YamlEncoder class requires the "Yaml" component. Try running "composer require symfony/yaml".');
}
if (!$dumper) {
$dumper = \array_key_exists(self::YAML_INDENTATION, $defaultContext) ? new Dumper($defaultContext[self::YAML_INDENTATION]) : new Dumper();
}
$this->dumper = $dumper;
$this->parser = $parser ?? new Parser();
unset($defaultContext[self::YAML_INDENTATION]);
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
}
public function encode(mixed $data, string $format, array $context = []): string
{
$context = array_merge($this->defaultContext, $context);
if ($context[self::PRESERVE_EMPTY_OBJECTS] ?? false) {
$context[self::YAML_FLAGS] |= Yaml::DUMP_OBJECT_AS_MAP;
}
return $this->dumper->dump($data, $context[self::YAML_INLINE], $context[self::YAML_INDENT], $context[self::YAML_FLAGS]);
}
public function supportsEncoding(string $format): bool
{
return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
}
public function decode(string $data, string $format, array $context = []): mixed
{
$context = array_merge($this->defaultContext, $context);
try {
return $this->parser->parse($data, $context[self::YAML_FLAGS]);
} catch (ParseException $e) {
throw new NotEncodableValueException($e->getMessage(), $e->getCode(), $e);
}
}
public function supportsDecoding(string $format): bool
{
return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
}
}