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

Sign up
Appearance settings

Commit f4ac407

Browse files
Refactor ObjectDriver to accept yamlConfig array in constructor and update serialization method to use it
1 parent e9a5f9c commit f4ac407

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

‎src/Drivers/ObjectDriver.php‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,23 @@
1212

1313
class ObjectDriver implements Driver
1414
{
15+
private const DEFAULT_YAML_CONFIG = [
16+
'yaml_inline' => 2,
17+
'yaml_indent' => 4,
18+
'yaml_flags' => Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK,
19+
];
20+
21+
/**
22+
* @param array{
23+
* yaml_inline: int,
24+
* yaml_indent: int,
25+
* yaml_flags: int
26+
* } $yamlConfig
27+
*/
1528
public function __construct(
16-
protected int$yamlInline = 2,
29+
protected array$yamlConfig = [],
1730
) {
31+
$this->yamlConfig = array_merge(self::DEFAULT_YAML_CONFIG, $yamlConfig);
1832
}
1933

2034
public function serialize($data): string
@@ -31,11 +45,7 @@ public function serialize($data): string
3145
$serializer = new Serializer($normalizers, $encoders);
3246

3347
return $this->dedent(
34-
$serializer->serialize($data, 'yaml', [
35-
'yaml_inline' => $this->yamlInline,
36-
'yaml_indent' => 4,
37-
'yaml_flags' => Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK,
38-
])
48+
$serializer->serialize($data, 'yaml', $this->yamlConfig)
3949
);
4050
}
4151

‎tests/Unit/Drivers/ObjectDriverTest.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ public function it_can_serialize_a_class_instance()
9292
#[Test]
9393
public function it_can_serialize_with_custom_parameters()
9494
{
95-
$driver = new ObjectDriver(3);
95+
$driver = new ObjectDriver([
96+
'yaml_inline' => 3,
97+
]);
9698

9799
$nestedObject = (object) [
98100
'foo' => (object) [

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /