@@ -42,7 +42,7 @@ public function encode($value, $depth, array $options, callable $encode)
42
42
43
43
/**
44
44
* Encodes the object as string according to encoding options.
45
- * @param object $object Object to convert to code
45
+ * @param object $object Object to encode as PHP
46
46
* @param array $options List of encoder options
47
47
* @param callable $encode Callback used to encode values
48
48
* @return string The object encoded as string
@@ -57,6 +57,22 @@ private function encodeObject($object, array $options, callable $encode)
57
57
return sprintf ('\\%s::__set_state(%s) ' , get_class ($ object ), $ encode ($ this ->getObjectState ($ object )));
58
58
}
59
59
60
+ return $ this ->encodeObjectArray ($ object , $ options , $ encode );
61
+ }
62
+
63
+ /**
64
+ * Encodes the object into one of the array formats.
65
+ * @param object $object Object to encode as PHP
66
+ * @param array $options List of encoder options
67
+ * @param callable $encode Callback used to encode values
68
+ * @return string The object encoded as string
69
+ */
70
+ private function encodeObjectArray ($ object , array $ options , callable $ encode )
71
+ {
72
+ if (!in_array ($ options ['object.format ' ], ['array ' , 'vars ' , 'iterate ' ])) {
73
+ throw new \RuntimeException ('Invalid object encoding format: ' . $ options ['object.format ' ]);
74
+ }
75
+
60
76
$ output = $ encode ($ this ->getObjectArray ($ object , $ options ['object.format ' ]));
61
77
62
78
if ($ options ['object.cast ' ]) {
@@ -79,15 +95,15 @@ private function getObjectArray($object, $format)
79
95
return (array ) $ object ;
80
96
} elseif ($ format === 'vars ' ) {
81
97
return get_object_vars ($ object );
82
- } elseif ($ format === 'iterate ' ) {
83
- $ array = [];
84
- foreach ($ object as $ key => $ value ) {
85
- $ array [$ key ] = $ value ;
86
- }
87
- return $ array ;
88
98
}
89
99
90
- throw new \RuntimeException ('Invalid object encoding format: ' . $ format );
100
+ $ array = [];
101
+
102
+ foreach ($ object as $ key => $ value ) {
103
+ $ array [$ key ] = $ value ;
104
+ }
105
+
106
+ return $ array ;
91
107
}
92
108
93
109
/**
0 commit comments