File tree Expand file tree Collapse file tree 3 files changed +72
-2
lines changed Expand file tree Collapse file tree 3 files changed +72
-2
lines changed Original file line number Diff line number Diff line change @@ -237,6 +237,6 @@ public function exchangeArray(array $input)
237
237
*/
238
238
public function toArray ()
239
239
{
240
- return ( array ) $ this ->getProxy ();
240
+ return ObjectUtils:: toArray ( $ this ->getProxy () );
241
241
}
242
242
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /**
4
+ * Copyright 2015 Cloud Creativity Limited
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ namespace CloudCreativity \JsonApi \Object ;
20
+
21
+ use InvalidArgumentException ;
22
+
23
+ class ObjectUtils
24
+ {
25
+
26
+ /**
27
+ * @param $object
28
+ * @return array
29
+ */
30
+ public static function toArray ($ object )
31
+ {
32
+ if (!is_object ($ object )) {
33
+ throw new InvalidArgumentException ('Expecting an object to convert to an array. ' );
34
+ }
35
+
36
+ $ arr = [];
37
+
38
+ foreach (get_object_vars ($ object ) as $ key => $ value ) {
39
+ $ arr [$ key ] = is_object ($ value ) ? static ::toArray ($ value ) : $ value ;
40
+ }
41
+
42
+ return $ arr ;
43
+ }
44
+ }
Original file line number Diff line number Diff line change 18
18
19
19
namespace CloudCreativity \JsonApi \Object ;
20
20
21
+ use stdClass ;
22
+
21
23
class ObjectProxyTraitTest extends \PHPUnit_Framework_TestCase
22
24
{
23
25
@@ -155,4 +157,28 @@ public function testArrayExchangeable()
155
157
$ this ->assertEquals ($ arr , $ this ->trait ->toArray ());
156
158
$ this ->assertEquals ($ this ->proxy , $ this ->trait ->getProxy ());
157
159
}
158
- }
160
+
161
+ public function testToArrayRecursive ()
162
+ {
163
+ $ object = new stdClass ();
164
+ $ object ->foo = 'bar ' ;
165
+ $ object ->baz = new stdClass ();
166
+ $ object ->baz ->bat = 'bazbat ' ;
167
+ $ object ->baz ->foobar = new stdClass ();
168
+ $ object ->baz ->foobar ->bazbat = 'foobarbazbat ' ;
169
+
170
+ $ expected = [
171
+ 'foo ' => 'bar ' ,
172
+ 'baz ' => [
173
+ 'bat ' => 'bazbat ' ,
174
+ 'foobar ' => [
175
+ 'bazbat ' => 'foobarbazbat ' ,
176
+ ],
177
+ ],
178
+ ];
179
+
180
+ $ this ->trait ->setProxy ($ object );
181
+
182
+ $ this ->assertSame ($ expected , $ this ->trait ->toArray ());
183
+ }
184
+ }
You can’t perform that action at this time.
0 commit comments