Commit 35aa9509 authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #94

parents 54b9605b f0826b23
...@@ -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);
} }
} }
<?php
namespace MongoDB\Tests;
use MongoDB\Model\BSONArray;
class BSONArrayTest extends TestCase
{
public function testBsonSerializeReindexesKeys()
{
$data = [0 => 'foo', 2 => 'bar'];
$array = new BSONArray($data);
$this->assertSame($data, $array->getArrayCopy());
$this->assertSame(['foo', 'bar'], $array->bsonSerialize());
}
}
<?php
namespace MongoDB\Tests;
use MongoDB\Model\BSONDocument;
use ArrayObject;
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()
{
$data = [0 => 'foo', 2 => 'bar'];
$document = new BSONDocument($data);
$this->assertSame($data, $document->getArrayCopy());
$this->assertEquals((object) [0 => 'foo', 2 => 'bar'], $document->bsonSerialize());
}
}
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