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 2017-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;
  19  
  20  use IteratorAggregate;
  21  use ReturnTypeWillChange;
  22  use stdClass;
  23  use Traversable;
  24  
  25  use function call_user_func;
  26  
  27  /**
  28   * Result class for mapReduce command results.
  29   *
  30   * This class allows for iteration of mapReduce results irrespective of the
  31   * output method (e.g. inline, collection) via the IteratorAggregate interface.
  32   * It also provides access to command statistics.
  33   *
  34   * @api
  35   * @see \MongoDB\Collection::mapReduce()
  36   * @see https://mongodb.com/docs/manual/reference/command/mapReduce/
  37   */
  38  class MapReduceResult implements IteratorAggregate
  39  {
  40      /** @var callable */
  41      private $getIterator;
  42  
  43      /** @var integer */
  44      private $executionTimeMS;
  45  
  46      /** @var array */
  47      private $counts;
  48  
  49      /** @var array */
  50      private $timing;
  51  
  52      /**
  53       * @internal
  54       * @param callable $getIterator Callback that returns a Traversable for mapReduce results
  55       * @param stdClass $result      Result document from the mapReduce command
  56       */
  57      public function __construct(callable $getIterator, stdClass $result)
  58      {
  59          $this->getIterator = $getIterator;
  60          $this->executionTimeMS = isset($result->timeMillis) ? (integer) $result->timeMillis : 0;
  61          $this->counts = isset($result->counts) ? (array) $result->counts : [];
  62          $this->timing = isset($result->timing) ? (array) $result->timing : [];
  63      }
  64  
  65      /**
  66       * Returns various count statistics from the mapReduce command.
  67       *
  68       * @return array
  69       */
  70      public function getCounts()
  71      {
  72          return $this->counts;
  73      }
  74  
  75      /**
  76       * Return the command execution time in milliseconds.
  77       *
  78       * @return integer
  79       */
  80      public function getExecutionTimeMS()
  81      {
  82          return $this->executionTimeMS;
  83      }
  84  
  85      /**
  86       * Return the mapReduce results as a Traversable.
  87       *
  88       * @see https://php.net/iteratoraggregate.getiterator
  89       * @return Traversable
  90       */
  91      #[ReturnTypeWillChange]
  92      public function getIterator()
  93      {
  94          return call_user_func($this->getIterator);
  95      }
  96  
  97      /**
  98       * Returns various timing statistics from the mapReduce command.
  99       *
 100       * Note: timing statistics are only available if the mapReduce command's
 101       * "verbose" option was true; otherwise, an empty array will be returned.
 102       *
 103       * @return array
 104       */
 105      public function getTiming()
 106      {
 107          return $this->timing;
 108      }
 109  }