Skip to content

Commit

Permalink
BSONDocument should only accept stdClass
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Dec 10, 2024
1 parent 4d4132e commit f2c8766
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 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 object|array<string, mixed> $input
* @param array<string, mixed>|stdClass $input
* @psalm-param class-string<ArrayIterator<string,mixed>>|class-string<ArrayObject<string,mixed>> $iteratorClass
*/
public function __construct(array|object $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);
}
Expand Down
15 changes: 4 additions & 11 deletions tests/Model/BSONDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,11 @@ public function testConstructorDefaultsToPropertyAccess(): void

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';
});
$object = (object) ['foo' => 'bar'];
$document = new BSONDocument($object);
$this->assertEquals(ArrayObject::ARRAY_AS_PROPS, $document->getFlags());
$this->assertSame('bar', $document->foo);
$this->assertObjectNotHasProperty('baz', $document);
$this->assertEquals($object, $document->bsonSerialize());
}

public function testBsonSerializeCastsToObject(): void
Expand Down

0 comments on commit f2c8766

Please sign in to comment.