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 1b1825c

Browse files
Merge pull request #212 from tassilogroeper/feature-object-driver-nesting
Allow setting yamlInline for ObjectDriver
2 parents 3cf8433 + f4ac407 commit 1b1825c

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

‎src/Drivers/ObjectDriver.php‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@
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+
*/
28+
public function __construct(
29+
protected array $yamlConfig = [],
30+
) {
31+
$this->yamlConfig = array_merge(self::DEFAULT_YAML_CONFIG, $yamlConfig);
32+
}
33+
1534
public function serialize($data): string
1635
{
1736
$normalizers = [
@@ -26,11 +45,7 @@ public function serialize($data): string
2645
$serializer = new Serializer($normalizers, $encoders);
2746

2847
return $this->dedent(
29-
$serializer->serialize($data, 'yaml', [
30-
'yaml_inline' => 2,
31-
'yaml_indent' => 4,
32-
'yaml_flags' => Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK,
33-
])
48+
$serializer->serialize($data, 'yaml', $this->yamlConfig)
3449
);
3550
}
3651

‎tests/Unit/Drivers/ObjectDriverTest.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,29 @@ public function it_can_serialize_a_class_instance()
8888

8989
$this->assertEquals($expected, $driver->serialize(new Obj));
9090
}
91+
92+
#[Test]
93+
public function it_can_serialize_with_custom_parameters()
94+
{
95+
$driver = new ObjectDriver([
96+
'yaml_inline' => 3,
97+
]);
98+
99+
$nestedObject = (object) [
100+
'foo' => (object) [
101+
'bar' => (object) [
102+
'baz' => ['qux', 'quux'],
103+
],
104+
],
105+
];
106+
$expected = implode("\n", [
107+
'foo:',
108+
' bar:',
109+
' baz: [qux, quux]',
110+
'',
111+
]);
112+
$this->assertEquals($expected, $driver->serialize($nestedObject));
113+
}
91114
}
92115

93116
class Obj

0 commit comments

Comments
(0)

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