Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add context option to configure the indentation of nested nodes for YamlEncoder #47243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 3 src/Symfony/Component/Serializer/Encoder/JsonEncode.php
Original file line number Diff line number Diff line change
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 16 src/Symfony/Component/Serializer/Encoder/YamlEncoder.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.