Skip to content

Commit

Permalink
Adds constants for YamlEncoder options
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Brumann committed Nov 8, 2020
1 parent bc25fbd commit 107e13f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 12 additions & 4 deletions Encoder/YamlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
{
Expand All @@ -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]);
}

/**
Expand All @@ -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]);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions Tests/Encoder/YamlEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ 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;

$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]));
}
}

0 comments on commit 107e13f

Please sign in to comment.