Skip to content

Commit f0826b2

Browse files
committed
BSONDocument should allow property access by default
Property access, which was already enabled on unserialization, allows code to work with the model as it would the driver's default stdClass
1 parent 1933e89 commit f0826b2

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Model/BSONDocument.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@
1616
*/
1717
class BSONDocument extends ArrayObject implements Serializable, Unserializable
1818
{
19+
/**
20+
* Constructor.
21+
*
22+
* This overrides the parent constructor to allow property access of entries
23+
* by default.
24+
*
25+
* @see http://php.net/arrayobject.construct
26+
*/
27+
public function __construct($input = [], $flags = ArrayObject::ARRAY_AS_PROPS, $iterator_class = 'ArrayIterator')
28+
{
29+
parent::__construct($input, $flags, $iterator_class);
30+
}
31+
1932
/**
2033
* Serialize the document to BSON.
2134
*
@@ -35,6 +48,6 @@ public function bsonSerialize()
3548
*/
3649
public function bsonUnserialize(array $data)
3750
{
38-
self::__construct($data, ArrayObject::ARRAY_AS_PROPS);
51+
parent::__construct($data, ArrayObject::ARRAY_AS_PROPS);
3952
}
4053
}

tests/Model/BSONDocumentTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77

88
class BSONDocumentTest extends TestCase
99
{
10+
public function testConstructorDefaultsToPropertyAccess()
11+
{
12+
$document = new BSONDocument(['foo' => 'bar']);
13+
$this->assertEquals(ArrayObject::ARRAY_AS_PROPS, $document->getFlags());
14+
$this->assertSame('bar', $document->foo);
15+
}
16+
1017
public function testBsonSerializeCastsToObject()
1118
{
1219
$data = [0 => 'foo', 2 => 'bar'];

0 commit comments

Comments
 (0)