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

Commit c304930

Browse filesBrowse files
committed
feature #47243 Add context option to configure the indentation of nested nodes for YamlEncoder (dbu)
This PR was merged into the 6.2 branch. Discussion ---------- Add context option to configure the indentation of nested nodes for `YamlEncoder` | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Allow to configure the amount of spaces for indentation in Yaml Dumper. Commits ------- f1dfb8f [Serializer] Add context option to configure the indentation of nested nodes for YamlEncoder
2 parents 6b56480 + f1dfb8f commit c304930
Copy full SHA for c304930

File tree

5 files changed

+35
-4
lines changed
Filter options

5 files changed

+35
-4
lines changed

‎src/Symfony/Component/Serializer/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* Deprecate calling `AttributeMetadata::setSerializedName()`, `ClassMetadata::setClassDiscriminatorMapping()` without arguments
1111
* Change the signature of `AttributeMetadataInterface::setSerializedName()` to `setSerializedName(?string)`
1212
* Change the signature of `ClassMetadataInterface::setClassDiscriminatorMapping()` to `setClassDiscriminatorMapping(?ClassDiscriminatorMapping)`
13+
* 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.
1314

1415
6.1
1516
---

‎src/Symfony/Component/Serializer/Context/Encoder/YamlEncoderContextBuilder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Context/Encoder/YamlEncoderContextBuilder.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
/**
1919
* A helper providing autocompletion for available YamlEncoder options.
2020
*
21+
* Note that the "indentation" setting is not offered in this builder because
22+
* it can only be set during the construction of the YamlEncoder, but not per
23+
* call.
24+
*
2125
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
2226
*/
2327
final class YamlEncoderContextBuilder implements ContextBuilderInterface

‎src/Symfony/Component/Serializer/Encoder/JsonEncode.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Encoder/JsonEncode.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
*/
2121
class JsonEncode implements EncoderInterface
2222
{
23+
/**
24+
* Configure the JSON flags bitmask.
25+
*/
2326
public const OPTIONS = 'json_encode_options';
2427

2528
private $defaultContext = [

‎src/Symfony/Component/Serializer/Encoder/YamlEncoder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Encoder/YamlEncoder.php
+15-1Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,17 @@ class YamlEncoder implements EncoderInterface, DecoderInterface
2828

2929
public const PRESERVE_EMPTY_OBJECTS = 'preserve_empty_objects';
3030

31+
/**
32+
* Override the amount of spaces to use for indentation of nested nodes.
33+
*
34+
* This option only works in the constructor, not in calls to `encode`.
35+
*/
36+
public const YAML_INDENTATION = 'yaml_indentation';
37+
3138
public const YAML_INLINE = 'yaml_inline';
39+
/**
40+
* Initial indentation for root element.
41+
*/
3242
public const YAML_INDENT = 'yaml_indent';
3343
public const YAML_FLAGS = 'yaml_flags';
3444

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

49-
$this->dumper = $dumper ?? new Dumper();
59+
if (!$dumper) {
60+
$dumper = \array_key_exists(self::YAML_INDENTATION, $defaultContext) ? new Dumper($defaultContext[self::YAML_INDENTATION]) : new Dumper();
61+
}
62+
$this->dumper = $dumper;
5063
$this->parser = $parser ?? new Parser();
64+
unset($defaultContext[self::YAML_INDENTATION]);
5165
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
5266
}
5367

‎src/Symfony/Component/Serializer/Tests/Encoder/YamlEncoderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Encoder/YamlEncoderTest.php
+12-3Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Serializer\Encoder\YamlEncoder;
16-
use Symfony\Component\Yaml\Dumper;
17-
use Symfony\Component\Yaml\Parser;
1816
use Symfony\Component\Yaml\Yaml;
1917

2018
/**
@@ -58,9 +56,20 @@ public function testSupportsDecoding()
5856
$this->assertFalse($encoder->supportsDecoding('json'));
5957
}
6058

59+
public function testIndentation()
60+
{
61+
$encoder = new YamlEncoder(null, null, [YamlEncoder::YAML_INLINE => 100, YamlEncoder::YAML_INDENTATION => 7]);
62+
63+
$expected = <<<'END'
64+
foo:
65+
bar: baz
66+
END;
67+
$this->assertSame($expected."\n", $encoder->encode(['foo' => ['bar' => 'baz']], 'yaml'));
68+
}
69+
6170
public function testContext()
6271
{
63-
$encoder = new YamlEncoder(new Dumper(), new Parser(), [YamlEncoder::YAML_INLINE => 1, YamlEncoder::YAML_INDENT => 4, YamlEncoder::YAML_FLAGS => Yaml::DUMP_OBJECT | Yaml::PARSE_OBJECT]);
72+
$encoder = new YamlEncoder(null, null, [YamlEncoder::YAML_INLINE => 1, YamlEncoder::YAML_INDENT => 4, YamlEncoder::YAML_FLAGS => Yaml::DUMP_OBJECT | Yaml::PARSE_OBJECT]);
6473

6574
$obj = new \stdClass();
6675
$obj->bar = 2;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.