File tree Expand file tree Collapse file tree 4 files changed +93
-1
lines changed
Expand file tree Collapse file tree 4 files changed +93
-1
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 {
You can’t perform that action at this time.
0 commit comments