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 311] [Versions 310 and 400] [Versions 310 and 401]

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