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 6, 2016
2 parents e8177e8 + 03fde99 commit b45ff32
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 1 deletion.
113 changes: 113 additions & 0 deletions src/Model/BSONArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace MongoDB\Model;

use MongoDB\BSON\Serializable;
use MongoDB\BSON\Unserializable;
use ArrayAccess;

/**
* 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 implements ArrayAccess, Serializable, Unserializable
{
private $data;

/**
* Constructor.
*
* @param array $data Array data
*/
public function __construct(array $data)
{
$this->data = $data;
}

/**
* Return the array data.
*
* @see http://php.net/oop5.magic#language.oop5.magic.debuginfo
* @return array
*/
public function __debugInfo()
{
return $this->data;
}

/**
* 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->data);
}

/**
* Unserialize the document to BSON.
*
* @see http://php.net/mongodb-bson-unserializable.bsonunserialize
* @param array $data Array data
*/
public function bsonUnserialize(array $data)
{
$this->data = $data;
}

/**
* Check whether a field exists.
*
* @see http://php.net/arrayaccess.offsetexists
* @param mixed $key Field name
* @return boolean
*/
public function offsetExists($key)
{
return array_key_exists($key, $this->data);
}

/**
* Return the field's value.
*
* @see http://php.net/arrayaccess.offsetget
* @param mixed $key Field name
* @return mixed
*/
public function offsetGet($key)
{
return $this->data[$key];
}

/**
* Set the field's value.
*
* @see http://php.net/arrayaccess.offsetset
* @param mixed $key Field name
* @param mixed $value Field value
*/
public function offsetSet($key, $value)
{
$this->data[$key] = $value;
}

/**
* Unset the field.
*
* @see http://php.net/arrayaccess.offsetunset
* @param mixed $key Field name
*/
public function offsetUnset($key)
{
unset($this->data[$key]);
}
}
115 changes: 115 additions & 0 deletions src/Model/BSONDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace MongoDB\Model;

use MongoDB\BSON\Serializable;
use MongoDB\BSON\Unserializable;
use ArrayAccess;

/**
* 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 implements ArrayAccess, Serializable, Unserializable
{
private $data;

/**
* Constructor.
*
* @param array|object $data Document data
* @throws InvalidArgumentException
*/
public function __construct($data)
{
if ( ! is_array($data) && ! is_object($data)) {
throw new InvalidArgumentTypeException('$data', $data, 'array or object');
}

$this->data = (array) $document;
}

/**
* Return the document data.
*
* @see http://php.net/oop5.magic#language.oop5.magic.debuginfo
* @return array
*/
public function __debugInfo()
{
return $this->data;
}

/**
* Serialize the document to BSON.
*
* @see http://php.net/mongodb-bson-serializable.bsonserialize
* @return object
*/
public function bsonSerialize()
{
return (object) $this->data;
}

/**
* Unserialize the document to BSON.
*
* @see http://php.net/mongodb-bson-unserializable.bsonunserialize
* @param array $data Document data
*/
public function bsonUnserialize(array $data)
{
$this->data = $data;
}

/**
* Check whether a field exists.
*
* @see http://php.net/arrayaccess.offsetexists
* @param mixed $key Field name
* @return boolean
*/
public function offsetExists($key)
{
return array_key_exists($key, $this->data);
}

/**
* Return the field's value.
*
* @see http://php.net/arrayaccess.offsetget
* @param mixed $key Field name
* @return mixed
*/
public function offsetGet($key)
{
return $this->data[$key];
}

/**
* Set the field's value.
*
* @see http://php.net/arrayaccess.offsetset
* @param mixed $key Field name
* @param mixed $value Field value
*/
public function offsetSet($key, $value)
{
$this->data[$key] = $value;
}

/**
* Unset the field.
*
* @see http://php.net/arrayaccess.offsetunset
* @param mixed $key Field name
*/
public function offsetUnset($key)
{
unset($this->data[$key]);
}
}
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 b45ff32

Please sign in to comment.