|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB; |
| 4 | + |
| 5 | +use BSON\ObjectId; |
| 6 | +use MongoDB\Driver\WriteResult; |
| 7 | + |
| 8 | +/** |
| 9 | + * Result class for a bulk write operation. |
| 10 | + */ |
| 11 | +class BulkWriteResult |
| 12 | +{ |
| 13 | + private $writeResult; |
| 14 | + private $insertedIds; |
| 15 | + |
| 16 | + /** |
| 17 | + * Constructor. |
| 18 | + * |
| 19 | + * @param WriteResult $writeResult |
| 20 | + * @param mixed[] $insertedIds |
| 21 | + */ |
| 22 | + public function __construct(WriteResult $writeResult, array $insertedIds) |
| 23 | + { |
| 24 | + $this->writeResult = $writeResult; |
| 25 | + $this->insertedIds = $insertedIds; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Return the number of documents that were deleted. |
| 30 | + * |
| 31 | + * This value is undefined if the write was not acknowledged. |
| 32 | + * |
| 33 | + * @see BulkWriteResult::isAcknowledged() |
| 34 | + * @return integer |
| 35 | + */ |
| 36 | + public function getDeletedCount() |
| 37 | + { |
| 38 | + return $this->writeResult->getDeletedCount(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Return the number of documents that were inserted. |
| 43 | + * |
| 44 | + * This value is undefined if the write was not acknowledged. |
| 45 | + * |
| 46 | + * @see BulkWriteResult::isAcknowledged() |
| 47 | + * @return integer |
| 48 | + */ |
| 49 | + public function getInsertedCount() |
| 50 | + { |
| 51 | + return $this->writeResult->getInsertedCount(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Return a map of the inserted documents' IDs. |
| 56 | + * |
| 57 | + * The index of each ID in the map corresponds to the document's position |
| 58 | + * in bulk operation. If the document had an ID prior to insertion (i.e. the |
| 59 | + * driver did not generate an ID), this will contain its "_id" field value. |
| 60 | + * Any driver-generated ID will be an MongoDB\Driver\ObjectID instance. |
| 61 | + * |
| 62 | + * @return mixed[] |
| 63 | + */ |
| 64 | + public function getInsertedIds() |
| 65 | + { |
| 66 | + return $this->insertedIds; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Return the number of documents that were matched by the filter. |
| 71 | + * |
| 72 | + * This value is undefined if the write was not acknowledged. |
| 73 | + * |
| 74 | + * @see BulkWriteResult::isAcknowledged() |
| 75 | + * @return integer |
| 76 | + */ |
| 77 | + public function getMatchedCount() |
| 78 | + { |
| 79 | + return $this->writeResult->getMatchedCount(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Return the number of documents that were modified. |
| 84 | + * |
| 85 | + * This value is undefined if the write was not acknowledged or if the write |
| 86 | + * executed as a legacy operation instead of write command. |
| 87 | + * |
| 88 | + * @see BulkWriteResult::isAcknowledged() |
| 89 | + * @return integer|null |
| 90 | + */ |
| 91 | + public function getModifiedCount() |
| 92 | + { |
| 93 | + return $this->writeResult->getModifiedCount(); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Return the number of documents that were upserted. |
| 98 | + * |
| 99 | + * This value is undefined if the write was not acknowledged. |
| 100 | + * |
| 101 | + * @see BulkWriteResult::isAcknowledged() |
| 102 | + * @return integer |
| 103 | + */ |
| 104 | + public function getUpsertedCount() |
| 105 | + { |
| 106 | + return $this->writeResult->getUpsertedCount(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Return a map of the upserted documents' IDs. |
| 111 | + * |
| 112 | + * The index of each ID in the map corresponds to the document's position |
| 113 | + * in bulk operation. If the document had an ID prior to upserting (i.e. the |
| 114 | + * server did not need to generate an ID), this will contain its "_id". Any |
| 115 | + * server-generated ID will be an MongoDB\Driver\ObjectID instance. |
| 116 | + * |
| 117 | + * @return mixed[] |
| 118 | + */ |
| 119 | + public function getUpsertedIds() |
| 120 | + { |
| 121 | + return $this->writeResult->getUpsertedIds(); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Return whether this update was acknowledged by the server. |
| 126 | + * |
| 127 | + * If the update was not acknowledged, other fields from the WriteResult |
| 128 | + * (e.g. matchedCount) will be undefined. |
| 129 | + * |
| 130 | + * @return boolean |
| 131 | + */ |
| 132 | + public function isAcknowledged() |
| 133 | + { |
| 134 | + return $this->writeResult->isAcknowledged(); |
| 135 | + } |
| 136 | +} |
0 commit comments