Skip to content

Commit

Permalink
PHPLIB-1600 Accept object in BSONDocument constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Dec 4, 2024
1 parent c386f6d commit 4d4132e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Model/BSONDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public function __clone()
* by default.
*
* @see https://php.net/arrayobject.construct
* @param array<string, mixed> $input
* @param object|array<string, mixed> $input
* @psalm-param class-string<ArrayIterator<string,mixed>>|class-string<ArrayObject<string,mixed>> $iteratorClass
*/
public function __construct(array $input = [], int $flags = ArrayObject::ARRAY_AS_PROPS, string $iteratorClass = ArrayIterator::class)
public function __construct(array|object $input = [], int $flags = ArrayObject::ARRAY_AS_PROPS, string $iteratorClass = ArrayIterator::class)
{
parent::__construct($input, $flags, $iteratorClass);

Check failure on line 60 in src/Model/BSONDocument.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyInvalidArgument

src/Model/BSONDocument.php:60:29: PossiblyInvalidArgument: Argument 1 of ArrayObject::__construct expects (array<TKey:ArrayObject as array-key, TValue:ArrayObject as mixed>)|null|object, but possibly different type array<string, mixed>|object provided (see https://psalm.dev/092)
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Model/BSONDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ public function testConstructorDefaultsToPropertyAccess(): void
$this->assertSame('bar', $document->foo);
}

public function testConstructorWithStandardObject(): void
{
$document = new BSONDocument((object) ['foo' => 'bar']);
$this->assertSame('bar', $document->foo);
}

public function testConstructorWithClassObject(): void
{
$document = new BSONDocument(new class () {
public string $foo = 'bar';
protected string $baz = 'qux';
});
$this->assertSame('bar', $document->foo);
$this->assertObjectNotHasProperty('baz', $document);
}

public function testBsonSerializeCastsToObject(): void
{
$data = [0 => 'foo', 2 => 'bar'];
Expand Down

0 comments on commit 4d4132e

Please sign in to comment.