Skip to content

Commit

Permalink
PHPLIB-74: Model classes for BSON array and document
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikola committed Dec 28, 2015
1 parent 3c01d73 commit 03fde99
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 0 deletions.
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]);
}
}

0 comments on commit 03fde99

Please sign in to comment.