Skip to content

Commit 35aa950

Browse files
committed
Merge pull request #94
2 parents 54b9605 + f0826b2 commit 35aa950

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-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/BSONArrayTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace MongoDB\Tests;
4+
5+
use MongoDB\Model\BSONArray;
6+
7+
class BSONArrayTest extends TestCase
8+
{
9+
public function testBsonSerializeReindexesKeys()
10+
{
11+
$data = [0 => 'foo', 2 => 'bar'];
12+
13+
$array = new BSONArray($data);
14+
$this->assertSame($data, $array->getArrayCopy());
15+
$this->assertSame(['foo', 'bar'], $array->bsonSerialize());
16+
}
17+
}

tests/Model/BSONDocumentTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace MongoDB\Tests;
4+
5+
use MongoDB\Model\BSONDocument;
6+
use ArrayObject;
7+
8+
class BSONDocumentTest extends TestCase
9+
{
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+
17+
public function testBsonSerializeCastsToObject()
18+
{
19+
$data = [0 => 'foo', 2 => 'bar'];
20+
21+
$document = new BSONDocument($data);
22+
$this->assertSame($data, $document->getArrayCopy());
23+
$this->assertEquals((object) [0 => 'foo', 2 => 'bar'], $document->bsonSerialize());
24+
}
25+
}

0 commit comments

Comments
 (0)