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 2018-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 Iterator;
  21  use MongoDB\Exception\InvalidArgumentException;
  22  use MongoDB\Exception\UnexpectedValueException;
  23  use ReturnTypeWillChange;
  24  
  25  use function is_array;
  26  use function MongoDB\BSON\toPHP;
  27  use function sprintf;
  28  use function strlen;
  29  use function substr;
  30  use function unpack;
  31  
  32  /**
  33   * Iterator for BSON documents.
  34   */
  35  class BSONIterator implements Iterator
  36  {
  37      /** @var integer */
  38      private static $bsonSize = 4;
  39  
  40      /** @var string */
  41      private $buffer;
  42  
  43      /** @var integer */
  44      private $bufferLength;
  45  
  46      /** @var mixed */
  47      private $current;
  48  
  49      /** @var integer */
  50      private $key = 0;
  51  
  52      /** @var integer */
  53      private $position = 0;
  54  
  55      /** @var array */
  56      private $options;
  57  
  58      /**
  59       * Constructs a BSON Iterator.
  60       *
  61       * Supported options:
  62       *
  63       *  * typeMap (array): Type map for BSON deserialization.
  64       *
  65       * @internal
  66       * @see https://php.net/manual/en/function.mongodb.bson-tophp.php
  67       * @param string $data    Concatenated, valid, BSON-encoded documents
  68       * @param array  $options Iterator options
  69       * @throws InvalidArgumentException for parameter/option parsing errors
  70       */
  71      public function __construct(string $data, array $options = [])
  72      {
  73          if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
  74              throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
  75          }
  76  
  77          if (! isset($options['typeMap'])) {
  78              $options['typeMap'] = [];
  79          }
  80  
  81          $this->buffer = $data;
  82          $this->bufferLength = strlen($data);
  83          $this->options = $options;
  84      }
  85  
  86      /**
  87       * @see https://php.net/iterator.current
  88       * @return mixed
  89       */
  90      #[ReturnTypeWillChange]
  91      public function current()
  92      {
  93          return $this->current;
  94      }
  95  
  96      /**
  97       * @see https://php.net/iterator.key
  98       * @return mixed
  99       */
 100      #[ReturnTypeWillChange]
 101      public function key()
 102      {
 103          return $this->key;
 104      }
 105  
 106      /**
 107       * @see https://php.net/iterator.next
 108       * @return void
 109       */
 110      #[ReturnTypeWillChange]
 111      public function next()
 112      {
 113          $this->key++;
 114          $this->current = null;
 115          $this->advance();
 116      }
 117  
 118      /**
 119       * @see https://php.net/iterator.rewind
 120       * @return void
 121       */
 122      #[ReturnTypeWillChange]
 123      public function rewind()
 124      {
 125          $this->key = 0;
 126          $this->position = 0;
 127          $this->current = null;
 128          $this->advance();
 129      }
 130  
 131      /**
 132       * @see https://php.net/iterator.valid
 133       */
 134      #[ReturnTypeWillChange]
 135      public function valid(): bool
 136      {
 137          return $this->current !== null;
 138      }
 139  
 140      private function advance(): void
 141      {
 142          if ($this->position === $this->bufferLength) {
 143              return;
 144          }
 145  
 146          if ($this->bufferLength - $this->position < self::$bsonSize) {
 147              throw new UnexpectedValueException(sprintf('Expected at least %d bytes; %d remaining', self::$bsonSize, $this->bufferLength - $this->position));
 148          }
 149  
 150          [, $documentLength] = unpack('V', substr($this->buffer, $this->position, self::$bsonSize));
 151  
 152          if ($this->bufferLength - $this->position < $documentLength) {
 153              throw new UnexpectedValueException(sprintf('Expected %d bytes; %d remaining', $documentLength, $this->bufferLength - $this->position));
 154          }
 155  
 156          $this->current = toPHP(substr($this->buffer, $this->position, $documentLength), $this->options['typeMap']);
 157          $this->position += $documentLength;
 158      }
 159  }