@@ -27,22 +27,23 @@ public function __construct(ScalarToNodeConverterInterface $scalarToNodeConverte
27
27
/**
28
28
* @param array|\Traversable $data
29
29
* @param string $path
30
+ * @param bool $allowSerializeEmpty
30
31
*
31
32
* @return AbstractParentNode
32
33
*
33
34
* @throws \InvalidArgumentException
34
35
*/
35
- public function convert ($ data , string $ path = '' ): AbstractParentNode
36
+ public function convert ($ data , string $ path = '' , bool $ allowSerializeEmpty = false ): AbstractParentNode
36
37
{
37
38
if (!is_array ($ data ) && !$ data instanceof \Traversable) {
38
39
throw new \InvalidArgumentException (sprintf ('Params need to be array or %s ' , \Traversable::class));
39
40
}
40
41
41
42
$ isArray = $ this ->isArray ($ data );
42
- $ parentNode = $ this ->getParentNode ($ isArray );
43
+ $ parentNode = $ this ->getParentNode ($ isArray, $ allowSerializeEmpty );
43
44
44
45
foreach ($ data as $ key => $ value ) {
45
- $ this ->addChildNode ($ parentNode , $ key , $ value , $ path , $ isArray );
46
+ $ this ->addChildNode ($ parentNode , $ key , $ value , $ path , $ isArray, $ allowSerializeEmpty );
46
47
}
47
48
48
49
return $ parentNode ;
@@ -69,16 +70,17 @@ private function isArray($data): bool
69
70
70
71
/**
71
72
* @param bool $isArray
73
+ * @param bool $allowSerializeEmpty
72
74
*
73
75
* @return AbstractParentNode
74
76
*/
75
- private function getParentNode (bool $ isArray ): AbstractParentNode
77
+ private function getParentNode (bool $ isArray, bool $ allowSerializeEmpty ): AbstractParentNode
76
78
{
77
79
if ($ isArray ) {
78
- return ArrayNode::create ();
80
+ return ArrayNode::create ($ allowSerializeEmpty );
79
81
}
80
82
81
- return ObjectNode::create ();
83
+ return ObjectNode::create ($ allowSerializeEmpty );
82
84
}
83
85
84
86
/**
@@ -87,11 +89,18 @@ private function getParentNode(bool $isArray): AbstractParentNode
87
89
* @param mixed $value
88
90
* @param string $path
89
91
* @param bool $isArray
92
+ * @param bool $allowSerializeEmpty
90
93
*/
91
- private function addChildNode (AbstractParentNode $ parentNode , $ key , $ value , string $ path , bool $ isArray )
92
- {
94
+ private function addChildNode (
95
+ AbstractParentNode $ parentNode ,
96
+ $ key ,
97
+ $ value ,
98
+ string $ path ,
99
+ bool $ isArray ,
100
+ bool $ allowSerializeEmpty
101
+ ) {
93
102
$ subPath = $ this ->getSubPath ($ path , $ key , $ isArray );
94
- $ node = $ this ->getNode ($ value , $ subPath );
103
+ $ node = $ this ->getNode ($ value , $ subPath, $ allowSerializeEmpty );
95
104
96
105
if ($ isArray ) {
97
106
$ parentNode ->add ($ node );
@@ -121,15 +130,16 @@ private function getSubPath(string $path, $key, bool $isArray): string
121
130
/**
122
131
* @param mixed $value
123
132
* @param string $path
133
+ * @param bool $allowSerializeEmpty
124
134
*
125
135
* @return AbstractNode
126
136
*/
127
- private function getNode ($ value , string $ path ): AbstractNode
137
+ private function getNode ($ value , string $ path, bool $ allowSerializeEmpty ): AbstractNode
128
138
{
129
139
if (is_array ($ value ) || $ value instanceof \Traversable) {
130
- return $ this ->convert ($ value , $ path );
140
+ return $ this ->convert ($ value , $ path, $ allowSerializeEmpty );
131
141
}
132
142
133
- return $ this ->scalarToNodeConverter ->convert ($ value , $ path );
143
+ return $ this ->scalarToNodeConverter ->convert ($ value , $ path, $ allowSerializeEmpty );
134
144
}
135
145
}
0 commit comments