Skip to content

Recursively cast object to array when casting the main attribute to a… #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Eloquent/Casts/AsArrayObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function get($model, $key, $value, $attributes)
$data = $attributes[$key];

if (is_object($data)) {
$data = (array) $data;
$data = mapObjectToArray($data);
}

return is_array($data) ? new ArrayObject($data, ArrayObject::ARRAY_AS_PROPS) : null;
Expand Down
3 changes: 2 additions & 1 deletion src/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function fromJson($value, $asObject = false)
return (object) $value;
}

return (array) $value;
// Recursively cast objects within the value to arrays
return mapObjectToArray($value);
}
}
11 changes: 11 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ function isDotString(string $string): bool
}
}

if (!function_exists('mapObjectToArray')) {
function mapObjectToArray(mixed $value): mixed
{
if(!is_object($value) && !is_array($value)) {
return $value;
}

return array_map('mapObjectToArray', (array) $value);
}
}

if (!function_exists('renameArrayKey')) {
/**
* @param array<mixed> $array
Expand Down
18 changes: 18 additions & 0 deletions tests/Eloquent/CastAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
$profile = [
'firstName' => fake()->firstName,
'lastName' => fake()->lastName,
'address' => [
'streetAddress' => fake()->streetAddress,
'city' => fake()->city,
'postcode' => fake()->postcode,
'country' => fake()->country,
],
];

$user = User::create([
Expand All @@ -43,13 +49,22 @@

expect($user->profileAsArray)->toBeArray();
expect($refreshedUser->profileAsArray)->toBeArray();
expect($refreshedUser->profileAsArray['address'])->toBeArray();

expect($retrievedUser->profileAsArray)->toBeObject();
expect($retrievedUser->profileAsArray->address)->toBeObject();
});

test('Cast attribute to ArrayObject', function () {
$profile = [
'firstName' => fake()->firstName,
'lastName' => fake()->lastName,
'address' => [
'streetAddress' => fake()->streetAddress,
'city' => fake()->city,
'postcode' => fake()->postcode,
'country' => fake()->country,
],
];

$user = User::create([
Expand All @@ -66,8 +81,11 @@

expect($user->profileAsArrayObjectCast)->toBeInstanceOf(ArrayObject::class);
expect($refreshedUser->profileAsArrayObjectCast)->toBeInstanceOf(ArrayObject::class);
expect($refreshedUser->profileAsArrayObjectCast->address)->toBeArray();

expect($refreshedUser->profileAsArrayObjectCast->age)->toBe(24);
expect($retrievedUser->profileAsArrayObjectCast)->toBeObject();
expect($retrievedUser->profileAsArrayObjectCast->address)->toBeObject();
});

test('Cast attribute to object', function () {
Expand Down