Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 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 single-document insert operation.
  25   */
  26  class InsertOneResult
  27  {
  28      /** @var WriteResult */
  29      private $writeResult;
  30  
  31      /** @var mixed */
  32      private $insertedId;
  33  
  34      /** @var boolean */
  35      private $isAcknowledged;
  36  
  37      /**
  38       * @param WriteResult $writeResult
  39       * @param mixed       $insertedId
  40       */
  41      public function __construct(WriteResult $writeResult, $insertedId)
  42      {
  43          $this->writeResult = $writeResult;
  44          $this->insertedId = $insertedId;
  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 InsertOneResult::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 the inserted document's ID.
  68       *
  69       * If the document had an ID prior to inserting (i.e. the driver did not
  70       * need to generate an ID), this will contain its "_id". Any
  71       * driver-generated ID will be a MongoDB\BSON\ObjectId instance.
  72       *
  73       * @return mixed
  74       */
  75      public function getInsertedId()
  76      {
  77          return $this->insertedId;
  78      }
  79  
  80      /**
  81       * Return whether this insert was acknowledged by the server.
  82       *
  83       * If the insert was not acknowledged, other fields from the WriteResult
  84       * (e.g. insertedCount) will be undefined.
  85       *
  86       * If the insert was not acknowledged, other fields from the WriteResult
  87       * (e.g. insertedCount) will be undefined and their getter methods should
  88       * not be invoked.
  89       *
  90       * @return boolean
  91       */
  92      public function isAcknowledged()
  93      {
  94          return $this->writeResult->isAcknowledged();
  95      }
  96  }