Skip to content

Commit

Permalink
[Serializer] Add context option to configure the indentation of neste…
Browse files Browse the repository at this point in the history
…d nodes for YamlEncoder
  • Loading branch information
dbu committed Sep 29, 2022
1 parent c5b4aa4 commit 29673ae
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* Deprecate calling `AttributeMetadata::setSerializedName()`, `ClassMetadata::setClassDiscriminatorMapping()` without arguments
* Change the signature of `AttributeMetadataInterface::setSerializedName()` to `setSerializedName(?string)`
* Change the signature of `ClassMetadataInterface::setClassDiscriminatorMapping()` to `setClassDiscriminatorMapping(?ClassDiscriminatorMapping)`
* Add option YamlEncoder::YAML_INDENTATION to YamlEncoder constructor options to configure additional indentation for each level of nesting. This allows configuring indentation in the service configuration.

6.1
---
Expand Down
4 changes: 4 additions & 0 deletions Context/Encoder/YamlEncoderContextBuilder.php
Expand Up @@ -18,6 +18,10 @@
/**
* A helper providing autocompletion for available YamlEncoder options.
*
* Note that the "indentation" setting is not offered in this builder because
* it can only be set during the construction of the YamlEncoder, but not per
* call.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class YamlEncoderContextBuilder implements ContextBuilderInterface
Expand Down
3 changes: 3 additions & 0 deletions Encoder/JsonEncode.php
Expand Up @@ -20,6 +20,9 @@
*/
class JsonEncode implements EncoderInterface
{
/**
* Configure the JSON flags bitmask.
*/
public const OPTIONS = 'json_encode_options';

private $defaultContext = [
Expand Down
16 changes: 15 additions & 1 deletion Encoder/YamlEncoder.php
Expand Up @@ -28,7 +28,17 @@ class YamlEncoder implements EncoderInterface, DecoderInterface

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';

Expand All @@ -46,8 +56,12 @@ public function __construct(Dumper $dumper = null, Parser $parser = null, array
throw new RuntimeException('The YamlEncoder class requires the "Yaml" component. Install "symfony/yaml" to use it.');
}

$this->dumper = $dumper ?? new Dumper();
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);
}

Expand Down
15 changes: 12 additions & 3 deletions Tests/Encoder/YamlEncoderTest.php
Expand Up @@ -13,8 +13,6 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Encoder\YamlEncoder;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml;

/**
Expand Down Expand Up @@ -58,9 +56,20 @@ public function testSupportsDecoding()
$this->assertFalse($encoder->supportsDecoding('json'));
}

public function testIndentation()
{
$encoder = new YamlEncoder(null, null, [YamlEncoder::YAML_INLINE => 100, YamlEncoder::YAML_INDENTATION => 7]);

$expected = <<<'END'
foo:
bar: baz
END;
$this->assertSame($expected."\n", $encoder->encode(['foo' => ['bar' => 'baz']], 'yaml'));
}

public function testContext()
{
$encoder = new YamlEncoder(new Dumper(), new Parser(), [YamlEncoder::YAML_INLINE => 1, YamlEncoder::YAML_INDENT => 4, YamlEncoder::YAML_FLAGS => Yaml::DUMP_OBJECT | Yaml::PARSE_OBJECT]);
$encoder = new YamlEncoder(null, null, [YamlEncoder::YAML_INLINE => 1, YamlEncoder::YAML_INDENT => 4, YamlEncoder::YAML_FLAGS => Yaml::DUMP_OBJECT | Yaml::PARSE_OBJECT]);

$obj = new \stdClass();
$obj->bar = 2;
Expand Down

0 comments on commit 29673ae

Please sign in to comment.