From 75f956628de5554dff33d9691196ac0f5c39785f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 11 Dec 2024 09:47:08 -0500 Subject: [PATCH] PHPLIB-1600 Accept `stdClass` objects in `BSONDocument` constructor (#1551) --- src/Model/BSONDocument.php | 4 ++-- tests/Model/BSONDocumentTest.php | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Model/BSONDocument.php b/src/Model/BSONDocument.php index b9a9c0d80..1ff8bc1e6 100644 --- a/src/Model/BSONDocument.php +++ b/src/Model/BSONDocument.php @@ -52,10 +52,10 @@ public function __clone() * by default. * * @see https://php.net/arrayobject.construct - * @param array $input + * @param array|stdClass $input * @psalm-param class-string>|class-string> $iteratorClass */ - public function __construct(array $input = [], int $flags = ArrayObject::ARRAY_AS_PROPS, string $iteratorClass = ArrayIterator::class) + public function __construct(array|stdClass $input = [], int $flags = ArrayObject::ARRAY_AS_PROPS, string $iteratorClass = ArrayIterator::class) { parent::__construct($input, $flags, $iteratorClass); } diff --git a/tests/Model/BSONDocumentTest.php b/tests/Model/BSONDocumentTest.php index 4d832d591..2000aef63 100644 --- a/tests/Model/BSONDocumentTest.php +++ b/tests/Model/BSONDocumentTest.php @@ -23,6 +23,13 @@ public function testConstructorDefaultsToPropertyAccess(): void $this->assertSame('bar', $document->foo); } + public function testConstructorWithStandardObject(): void + { + $object = (object) ['foo' => 'bar']; + $document = new BSONDocument($object); + $this->assertEquals($object, $document->bsonSerialize()); + } + public function testBsonSerializeCastsToObject(): void { $data = [0 => 'foo', 2 => 'bar'];