BSONDocument.php 1.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?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
{
19 20 21 22 23 24 25 26 27 28 29 30 31
    /**
     * Constructor.
     *
     * This overrides the parent constructor to allow property access of entries
     * by default.
     *
     * @see http://php.net/arrayobject.construct
     */
    public function __construct($input = [], $flags = ArrayObject::ARRAY_AS_PROPS, $iterator_class = 'ArrayIterator')
    {
        parent::__construct($input, $flags, $iterator_class);
    }

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    /**
     * 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;
    }

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    /**
     * 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)
    {
67
        parent::__construct($data, ArrayObject::ARRAY_AS_PROPS);
68 69
    }
}