Skip to content

Commit

Permalink
Fix serialize data in array
Browse files Browse the repository at this point in the history
  • Loading branch information
DyanGalih committed Jun 30, 2019
1 parent 43a773a commit 1aeeb2b
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Tools/PopoTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,27 @@ public function serializeJson($object)
*/
public function serialize($object)
{
$objectAsArray = (array) $object;
$objectAsArray = (array)$object;
foreach ($objectAsArray as $key => $value) {
if (stripos($key, "\0") === 0) {
$newKey = $this->fixKeyName($key);
$this->replaceKey($objectAsArray, $key, $newKey);
}else{
} else {
$newKey = $key;
}

if($value instanceof Collection){
if ($value instanceof Collection) {
$value = $this->serialize($objectAsArray[$newKey]);
$objectAsArray[$newKey] = $value['items'];
}elseif(is_object($value)) {
} elseif (is_object($value)) {
$objectAsArray[$newKey] = $this->serialize($objectAsArray[$newKey]);
} elseif (is_array($value)) {
for ($i = 0; $i < count($value); $i++) {
$objectAsArray[$newKey][$i] = $this->serialize($value[$i]);
}
}
}

return $objectAsArray;
}

Expand All @@ -73,7 +77,7 @@ function replaceKey(&$array, $curkey, $newkey)
* @param string $oldKey
* @return string
*/
function fixKeyName(string $oldKey) : string
function fixKeyName(string $oldKey): string
{
return substr($oldKey, strpos($oldKey, "\0", 2) + 1);
}
Expand Down

0 comments on commit 1aeeb2b

Please sign in to comment.