Commit c094f990 authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-163: __set_state() for BSON array and document models

parent 35aa9509
...@@ -16,6 +16,22 @@ use ArrayObject; ...@@ -16,6 +16,22 @@ use ArrayObject;
*/ */
class BSONArray extends ArrayObject implements Serializable, Unserializable 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. * Serialize the array to BSON.
* *
......
...@@ -29,6 +29,22 @@ class BSONDocument extends ArrayObject implements Serializable, Unserializable ...@@ -29,6 +29,22 @@ class BSONDocument extends ArrayObject implements Serializable, Unserializable
parent::__construct($input, $flags, $iterator_class); 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. * Serialize the document to BSON.
* *
......
...@@ -14,4 +14,13 @@ class BSONArrayTest extends TestCase ...@@ -14,4 +14,13 @@ class BSONArrayTest extends TestCase
$this->assertSame($data, $array->getArrayCopy()); $this->assertSame($data, $array->getArrayCopy());
$this->assertSame(['foo', 'bar'], $array->bsonSerialize()); $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 ...@@ -22,4 +22,13 @@ class BSONDocumentTest extends TestCase
$this->assertSame($data, $document->getArrayCopy()); $this->assertSame($data, $document->getArrayCopy());
$this->assertEquals((object) [0 => 'foo', 2 => 'bar'], $document->bsonSerialize()); $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