Skip to content

Commit 80a0d52

Browse files
committed
Merge pull request #78
2 parents e8177e8 + 81fcefc commit 80a0d52

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

src/Client.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ class Client
4040
*/
4141
public function __construct($uri = 'mongodb://localhost:27017', array $uriOptions = [], array $driverOptions = [])
4242
{
43+
$driverOptions += [
44+
'typeMap' => [
45+
'array' => 'MongoDB\Model\BSONArray',
46+
'document' => 'MongoDB\Model\BSONDocument',
47+
'root' => 'MongoDB\Model\BSONDocument',
48+
],
49+
];
50+
4351
if (isset($driverOptions['typeMap']) && ! is_array($driverOptions['typeMap'])) {
4452
throw new InvalidArgumentTypeException('"typeMap" driver option', $driverOptions['typeMap'], 'array');
4553
}

src/Model/BSONArray.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace MongoDB\Model;
4+
5+
use MongoDB\BSON\Serializable;
6+
use MongoDB\BSON\Unserializable;
7+
use ArrayObject;
8+
9+
/**
10+
* Model class for a BSON array.
11+
*
12+
* The internal data will be filtered through array_values() during BSON
13+
* serialization to ensure that it becomes a BSON array.
14+
*
15+
* @api
16+
*/
17+
class BSONArray extends ArrayObject implements Serializable, Unserializable
18+
{
19+
/**
20+
* Serialize the array to BSON.
21+
*
22+
* The array data will be numerically reindexed to ensure that it is stored
23+
* as a BSON array.
24+
*
25+
* @see http://php.net/mongodb-bson-serializable.bsonserialize
26+
* @return array
27+
*/
28+
public function bsonSerialize()
29+
{
30+
return array_values($this->getArrayCopy());
31+
}
32+
33+
/**
34+
* Unserialize the document to BSON.
35+
*
36+
* @see http://php.net/mongodb-bson-unserializable.bsonunserialize
37+
* @param array $data Array data
38+
*/
39+
public function bsonUnserialize(array $data)
40+
{
41+
self::__construct($data);
42+
}
43+
}

src/Model/BSONDocument.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace MongoDB\Model;
4+
5+
use MongoDB\BSON\Serializable;
6+
use MongoDB\BSON\Unserializable;
7+
use ArrayObject;
8+
9+
/**
10+
* Model class for a BSON document.
11+
*
12+
* The internal data will be cast to an object during BSON serialization to
13+
* ensure that it becomes a BSON document.
14+
*
15+
* @api
16+
*/
17+
class BSONDocument extends ArrayObject implements Serializable, Unserializable
18+
{
19+
/**
20+
* Serialize the document to BSON.
21+
*
22+
* @see http://php.net/mongodb-bson-serializable.bsonserialize
23+
* @return object
24+
*/
25+
public function bsonSerialize()
26+
{
27+
return (object) $this->getArrayCopy();
28+
}
29+
30+
/**
31+
* Unserialize the document to BSON.
32+
*
33+
* @see http://php.net/mongodb-bson-unserializable.bsonunserialize
34+
* @param array $data Array data
35+
*/
36+
public function bsonUnserialize(array $data)
37+
{
38+
self::__construct($data, ArrayObject::ARRAY_AS_PROPS);
39+
}
40+
}

src/Model/IndexInput.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ public function __toString()
7474
* Serialize the index information to BSON for index creation.
7575
*
7676
* @see MongoDB\Collection::createIndexes()
77-
* @see http://php.net/bson-serializable.bsonserialize
77+
* @see http://php.net/mongodb-bson-serializable.bsonserialize
78+
* @return array
7879
*/
7980
public function bsonSerialize()
8081
{

0 commit comments

Comments
 (0)