Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 401]

   1  <?php
   2  /*
   3   * Copyright 2016-present MongoDB, Inc.
   4   *
   5   * Licensed under the Apache License, Version 2.0 (the "License");
   6   * you may not use this file except in compliance with the License.
   7   * You may obtain a copy of the License at
   8   *
   9   *   http://www.apache.org/licenses/LICENSE-2.0
  10   *
  11   * Unless required by applicable law or agreed to in writing, software
  12   * distributed under the License is distributed on an "AS IS" BASIS,
  13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14   * See the License for the specific language governing permissions and
  15   * limitations under the License.
  16   */
  17  
  18  namespace MongoDB\Model;
  19  
  20  use ArrayObject;
  21  use JsonSerializable;
  22  use MongoDB\BSON\Serializable;
  23  use MongoDB\BSON\Unserializable;
  24  use function MongoDB\recursive_copy;
  25  
  26  /**
  27   * Model class for a BSON document.
  28   *
  29   * The internal data will be cast to an object during BSON serialization to
  30   * ensure that it becomes a BSON document.
  31   *
  32   * @api
  33   */
  34  class BSONDocument extends ArrayObject implements JsonSerializable, Serializable, Unserializable
  35  {
  36      /**
  37       * Deep clone this BSONDocument.
  38       */
  39      public function __clone()
  40      {
  41          foreach ($this as $key => $value) {
  42              $this[$key] = recursive_copy($value);
  43          }
  44      }
  45  
  46      /**
  47       * This overrides the parent constructor to allow property access of entries
  48       * by default.
  49       *
  50       * @see http://php.net/arrayobject.construct
  51       * @param array   $input
  52       * @param integer $flags
  53       * @param string  $iterator_class
  54       */
  55      public function __construct($input = [], $flags = ArrayObject::ARRAY_AS_PROPS, $iterator_class = 'ArrayIterator')
  56      {
  57          parent::__construct($input, $flags, $iterator_class);
  58      }
  59  
  60      /**
  61       * Factory method for var_export().
  62       *
  63       * @see http://php.net/oop5.magic#object.set-state
  64       * @see http://php.net/var-export
  65       * @param array $properties
  66       * @return self
  67       */
  68      public static function __set_state(array $properties)
  69      {
  70          $document = new static();
  71          $document->exchangeArray($properties);
  72  
  73          return $document;
  74      }
  75  
  76      /**
  77       * Serialize the document to BSON.
  78       *
  79       * @see http://php.net/mongodb-bson-serializable.bsonserialize
  80       * @return object
  81       */
  82      public function bsonSerialize()
  83      {
  84          return (object) $this->getArrayCopy();
  85      }
  86  
  87      /**
  88       * Unserialize the document to BSON.
  89       *
  90       * @see http://php.net/mongodb-bson-unserializable.bsonunserialize
  91       * @param array $data Array data
  92       */
  93      public function bsonUnserialize(array $data)
  94      {
  95          parent::__construct($data, ArrayObject::ARRAY_AS_PROPS);
  96      }
  97  
  98      /**
  99       * Serialize the array to JSON.
 100       *
 101       * @see http://php.net/jsonserializable.jsonserialize
 102       * @return object
 103       */
 104      public function jsonSerialize()
 105      {
 106          return (object) $this->getArrayCopy();
 107      }
 108  }