Commit 7039bd7a authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #96

parents 35aa9509 c094f990
......@@ -16,6 +16,22 @@ use ArrayObject;
*/
class BSONArray extends ArrayObject implements Serializable, Unserializable
{
/**
* Factory method for var_export().
*
* @see http://php.net/oop5.magic#object.set-state
* @see http://php.net/var-export
* @param array $properties
* @return self
*/
public static function __set_state(array $properties)
{
$array = new static;
$array->exchangeArray($properties);
return $array;
}
/**
* Serialize the array to BSON.
*
......
......@@ -29,6 +29,22 @@ class BSONDocument extends ArrayObject implements Serializable, Unserializable
parent::__construct($input, $flags, $iterator_class);
}
/**
* Factory method for var_export().
*
* @see http://php.net/oop5.magic#object.set-state
* @see http://php.net/var-export
* @param array $properties
* @return self
*/
public static function __set_state(array $properties)
{
$document = new static;
$document->exchangeArray($properties);
return $document;
}
/**
* Serialize the document to BSON.
*
......
......@@ -14,4 +14,13 @@ class BSONArrayTest extends TestCase
$this->assertSame($data, $array->getArrayCopy());
$this->assertSame(['foo', 'bar'], $array->bsonSerialize());
}
public function testSetState()
{
$data = ['foo', 'bar'];
$array = BSONArray::__set_state($data);
$this->assertInstanceOf('MongoDB\Model\BSONArray', $array);
$this->assertSame($data, $array->getArrayCopy());
}
}
......@@ -22,4 +22,13 @@ class BSONDocumentTest extends TestCase
$this->assertSame($data, $document->getArrayCopy());
$this->assertEquals((object) [0 => 'foo', 2 => 'bar'], $document->bsonSerialize());
}
public function testSetState()
{
$data = ['foo' => 'bar'];
$document = BSONDocument::__set_state($data);
$this->assertInstanceOf('MongoDB\Model\BSONDocument', $document);
$this->assertSame($data, $document->getArrayCopy());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment