Skip to content

Commit

Permalink
Merge pull request #78
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikola committed Jan 7, 2016
2 parents e8177e8 + 81fcefc commit 80a0d52
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ class Client
*/
public function __construct($uri = 'mongodb://localhost:27017', array $uriOptions = [], array $driverOptions = [])
{
$driverOptions += [
'typeMap' => [
'array' => 'MongoDB\Model\BSONArray',
'document' => 'MongoDB\Model\BSONDocument',
'root' => 'MongoDB\Model\BSONDocument',
],
];

if (isset($driverOptions['typeMap']) && ! is_array($driverOptions['typeMap'])) {
throw new InvalidArgumentTypeException('"typeMap" driver option', $driverOptions['typeMap'], 'array');
}
Expand Down
43 changes: 43 additions & 0 deletions src/Model/BSONArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace MongoDB\Model;

use MongoDB\BSON\Serializable;
use MongoDB\BSON\Unserializable;
use ArrayObject;

/**
* Model class for a BSON array.
*
* The internal data will be filtered through array_values() during BSON
* serialization to ensure that it becomes a BSON array.
*
* @api
*/
class BSONArray extends ArrayObject implements Serializable, Unserializable
{
/**
* Serialize the array to BSON.
*
* The array data will be numerically reindexed to ensure that it is stored
* as a BSON array.
*
* @see http://php.net/mongodb-bson-serializable.bsonserialize
* @return array
*/
public function bsonSerialize()
{
return array_values($this->getArrayCopy());
}

/**
* Unserialize the document to BSON.
*
* @see http://php.net/mongodb-bson-unserializable.bsonunserialize
* @param array $data Array data
*/
public function bsonUnserialize(array $data)
{
self::__construct($data);
}
}
40 changes: 40 additions & 0 deletions src/Model/BSONDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace MongoDB\Model;

use MongoDB\BSON\Serializable;
use MongoDB\BSON\Unserializable;
use ArrayObject;

/**
* Model class for a BSON document.
*
* The internal data will be cast to an object during BSON serialization to
* ensure that it becomes a BSON document.
*
* @api
*/
class BSONDocument extends ArrayObject implements Serializable, Unserializable
{
/**
* Serialize the document to BSON.
*
* @see http://php.net/mongodb-bson-serializable.bsonserialize
* @return object
*/
public function bsonSerialize()
{
return (object) $this->getArrayCopy();
}

/**
* Unserialize the document to BSON.
*
* @see http://php.net/mongodb-bson-unserializable.bsonunserialize
* @param array $data Array data
*/
public function bsonUnserialize(array $data)
{
self::__construct($data, ArrayObject::ARRAY_AS_PROPS);
}
}
3 changes: 2 additions & 1 deletion src/Model/IndexInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public function __toString()
* Serialize the index information to BSON for index creation.
*
* @see MongoDB\Collection::createIndexes()
* @see http://php.net/bson-serializable.bsonserialize
* @see http://php.net/mongodb-bson-serializable.bsonserialize
* @return array
*/
public function bsonSerialize()
{
Expand Down

0 comments on commit 80a0d52

Please sign in to comment.