Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401]

   1  <?php
   2  /*
   3   * Copyright 2015-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 MongoDB\Driver\WriteResult;
  21  use MongoDB\Exception\BadMethodCallException;
  22  
  23  /**
  24   * Result class for a multi-document insert operation.
  25   */
  26  class InsertManyResult
  27  {
  28      /** @var WriteResult */
  29      private $writeResult;
  30  
  31      /** @var mixed[] */
  32      private $insertedIds;
  33  
  34      /** @var boolean */
  35      private $isAcknowledged;
  36  
  37      /**
  38       * @param WriteResult $writeResult
  39       * @param mixed[]     $insertedIds
  40       */
  41      public function __construct(WriteResult $writeResult, array $insertedIds)
  42      {
  43          $this->writeResult = $writeResult;
  44          $this->insertedIds = $insertedIds;
  45          $this->isAcknowledged = $writeResult->isAcknowledged();
  46      }
  47  
  48      /**
  49       * Return the number of documents that were inserted.
  50       *
  51       * This method should only be called if the write was acknowledged.
  52       *
  53       * @see InsertManyResult::isAcknowledged()
  54       * @return integer
  55       * @throws BadMethodCallException is the write result is unacknowledged
  56       */
  57      public function getInsertedCount()
  58      {
  59          if ($this->isAcknowledged) {
  60              return $this->writeResult->getInsertedCount();
  61          }
  62  
  63          throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
  64      }
  65  
  66      /**
  67       * Return a map of the inserted documents' IDs.
  68       *
  69       * The index of each ID in the map corresponds to each document's position
  70       * in the bulk operation. If a document had an ID prior to inserting (i.e.
  71       * the driver did not generate an ID), the index will contain its "_id"
  72       * field value. Any driver-generated ID will be a MongoDB\BSON\ObjectId
  73       * instance.
  74       *
  75       * @return mixed[]
  76       */
  77      public function getInsertedIds()
  78      {
  79          return $this->insertedIds;
  80      }
  81  
  82      /**
  83       * Return whether this insert result was acknowledged by the server.
  84       *
  85       * If the insert was not acknowledged, other fields from the WriteResult
  86       * (e.g. insertedCount) will be undefined.
  87       *
  88       * @return boolean
  89       */
  90      public function isAcknowledged()
  91      {
  92          return $this->writeResult->isAcknowledged();
  93      }
  94  }