Skip to content
Closed
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
11 changes: 8 additions & 3 deletions src/Database/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,7 @@ public function getDocument(Document $collection, string $id, array $queries = [

$resultArray = $this->client->toArray($result[0]);
$result = $this->replaceChars('_', '$', $resultArray);
$document = new Document($result);
$document = new Document($this->convertStdClassToArray($result));
$document = $this->castingAfter($collection, $document);

// Ensure missing relationship attributes are set to null (MongoDB doesn't store null fields)
Expand Down Expand Up @@ -1308,6 +1308,7 @@ public function createDocument(Document $collection, Document $document): Docume
$options = $this->getTransactionOptions();
$result = $this->insertDocument($name, $this->removeNullKeys($record), $options);
$result = $this->replaceChars('_', '$', $result);
$result = $this->convertStdClassToArray($result);
// in order to keep the original object refrence.
foreach ($result as $key => $value) {
$document->setAttribute($key, $value);
Expand Down Expand Up @@ -1366,7 +1367,7 @@ public function castingAfter(Document $collection, Document $document): Document
switch ($type) {
case Database::VAR_INTEGER:
case Database::VAR_BIGINT:
$node = (int)$node;
$node = is_object($node) ? (int)(string)$node : (int)$node;
break;
case Database::VAR_DATETIME:
$node = $this->convertUTCDateToString($node);
Expand Down Expand Up @@ -1400,6 +1401,10 @@ public function castingAfter(Document $collection, Document $document): Document

private function convertStdClassToArray(mixed $value): mixed
{
if (is_object($value) && (get_class($value) === 'MongoDB\BSON\Int64' || $value instanceof \MongoDB\BSON\Int64)) {
return (int)(string)$value;
}

if (is_object($value) && get_class($value) === stdClass::class) {
return array_map($this->convertStdClassToArray(...), get_object_vars($value));
}
Expand Down Expand Up @@ -1587,7 +1592,7 @@ public function createDocuments(Document $collection, array $documents): array

foreach ($documents as $index => $document) {
$documents[$index] = $this->replaceChars('_', '$', $this->client->toArray($document));
$documents[$index] = new Document($documents[$index]);
$documents[$index] = new Document($this->convertStdClassToArray($documents[$index]));
}

return $documents;
Expand Down
17 changes: 13 additions & 4 deletions src/Database/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,21 @@ public function __construct(array $input = [])
}

foreach ($value as $childKey => $child) {
// An array value is either a list of nested sub-documents or a list of
// plain items (dates, numbers, strings): wrap the former, leave the latter.
// is_array() tells them apart and avoids array-accessing a non-array
// value (e.g. a UTCDateTime), which would otherwise fatal.
if ($child instanceof self) {
continue;
}

if (\is_array($child) && (isset($child['$id']) || isset($child['$collection']))) {
$value[$childKey] = new self($child);
} elseif ($child instanceof \ArrayAccess && (isset($child['$id']) || isset($child['$collection']))) {
if ($child instanceof \Traversable) {
$value[$childKey] = new self(\iterator_to_array($child));
} elseif (\method_exists($child, 'getArrayCopy')) {
$value[$childKey] = new self($child->getArrayCopy());
} elseif (\method_exists($child, 'toArray')) {
$value[$childKey] = new self($child->toArray());
}
// If none of the above, we cannot safely enumerate the offsets — leave as-is.
}
}

Expand Down
42 changes: 42 additions & 0 deletions tests/unit/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,46 @@ public function testEmptyDocumentSequence(): void
$this->assertNull($empty->getSequence());
$this->assertNotSame('', $empty->getSequence());
}

public function testNonArrayAccessObjectsInArray(): void
{
$mockObject = new class () {
public function __toString(): string
{
return '-3408048000';
}
};

$doc = new Document([
'$id' => 'test_non_array_access',
'listOfIntegers' => [
$mockObject,
],
]);

$this->assertEquals('test_non_array_access', $doc->getId());
$this->assertCount(1, $doc->getAttribute('listOfIntegers'));
$this->assertSame($mockObject, $doc->getAttribute('listOfIntegers')[0]);
}

public function testArrayAccessHydrationInArray(): void
{
$arrayAccessObject = new \ArrayObject([
'$id' => 'nested_doc',
'name' => 'nested_name',
]);

$doc = new Document([
'$id' => 'parent_doc',
'children' => [
$arrayAccessObject,
],
]);

$this->assertEquals('parent_doc', $doc->getId());
$this->assertCount(1, $doc->getAttribute('children'));
$this->assertInstanceOf(Document::class, $doc->getAttribute('children')[0]);
$this->assertEquals('nested_doc', $doc->getAttribute('children')[0]->getId());
$this->assertEquals('nested_name', $doc->getAttribute('children')[0]->getAttribute('name'));
}
}
Loading