From 107e13f4b3afd8f53a7ab77a17ea5662a0e1f381 Mon Sep 17 00:00:00 2001 From: Denis Brumann Date: Sat, 7 Nov 2020 19:44:24 +0100 Subject: [PATCH] Adds constants for YamlEncoder options --- Encoder/YamlEncoder.php | 16 ++++++++++++---- Tests/Encoder/YamlEncoderTest.php | 6 +++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Encoder/YamlEncoder.php b/Encoder/YamlEncoder.php index a515b2002..63c3479a7 100644 --- a/Encoder/YamlEncoder.php +++ b/Encoder/YamlEncoder.php @@ -28,9 +28,17 @@ class YamlEncoder implements EncoderInterface, DecoderInterface public const PRESERVE_EMPTY_OBJECTS = 'preserve_empty_objects'; + public const YAML_INLINE = 'yaml_inline'; + public const YAML_INDENT = 'yaml_indent'; + public const YAML_FLAGS = 'yaml_flags'; + private $dumper; private $parser; - private $defaultContext = ['yaml_inline' => 0, 'yaml_indent' => 0, 'yaml_flags' => 0]; + private $defaultContext = [ + self::YAML_INLINE => 0, + self::YAML_INDENT => 0, + self::YAML_FLAGS => 0, + ]; public function __construct(Dumper $dumper = null, Parser $parser = null, array $defaultContext = []) { @@ -51,10 +59,10 @@ public function encode($data, string $format, array $context = []) $context = array_merge($this->defaultContext, $context); if (isset($context[self::PRESERVE_EMPTY_OBJECTS])) { - $context['yaml_flags'] |= Yaml::DUMP_OBJECT_AS_MAP; + $context[self::YAML_FLAGS] |= Yaml::DUMP_OBJECT_AS_MAP; } - return $this->dumper->dump($data, $context['yaml_inline'], $context['yaml_indent'], $context['yaml_flags']); + return $this->dumper->dump($data, $context[self::YAML_INLINE], $context[self::YAML_INDENT], $context[self::YAML_FLAGS]); } /** @@ -72,7 +80,7 @@ public function decode(string $data, string $format, array $context = []) { $context = array_merge($this->defaultContext, $context); - return $this->parser->parse($data, $context['yaml_flags']); + return $this->parser->parse($data, $context[self::YAML_FLAGS]); } /** diff --git a/Tests/Encoder/YamlEncoderTest.php b/Tests/Encoder/YamlEncoderTest.php index 27b98eabb..708d3dc62 100644 --- a/Tests/Encoder/YamlEncoderTest.php +++ b/Tests/Encoder/YamlEncoderTest.php @@ -60,7 +60,7 @@ public function testSupportsDecoding() public function testContext() { - $encoder = new YamlEncoder(new Dumper(), new Parser(), ['yaml_inline' => 1, 'yaml_indent' => 4, 'yaml_flags' => Yaml::DUMP_OBJECT | Yaml::PARSE_OBJECT]); + $encoder = new YamlEncoder(new Dumper(), new Parser(), [YamlEncoder::YAML_INLINE => 1, YamlEncoder::YAML_INDENT => 4, YamlEncoder::YAML_FLAGS => Yaml::DUMP_OBJECT | Yaml::PARSE_OBJECT]); $obj = new \stdClass(); $obj->bar = 2; @@ -68,8 +68,8 @@ public function testContext() $legacyTag = " foo: !php/object:O:8:\"stdClass\":1:{s:3:\"bar\";i:2;}\n"; $spacedTag = " foo: !php/object 'O:8:\"stdClass\":1:{s:3:\"bar\";i:2;}'\n"; $this->assertThat($encoder->encode(['foo' => $obj], 'yaml'), $this->logicalOr($this->equalTo($legacyTag), $this->equalTo($spacedTag))); - $this->assertEquals(' { foo: null }', $encoder->encode(['foo' => $obj], 'yaml', ['yaml_inline' => 0, 'yaml_indent' => 2, 'yaml_flags' => 0])); + $this->assertEquals(' { foo: null }', $encoder->encode(['foo' => $obj], 'yaml', [YamlEncoder::YAML_INLINE => 0, YamlEncoder::YAML_INDENT => 2, YamlEncoder::YAML_FLAGS => 0])); $this->assertEquals(['foo' => $obj], $encoder->decode("foo: !php/object 'O:8:\"stdClass\":1:{s:3:\"bar\";i:2;}'", 'yaml')); - $this->assertEquals(['foo' => null], $encoder->decode("foo: !php/object 'O:8:\"stdClass\":1:{s:3:\"bar\";i:2;}'", 'yaml', ['yaml_flags' => 0])); + $this->assertEquals(['foo' => null], $encoder->decode("foo: !php/object 'O:8:\"stdClass\":1:{s:3:\"bar\";i:2;}'", 'yaml', [YamlEncoder::YAML_FLAGS => 0])); } }