Skip to content

Commit

Permalink
fix: toRawArray() behavior
Browse files Browse the repository at this point in the history
The previous code no longer makes sense.
  • Loading branch information
kenjis committed Sep 30, 2023
1 parent 5b13b24 commit c5576d9
Showing 1 changed file with 2 additions and 36 deletions.
38 changes: 2 additions & 36 deletions system/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function toArray(bool $onlyChanged = false, bool $cast = true, bool $recu
}

/**
* Returns the raw values of the current attributes.
* Returns the values for database of the current attributes.
*
* @param bool $onlyChanged If true, only return values that have changed since object creation
* @param bool $recursive If true, inner entities will be cast as array as well.
Expand All @@ -213,41 +213,7 @@ public function toArray(bool $onlyChanged = false, bool $cast = true, bool $recu
*/
public function toRawArray(bool $onlyChanged = false, bool $recursive = false): array
{
$return = [];

if (! $onlyChanged) {
if ($recursive) {
return array_map(static function ($value) use ($onlyChanged, $recursive) {
if ($value instanceof self) {
$value = $value->toRawArray($onlyChanged, $recursive);
} elseif (is_callable([$value, 'toRawArray'])) {
$value = $value->toRawArray();
}

return $value;
}, $this->attributes);
}

return $this->attributes;
}

foreach ($this->attributes as $key => $value) {
if (! $this->hasChanged($key)) {
continue;
}

if ($recursive) {
if ($value instanceof self) {
$value = $value->toRawArray($onlyChanged, $recursive);
} elseif (is_callable([$value, 'toRawArray'])) {
$value = $value->toRawArray();
}
}

$return[$key] = $value;
}

return $return;
return $this->toDatabase($onlyChanged, $recursive);
}

/**
Expand Down

0 comments on commit c5576d9

Please sign in to comment.