Skip to content

Commit 75f9566

Browse files
authored
PHPLIB-1600 Accept stdClass objects in BSONDocument constructor (#1551)
1 parent 0aa6da8 commit 75f9566

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/Model/BSONDocument.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ public function __clone()
5252
* by default.
5353
*
5454
* @see https://php.net/arrayobject.construct
55-
* @param array<string, mixed> $input
55+
* @param array<string, mixed>|stdClass $input
5656
* @psalm-param class-string<ArrayIterator<string,mixed>>|class-string<ArrayObject<string,mixed>> $iteratorClass
5757
*/
58-
public function __construct(array $input = [], int $flags = ArrayObject::ARRAY_AS_PROPS, string $iteratorClass = ArrayIterator::class)
58+
public function __construct(array|stdClass $input = [], int $flags = ArrayObject::ARRAY_AS_PROPS, string $iteratorClass = ArrayIterator::class)
5959
{
6060
parent::__construct($input, $flags, $iteratorClass);
6161
}

tests/Model/BSONDocumentTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ public function testConstructorDefaultsToPropertyAccess(): void
2323
$this->assertSame('bar', $document->foo);
2424
}
2525

26+
public function testConstructorWithStandardObject(): void
27+
{
28+
$object = (object) ['foo' => 'bar'];
29+
$document = new BSONDocument($object);
30+
$this->assertEquals($object, $document->bsonSerialize());
31+
}
32+
2633
public function testBsonSerializeCastsToObject(): void
2734
{
2835
$data = [0 => 'foo', 2 => 'bar'];

0 commit comments

Comments
 (0)