Skip to content

Commit 4d4132e

Browse files
committed
PHPLIB-1600 Accept object in BSONDocument constructor
1 parent c386f6d commit 4d4132e

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-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 object|array<string, mixed> $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|object $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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ public function testConstructorDefaultsToPropertyAccess(): void
2323
$this->assertSame('bar', $document->foo);
2424
}
2525

26+
public function testConstructorWithStandardObject(): void
27+
{
28+
$document = new BSONDocument((object) ['foo' => 'bar']);
29+
$this->assertSame('bar', $document->foo);
30+
}
31+
32+
public function testConstructorWithClassObject(): void
33+
{
34+
$document = new BSONDocument(new class () {
35+
public string $foo = 'bar';
36+
protected string $baz = 'qux';
37+
});
38+
$this->assertSame('bar', $document->foo);
39+
$this->assertObjectNotHasProperty('baz', $document);
40+
}
41+
2642
public function testBsonSerializeCastsToObject(): void
2743
{
2844
$data = [0 => 'foo', 2 => 'bar'];

0 commit comments

Comments
 (0)