22
33namespace MongoDB ;
44
5- use MongoDB \BSON \ObjectId ;
65use MongoDB \Driver \WriteResult ;
6+ use MongoDB \Exception \BadMethodCallException ;
77
88/**
99 * Result class for a bulk write operation.
@@ -12,6 +12,7 @@ class BulkWriteResult
1212{
1313 private $ writeResult ;
1414 private $ insertedIds ;
15+ private $ isAcknowledged ;
1516
1617 /**
1718 * Constructor.
@@ -23,41 +24,53 @@ public function __construct(WriteResult $writeResult, array $insertedIds)
2324 {
2425 $ this ->writeResult = $ writeResult ;
2526 $ this ->insertedIds = $ insertedIds ;
27+ $ this ->isAcknowledged = $ writeResult ->isAcknowledged ();
2628 }
2729
2830 /**
2931 * Return the number of documents that were deleted.
3032 *
31- * This value is undefined if the write was not acknowledged.
33+ * This method should only be called if the write was acknowledged.
3234 *
3335 * @see BulkWriteResult::isAcknowledged()
3436 * @return integer
37+ * @throws BadMethodCallException is the write result is unacknowledged
3538 */
3639 public function getDeletedCount ()
3740 {
38- return $ this ->writeResult ->getDeletedCount ();
41+ if ($ this ->isAcknowledged ) {
42+ return $ this ->writeResult ->getDeletedCount ();
43+ }
44+
45+ throw BadMethodCallException::unacknowledgedWriteResultAccess (__METHOD__ );
3946 }
4047
4148 /**
4249 * Return the number of documents that were inserted.
4350 *
44- * This value is undefined if the write was not acknowledged.
51+ * This method should only be called if the write was acknowledged.
4552 *
4653 * @see BulkWriteResult::isAcknowledged()
4754 * @return integer
55+ * @throws BadMethodCallException is the write result is unacknowledged
4856 */
4957 public function getInsertedCount ()
5058 {
51- return $ this ->writeResult ->getInsertedCount ();
59+ if ($ this ->isAcknowledged ) {
60+ return $ this ->writeResult ->getInsertedCount ();
61+ }
62+
63+ throw BadMethodCallException::unacknowledgedWriteResultAccess (__METHOD__ );
5264 }
5365
5466 /**
5567 * Return a map of the inserted documents' IDs.
5668 *
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.
69+ * The index of each ID in the map corresponds to the document's position in
70+ * the bulk operation. If the document had an ID prior to insertion (i.e.
71+ * the driver did not generate an ID), this will contain its "_id" field
72+ * value. Any driver-generated ID will be an MongoDB\Driver\ObjectID
73+ * instance.
6174 *
6275 * @return mixed[]
6376 */
@@ -69,41 +82,58 @@ public function getInsertedIds()
6982 /**
7083 * Return the number of documents that were matched by the filter.
7184 *
72- * This value is undefined if the write was not acknowledged.
85+ * This method should only be called if the write was acknowledged.
7386 *
7487 * @see BulkWriteResult::isAcknowledged()
7588 * @return integer
89+ * @throws BadMethodCallException is the write result is unacknowledged
7690 */
7791 public function getMatchedCount ()
7892 {
79- return $ this ->writeResult ->getMatchedCount ();
93+ if ($ this ->isAcknowledged ) {
94+ return $ this ->writeResult ->getMatchedCount ();
95+ }
96+
97+ throw BadMethodCallException::unacknowledgedWriteResultAccess (__METHOD__ );
8098 }
8199
82100 /**
83101 * Return the number of documents that were modified.
84102 *
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.
103+ * This value is undefined (i.e. null) if the write executed as a legacy
104+ * operation instead of command.
105+ *
106+ * This method should only be called if the write was acknowledged.
87107 *
88108 * @see BulkWriteResult::isAcknowledged()
89109 * @return integer|null
110+ * @throws BadMethodCallException is the write result is unacknowledged
90111 */
91112 public function getModifiedCount ()
92113 {
93- return $ this ->writeResult ->getModifiedCount ();
114+ if ($ this ->isAcknowledged ) {
115+ return $ this ->writeResult ->getModifiedCount ();
116+ }
117+
118+ throw BadMethodCallException::unacknowledgedWriteResultAccess (__METHOD__ );
94119 }
95120
96121 /**
97122 * Return the number of documents that were upserted.
98123 *
99- * This value is undefined if the write was not acknowledged.
124+ * This method should only be called if the write was acknowledged.
100125 *
101126 * @see BulkWriteResult::isAcknowledged()
102127 * @return integer
128+ * @throws BadMethodCallException is the write result is unacknowledged
103129 */
104130 public function getUpsertedCount ()
105131 {
106- return $ this ->writeResult ->getUpsertedCount ();
132+ if ($ this ->isAcknowledged ) {
133+ return $ this ->writeResult ->getUpsertedCount ();
134+ }
135+
136+ throw BadMethodCallException::unacknowledgedWriteResultAccess (__METHOD__ );
107137 }
108138
109139 /**
@@ -114,11 +144,19 @@ public function getUpsertedCount()
114144 * server did not need to generate an ID), this will contain its "_id". Any
115145 * server-generated ID will be an MongoDB\Driver\ObjectID instance.
116146 *
147+ * This method should only be called if the write was acknowledged.
148+ *
149+ * @see BulkWriteResult::isAcknowledged()
117150 * @return mixed[]
151+ * @throws BadMethodCallException is the write result is unacknowledged
118152 */
119153 public function getUpsertedIds ()
120154 {
121- return $ this ->writeResult ->getUpsertedIds ();
155+ if ($ this ->isAcknowledged ) {
156+ return $ this ->writeResult ->getUpsertedIds ();
157+ }
158+
159+ throw BadMethodCallException::unacknowledgedWriteResultAccess (__METHOD__ );
122160 }
123161
124162 /**
@@ -131,6 +169,6 @@ public function getUpsertedIds()
131169 */
132170 public function isAcknowledged ()
133171 {
134- return $ this ->writeResult -> isAcknowledged () ;
172+ return $ this ->isAcknowledged ;
135173 }
136174}
0 commit comments