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 a5e7572

Browse filesBrowse files
committed
forward the context from the JsonEncoder to JsonEncode and JsonDecode
1 parent dfcf900 commit a5e7572
Copy full SHA for a5e7572

File tree

3 files changed

+26
-4
lines changed
Filter options

3 files changed

+26
-4
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
->tag('serializer.encoder')
178178

179179
->set('serializer.encoder.json', JsonEncoder::class)
180+
->args([null, null])
180181
->tag('serializer.encoder')
181182

182183
->set('serializer.encoder.yaml', YamlEncoder::class)

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Encoder/JsonEncoder.php
+10-3Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,22 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
2323
protected $encodingImpl;
2424
protected $decodingImpl;
2525

26-
public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null)
26+
private $defaultContext;
27+
28+
public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null, array $defaultContext = [])
2729
{
28-
$this->encodingImpl = $encodingImpl ?? new JsonEncode();
29-
$this->decodingImpl = $decodingImpl ?? new JsonDecode([JsonDecode::ASSOCIATIVE => true]);
30+
$this->encodingImpl = $encodingImpl ?? new JsonEncode($defaultContext);
31+
$this->decodingImpl = $decodingImpl ?? new JsonDecode(array_merge([JsonDecode::ASSOCIATIVE => true], $defaultContext));
32+
$this->defaultContext = $defaultContext;
3033
}
3134

3235
/**
3336
* {@inheritdoc}
3437
*/
3538
public function encode(mixed $data, string $format, array $context = []): string
3639
{
40+
$context = array_merge($this->defaultContext, $context);
41+
3742
return $this->encodingImpl->encode($data, self::FORMAT, $context);
3843
}
3944

@@ -42,6 +47,8 @@ public function encode(mixed $data, string $format, array $context = []): string
4247
*/
4348
public function decode(string $data, string $format, array $context = []): mixed
4449
{
50+
$context = array_merge($this->defaultContext, $context);
51+
4552
return $this->decodingImpl->decode($data, self::FORMAT, $context);
4653
}
4754

‎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

0 commit comments

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