File tree 2 files changed +55
-4
lines changed
2 files changed +55
-4
lines changed Original file line number Diff line number Diff line change @@ -56,17 +56,28 @@ protected function recursiveSetValues(array &$array)
56
56
57
57
/**
58
58
* @param array $array
59
+ * @param null $parentKey
60
+ * @param null $currentKey
59
61
*/
60
- protected function recursiveFlattenOneElementObjectsToScalarType (array &$ array )
62
+ protected function recursiveFlattenOneElementObjectsToScalarType (array &$ array, $ parentKey = null , $ currentKey = null )
61
63
{
62
64
if (1 === \count ($ array ) && \is_scalar (\end ($ array ))) {
63
- $ array = \array_pop ($ array );
65
+ if ($ parentKey == $ currentKey ) {
66
+ $ array = \array_pop ($ array );
67
+ }
64
68
}
65
69
66
70
if (\is_array ($ array )) {
67
- foreach ($ array as &$ value ) {
71
+ foreach ($ array as $ parentKey => &$ value ) {
72
+
68
73
if (\is_array ($ value )) {
69
- $ this ->recursiveFlattenOneElementObjectsToScalarType ($ value );
74
+ $ key = null ;
75
+ foreach ($ value as $ key => $ v ) {
76
+ if (is_array ($ v )) {
77
+ $ this ->recursiveFlattenOneElementObjectsToScalarType ($ v , $ parentKey , $ key );
78
+ }
79
+ }
80
+ $ this ->recursiveFlattenOneElementObjectsToScalarType ($ value , $ parentKey , $ key );
70
81
}
71
82
}
72
83
}
Original file line number Diff line number Diff line change 23
23
24
24
class JsonTransformerTest extends \PHPUnit_Framework_TestCase
25
25
{
26
+
27
+ public function testSerializationObjectWithArrayOfOneAttribute ()
28
+ {
29
+ $ object = new \stdClass ();
30
+ $ object ->payload = ['userId ' => 1 ];
31
+
32
+ $ serializer = new DeepCopySerializer (new JsonTransformer ());
33
+
34
+ $ expected = <<<STRING
35
+ {
36
+ "payload": {
37
+ "userId": 1
38
+ }
39
+ }
40
+ STRING ;
41
+
42
+ $ this ->assertEquals ($ expected , $ serializer ->serialize ($ object ));
43
+ }
44
+
45
+ public function testSerializationObjectWithObjectOfOneAttribute ()
46
+ {
47
+ $ internal = new \stdClass ();
48
+ $ internal ->userId = 1 ;
49
+
50
+ $ object = new \stdClass ();
51
+ $ object ->payload = $ internal ;
52
+
53
+ $ serializer = new DeepCopySerializer (new JsonTransformer ());
54
+
55
+ $ expected = <<<STRING
56
+ {
57
+ "payload": {
58
+ "userId": 1
59
+ }
60
+ }
61
+ STRING ;
62
+
63
+ $ this ->assertEquals ($ expected , $ serializer ->serialize ($ object ));
64
+ }
65
+
26
66
public function testSerialization ()
27
67
{
28
68
$ object = $ this ->getObject ();
You can’t perform that action at this time.
0 commit comments