Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/Drivers/ObjectDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@

class ObjectDriver implements Driver
{
private const DEFAULT_YAML_CONFIG = [
'yaml_inline' => 2,
'yaml_indent' => 4,
'yaml_flags' => Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK,
];

/**
* @param array{
* yaml_inline: int,
* yaml_indent: int,
* yaml_flags: int
* } $yamlConfig
*/
public function __construct(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this single parameter, let's allow passing an array with options.

The default options should be

  'yaml_inline' => 2,
  'yaml_indent' => 4,
  'yaml_flags' => Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK,

Any options passed should be merged with the defaults.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also thought about it. fine with me.
However, I had a hard time, testing the indentation. The dedent function does behave, like I expected.
E.g. When setting the intent to 2 only the first line is corrected, but the others are still indented by 4 ?!

If you are fine to have a test for only the inline var, I can do it in my PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are fine to have a test for only the inline var, I can do it in my PR.

That's ok!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! Requested changes are done!

protected array $yamlConfig = [],
) {
$this->yamlConfig = array_merge(self::DEFAULT_YAML_CONFIG, $yamlConfig);
}

public function serialize($data): string
{
$normalizers = [
Expand All @@ -26,11 +45,7 @@ public function serialize($data): string
$serializer = new Serializer($normalizers, $encoders);

return $this->dedent(
$serializer->serialize($data, 'yaml', [
'yaml_inline' => 2,
'yaml_indent' => 4,
'yaml_flags' => Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK,
])
$serializer->serialize($data, 'yaml', $this->yamlConfig)
);
}

Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/Drivers/ObjectDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,29 @@ public function it_can_serialize_a_class_instance()

$this->assertEquals($expected, $driver->serialize(new Obj));
}

#[Test]
public function it_can_serialize_with_custom_parameters()
{
$driver = new ObjectDriver([
'yaml_inline' => 3,
]);

$nestedObject = (object) [
'foo' => (object) [
'bar' => (object) [
'baz' => ['qux', 'quux'],
],
],
];
$expected = implode("\n", [
'foo:',
' bar:',
' baz: [qux, quux]',
'',
]);
$this->assertEquals($expected, $driver->serialize($nestedObject));
}
}

class Obj
Expand Down