Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 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   *   https://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 ReturnTypeWillChange;
  25  
  26  use function MongoDB\recursive_copy;
  27  
  28  /**
  29   * Model class for a BSON document.
  30   *
  31   * The internal data will be cast to an object during BSON serialization to
  32   * ensure that it becomes a BSON document.
  33   *
  34   * @api
  35   */
  36  class BSONDocument extends ArrayObject implements JsonSerializable, Serializable, Unserializable
  37  {
  38      /**
  39       * Deep clone this BSONDocument.
  40       */
  41      public function __clone()
  42      {
  43          foreach ($this as $key => $value) {
  44              $this[$key] = recursive_copy($value);
  45          }
  46      }
  47  
  48      /**
  49       * This overrides the parent constructor to allow property access of entries
  50       * by default.
  51       *
  52       * @see https://php.net/arrayobject.construct
  53       */
  54      public function __construct(array $input = [], int $flags = ArrayObject::ARRAY_AS_PROPS, string $iteratorClass = 'ArrayIterator')
  55      {
  56          parent::__construct($input, $flags, $iteratorClass);
  57      }
  58  
  59      /**
  60       * Factory method for var_export().
  61       *
  62       * @see https://php.net/oop5.magic#object.set-state
  63       * @see https://php.net/var-export
  64       * @return self
  65       */
  66      public static function __set_state(array $properties)
  67      {
  68          $document = new static();
  69          $document->exchangeArray($properties);
  70  
  71          return $document;
  72      }
  73  
  74      /**
  75       * Serialize the document to BSON.
  76       *
  77       * @see https://php.net/mongodb-bson-serializable.bsonserialize
  78       * @return object
  79       */
  80      #[ReturnTypeWillChange]
  81      public function bsonSerialize()
  82      {
  83          return (object) $this->getArrayCopy();
  84      }
  85  
  86      /**
  87       * Unserialize the document to BSON.
  88       *
  89       * @see https://php.net/mongodb-bson-unserializable.bsonunserialize
  90       * @param array $data Array data
  91       */
  92      #[ReturnTypeWillChange]
  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 https://php.net/jsonserializable.jsonserialize
 102       * @return object
 103       */
 104      #[ReturnTypeWillChange]
 105      public function jsonSerialize()
 106      {
 107          return (object) $this->getArrayCopy();
 108      }
 109  }