Skip to content

Commit 1647c8b

Browse files
committed
Recursively convert objects to arrays.
1 parent fb1fc1e commit 1647c8b

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

src/Object/ObjectProxyTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,6 @@ public function exchangeArray(array $input)
237237
*/
238238
public function toArray()
239239
{
240-
return (array) $this->getProxy();
240+
return ObjectUtils::toArray($this->getProxy());
241241
}
242242
}

src/Object/ObjectUtils.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}

test/Object/ObjectProxyTraitTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
namespace CloudCreativity\JsonApi\Object;
2020

21+
use stdClass;
22+
2123
class ObjectProxyTraitTest extends \PHPUnit_Framework_TestCase
2224
{
2325

@@ -155,4 +157,28 @@ public function testArrayExchangeable()
155157
$this->assertEquals($arr, $this->trait->toArray());
156158
$this->assertEquals($this->proxy, $this->trait->getProxy());
157159
}
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+
}

0 commit comments

Comments
 (0)