Skip to content

Commit 1e494b6

Browse files
split conversion function, and change test
1 parent 460f548 commit 1e494b6

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

src/Eloquent/Model.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -319,31 +319,44 @@ protected function fromDecimal($value, $decimals)
319319
return new Decimal128($this->asDecimal($value, $decimals));
320320
}
321321

322+
/**
323+
* Convert MongoDB objects to their string representations.
324+
*
325+
* This method converts MongoDB related objects (ObjectID, Binary, UTCDateTime)
326+
* to their serialized representations, ensuring the values can be correctly
327+
* serialized when the model is converted to JSON.
328+
*
329+
* @param mixed $value The value to convert
330+
*
331+
* @return mixed
332+
*/
333+
protected function convertMongoObjects(mixed $value): mixed
334+
{
335+
if ($value instanceof ObjectID) {
336+
$value = (string) $value;
337+
} elseif ($value instanceof Binary) {
338+
$value = (string) $value->getData();
339+
} elseif ($value instanceof UTCDateTime) {
340+
$value = $this->serializeDate($value->toDateTime());
341+
} elseif (is_array($value)) {
342+
foreach ($value as &$nestedValue) {
343+
$nestedValue = $this->convertMongoObjects($nestedValue);
344+
}
345+
}
346+
347+
return $value;
348+
}
349+
322350
/** @inheritdoc */
323351
public function attributesToArray()
324352
{
325353
$attributes = parent::attributesToArray();
326354

327-
// Because the original Eloquent never returns objects, we convert
328355
// MongoDB related objects to a string representation. This kind
329356
// of mimics the SQL behaviour so that dates are formatted
330357
// nicely when your models are converted to JSON.
331-
$convertMongoObjects = function (&$value) use (&$convertMongoObjects) {
332-
if ($value instanceof ObjectID) {
333-
$value = (string) $value;
334-
} elseif ($value instanceof Binary) {
335-
$value = (string) $value->getData();
336-
} elseif ($value instanceof UTCDateTime) {
337-
$value = $this->serializeDate($value->toDateTime());
338-
} elseif (is_array($value)) {
339-
foreach ($value as &$embedValue) {
340-
$convertMongoObjects($embedValue);
341-
}
342-
}
343-
};
344-
345358
foreach ($attributes as $key => &$value) {
346-
$convertMongoObjects($value);
359+
$value = $this->convertMongoObjects($value);
347360
}
348361

349362
return $attributes;

tests/EmbeddedRelationsTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use MongoDB\Laravel\Tests\Models\User;
2020

2121
use function array_merge;
22-
use function PHPUnit\Framework\assertIsString;
2322

2423
class EmbeddedRelationsTest extends TestCase
2524
{
@@ -34,6 +33,7 @@ public function tearDown(): void
3433
Client::truncate();
3534
Group::truncate();
3635
Photo::truncate();
36+
Address::truncate();
3737
}
3838

3939
public function testEmbedsManySave()
@@ -949,10 +949,13 @@ public function testGetQueueableRelationsEmbedsOne()
949949

950950
public function testEmbedManySerialization()
951951
{
952-
$user = User::create(['name' => 'John Doe']);
953-
$user->addresses()->save(new Address(['city' => 'New York']));
952+
$address = new Address(['country' => 'France']);
953+
$address->save();
954+
955+
$address->addresses()->create(['city' => 'Paris']);
956+
$address->addresses()->create(['city' => 'Nice']);
954957

955-
$results = $user->toArray();
958+
$results = $address->toArray();
956959
$this->assertIsString($results['addresses'][0]['_id']);
957960
}
958961
}

0 commit comments

Comments
 (0)