File tree Expand file tree Collapse file tree 2 files changed +43
-5
lines changed Expand file tree Collapse file tree 2 files changed +43
-5
lines changed Original file line number Diff line number Diff line change 1212
1313class 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
Original file line number Diff line number Diff 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
93116class Obj
You can’t perform that action at this time.
0 commit comments