Commit f0826b23 authored by Jeremy Mikola's avatar Jeremy Mikola

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
parent 1933e89f
...@@ -16,6 +16,19 @@ use ArrayObject; ...@@ -16,6 +16,19 @@ use ArrayObject;
*/ */
class BSONDocument extends ArrayObject implements Serializable, Unserializable class BSONDocument extends ArrayObject implements Serializable, Unserializable
{ {
/**
* Constructor.
*
* This overrides the parent constructor to allow property access of entries
* by default.
*
* @see http://php.net/arrayobject.construct
*/
public function __construct($input = [], $flags = ArrayObject::ARRAY_AS_PROPS, $iterator_class = 'ArrayIterator')
{
parent::__construct($input, $flags, $iterator_class);
}
/** /**
* Serialize the document to BSON. * Serialize the document to BSON.
* *
...@@ -35,6 +48,6 @@ class BSONDocument extends ArrayObject implements Serializable, Unserializable ...@@ -35,6 +48,6 @@ class BSONDocument extends ArrayObject implements Serializable, Unserializable
*/ */
public function bsonUnserialize(array $data) public function bsonUnserialize(array $data)
{ {
self::__construct($data, ArrayObject::ARRAY_AS_PROPS); parent::__construct($data, ArrayObject::ARRAY_AS_PROPS);
} }
} }
...@@ -7,6 +7,13 @@ use ArrayObject; ...@@ -7,6 +7,13 @@ use ArrayObject;
class BSONDocumentTest extends TestCase class BSONDocumentTest extends TestCase
{ {
public function testConstructorDefaultsToPropertyAccess()
{
$document = new BSONDocument(['foo' => 'bar']);
$this->assertEquals(ArrayObject::ARRAY_AS_PROPS, $document->getFlags());
$this->assertSame('bar', $document->foo);
}
public function testBsonSerializeCastsToObject() public function testBsonSerializeCastsToObject()
{ {
$data = [0 => 'foo', 2 => 'bar']; $data = [0 => 'foo', 2 => 'bar'];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment