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 array_values;
  27  use function MongoDB\recursive_copy;
  28  
  29  /**
  30   * Model class for a BSON array.
  31   *
  32   * The internal data will be filtered through array_values() during BSON
  33   * serialization to ensure that it becomes a BSON array.
  34   *
  35   * @api
  36   */
  37  class BSONArray extends ArrayObject implements JsonSerializable, Serializable, Unserializable
  38  {
  39      /**
  40       * Clone this BSONArray.
  41       */
  42      public function __clone()
  43      {
  44          foreach ($this as $key => $value) {
  45              $this[$key] = recursive_copy($value);
  46          }
  47      }
  48  
  49      /**
  50       * Factory method for var_export().
  51       *
  52       * @see https://php.net/oop5.magic#object.set-state
  53       * @see https://php.net/var-export
  54       * @return self
  55       */
  56      public static function __set_state(array $properties)
  57      {
  58          $array = new static();
  59          $array->exchangeArray($properties);
  60  
  61          return $array;
  62      }
  63  
  64      /**
  65       * Serialize the array to BSON.
  66       *
  67       * The array data will be numerically reindexed to ensure that it is stored
  68       * as a BSON array.
  69       *
  70       * @see https://php.net/mongodb-bson-serializable.bsonserialize
  71       * @return array
  72       */
  73      #[ReturnTypeWillChange]
  74      public function bsonSerialize()
  75      {
  76          return array_values($this->getArrayCopy());
  77      }
  78  
  79      /**
  80       * Unserialize the document to BSON.
  81       *
  82       * @see https://php.net/mongodb-bson-unserializable.bsonunserialize
  83       * @param array $data Array data
  84       */
  85      #[ReturnTypeWillChange]
  86      public function bsonUnserialize(array $data)
  87      {
  88          self::__construct($data);
  89      }
  90  
  91      /**
  92       * Serialize the array to JSON.
  93       *
  94       * The array data will be numerically reindexed to ensure that it is stored
  95       * as a JSON array.
  96       *
  97       * @see https://php.net/jsonserializable.jsonserialize
  98       * @return array
  99       */
 100      #[ReturnTypeWillChange]
 101      public function jsonSerialize()
 102      {
 103          return array_values($this->getArrayCopy());
 104      }
 105  }