Skip to content

Commit 92da0ad

Browse files
improve array handling in getObjectWrapperAsArray once more (#4)
* Saves originalData and introduces method `getOriginalData` to access it; Adds method `getDataAsArray` to get data in array form * remove clone * improve array handling in getObjectWrapperAsArray * improve array handling in getObjectWrapperAsArray once more
1 parent 5fe747a commit 92da0ad

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/ObjectWrapper.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,23 @@ private function getObjectWrapperAsArray(ObjectWrapper $objectWrapper)
184184
$data[$key] = $this->getObjectWrapperAsArray($item);
185185
continue;
186186
} elseif (is_array($item)) {
187-
$data[$key] = [];
188-
foreach ($item as $arrayKey => $arrayItem) {
189-
$data[$key][$arrayKey] = $this->getObjectWrapperAsArray($arrayItem);
190-
}
187+
$data[$key] = $this->getObjectWrapperFromArray($item);
188+
} else {
189+
$data[$key] = $item;
190+
}
191+
}
192+
193+
return $data;
194+
}
195+
196+
private function getObjectWrapperFromArray(array $list)
197+
{
198+
$data = [];
199+
foreach ($list as $key => $item) {
200+
if ($item instanceof ObjectWrapper) {
201+
$data[$key] = $this->getObjectWrapperAsArray($item);
202+
} elseif (is_array($item)) {
203+
$data[$key] = $this->getObjectWrapperFromArray($item);
191204
} else {
192205
$data[$key] = $item;
193206
}

tests/ObjectWrapperTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ public function testGetOriginalDataAsArray()
411411
$secondArrayItem->b = 2;
412412
$thirdArrayItem = new stdClass();
413413
$thirdArrayItem->c = 3;
414-
$data->b = [$firstArrayItem, $secondArrayItem, $thirdArrayItem];
414+
$data->b = [$firstArrayItem, $secondArrayItem, $thirdArrayItem, 4];
415415

416416
$object = new ObjectWrapper($data);
417417
$originalData = $object->getDataAsArray();
@@ -422,6 +422,7 @@ public function testGetOriginalDataAsArray()
422422
['a' => 1],
423423
['b' => 2],
424424
['c' => 3],
425+
4,
425426
],
426427
];
427428
$this->assertDeepEquals($expectedArray, $originalData);

0 commit comments

Comments
 (0)