Skip to content

Commit e9a5f9c

Browse files
allow setting yamlInline for ObjectDriver
1 parent 3cf8433 commit e9a5f9c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/Drivers/ObjectDriver.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
class ObjectDriver implements Driver
1414
{
15+
public function __construct(
16+
protected int $yamlInline = 2,
17+
) {
18+
}
19+
1520
public function serialize($data): string
1621
{
1722
$normalizers = [
@@ -27,7 +32,7 @@ public function serialize($data): string
2732

2833
return $this->dedent(
2934
$serializer->serialize($data, 'yaml', [
30-
'yaml_inline' => 2,
35+
'yaml_inline' => $this->yamlInline,
3136
'yaml_indent' => 4,
3237
'yaml_flags' => Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK,
3338
])

tests/Unit/Drivers/ObjectDriverTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,27 @@ 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(3);
96+
97+
$nestedObject = (object) [
98+
'foo' => (object) [
99+
'bar' => (object) [
100+
'baz' => ['qux', 'quux'],
101+
],
102+
],
103+
];
104+
$expected = implode("\n", [
105+
'foo:',
106+
' bar:',
107+
' baz: [qux, quux]',
108+
'',
109+
]);
110+
$this->assertEquals($expected, $driver->serialize($nestedObject));
111+
}
91112
}
92113

93114
class Obj

0 commit comments

Comments
 (0)