Skip to content

Commit

Permalink
Support for Collection casting
Browse files Browse the repository at this point in the history
  • Loading branch information
eugabrielsilva committed Oct 9, 2023
1 parent bcac5b0 commit 625acb4
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/Controllers/ExtendedElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Glowie\Core\Element;
use Glowie\Core\Traits\ElementTrait;
use Glowie\Core\Collection;
use Util;

/**
Expand Down Expand Up @@ -77,7 +78,7 @@ public function toJson(int $flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLAS
*/
private function iterateRecursive(array $data){
foreach($data as $key => $value){
if($this->isElementLike($value)){
if($this->isElementLike($value) || $value instanceof Collection){
$data[$key] = $this->iterateRecursive($value->toArray());
}else if(is_array($value)){
$data[$key] = $this->iterateRecursive($value);
Expand All @@ -98,6 +99,10 @@ private function setPropertyType($value){
$value->{self::PROP_NAME} = Util::encryptString(get_class($value));
}

if($value instanceof Collection && !isset($value[self::PROP_NAME])){
$value[self::PROP_NAME] = Util::encryptString(get_class($value));
}

return $value;
}

Expand Down Expand Up @@ -144,7 +149,24 @@ private function invokePropertyType($value){
unset($value->{self::PROP_NAME});

// Parse properties
if($this->isElementLike($newValue)) $newValue->set((array)$value);
if($this->isElementLike($newValue) || $newValue instanceof Collection) $newValue->set((array)$value);

// Return new instance
$value = $newValue;
}
}else if(is_array($value) && isset($value[self::PROP_NAME])){
// Checks for hashed array
$class = Util::decryptString($value[self::PROP_NAME]);

// Instantiate object
if($class && class_exists($class)){
$newValue = new $class();

// Remove objHash property from array
unset($value[self::PROP_NAME]);

// Parse properties
if($this->isElementLike($newValue) || $newValue instanceof Collection) $newValue->set((array)$value);

// Return new instance
$value = $newValue;
Expand Down

0 comments on commit 625acb4

Please sign in to comment.