| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Serializer\Encoder; |
| 13 |
|
| 14 |
use Symfony\Component\Serializer\Exception\NotEncodableValueException; |
| 15 |
use Symfony\Component\Serializer\Exception\RuntimeException; |
| 16 |
use Symfony\Component\Yaml\Dumper; |
| 17 |
use Symfony\Component\Yaml\Exception\ParseException; |
| 18 |
use Symfony\Component\Yaml\Parser; |
| 19 |
use Symfony\Component\Yaml\Yaml; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
> |
| 25 |
|
| 26 |
class YamlEncoder implements EncoderInterface, DecoderInterface |
| 27 |
{ |
| 28 |
public const FORMAT = 'yaml'; |
| 29 |
private const ALTERNATIVE_FORMAT = 'yml'; |
| 30 |
|
| 31 |
public const PRESERVE_EMPTY_OBJECTS = 'preserve_empty_objects'; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public const YAML_INDENTATION = 'yaml_indentation'; |
| 39 |
|
| 40 |
public const YAML_INLINE = 'yaml_inline'; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public const YAML_INDENT = 'yaml_indent'; |
| 45 |
public const YAML_FLAGS = 'yaml_flags'; |
| 46 |
|
| 47 |
private readonly Dumper $dumper; |
| 48 |
private readonly Parser $parser; |
| 49 |
private array $defaultContext = [ |
| 50 |
self::YAML_INLINE => 0, |
| 51 |
self::YAML_INDENT => 0, |
| 52 |
self::YAML_FLAGS => 0, |
| 53 |
]; |
| 54 |
|
| 55 |
public function __construct(?Dumper $dumper = null, ?Parser $parser = null, array $defaultContext = []) |
| 56 |
{ |
| 57 |
if (!class_exists(Dumper::class)) { |
| 58 |
throw new RuntimeException('The YamlEncoder class requires the "Yaml" component. Try running "composer require symfony/yaml".'); |
| 59 |
} |
| 60 |
|
| 61 |
if (!$dumper) { |
| 62 |
$dumper = \array_key_exists(self::YAML_INDENTATION, $defaultContext) ? new Dumper($defaultContext[self::YAML_INDENTATION]) : new Dumper(); |
| 63 |
} |
| 64 |
$this->dumper = $dumper; |
| 65 |
$this->parser = $parser ?? new Parser(); |
| 66 |
unset($defaultContext[self::YAML_INDENTATION]); |
| 67 |
$this->defaultContext = array_merge($this->defaultContext, $defaultContext); |
| 68 |
} |
| 69 |
|
| 70 |
public function encode(mixed $data, string $format, array $context = []): string |
| 71 |
{ |
| 72 |
$context = array_merge($this->defaultContext, $context); |
| 73 |
|
| 74 |
if ($context[self::PRESERVE_EMPTY_OBJECTS] ?? false) { |
| 75 |
$context[self::YAML_FLAGS] |= Yaml::DUMP_OBJECT_AS_MAP; |
| 76 |
} |
| 77 |
|
| 78 |
return $this->dumper->dump($data, $context[self::YAML_INLINE], $context[self::YAML_INDENT], $context[self::YAML_FLAGS]); |
| 79 |
} |
| 80 |
|
| 81 |
public function supportsEncoding(string $format): bool |
| 82 |
{ |
| 83 |
return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format; |
| 84 |
} |
| 85 |
|
| 86 |
public function decode(string $data, string $format, array $context = []): mixed |
| 87 |
{ |
| 88 |
$context = array_merge($this->defaultContext, $context); |
| 89 |
|
| 90 |
try { |
| 91 |
return $this->parser->parse($data, $context[self::YAML_FLAGS]); |
| 92 |
} catch (ParseException $e) { |
| 93 |
throw new NotEncodableValueException($e->getMessage(), $e->getCode(), $e); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public function supportsDecoding(string $format): bool |
| 98 |
{ |
| 99 |
return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format; |
| 100 |
} |
| 101 |
} |