2
2
3
3
namespace MongoDB ;
4
4
5
- use MongoDB \BSON \ObjectId ;
6
5
use MongoDB \Driver \WriteResult ;
6
+ use MongoDB \Exception \BadMethodCallException ;
7
7
8
8
/**
9
9
* Result class for a bulk write operation.
@@ -12,6 +12,7 @@ class BulkWriteResult
12
12
{
13
13
private $ writeResult ;
14
14
private $ insertedIds ;
15
+ private $ isAcknowledged ;
15
16
16
17
/**
17
18
* Constructor.
@@ -23,41 +24,53 @@ public function __construct(WriteResult $writeResult, array $insertedIds)
23
24
{
24
25
$ this ->writeResult = $ writeResult ;
25
26
$ this ->insertedIds = $ insertedIds ;
27
+ $ this ->isAcknowledged = $ writeResult ->isAcknowledged ();
26
28
}
27
29
28
30
/**
29
31
* Return the number of documents that were deleted.
30
32
*
31
- * This value is undefined if the write was not acknowledged.
33
+ * This method should only be called if the write was acknowledged.
32
34
*
33
35
* @see BulkWriteResult::isAcknowledged()
34
36
* @return integer
37
+ * @throws BadMethodCallException is the write result is unacknowledged
35
38
*/
36
39
public function getDeletedCount ()
37
40
{
38
- return $ this ->writeResult ->getDeletedCount ();
41
+ if ($ this ->isAcknowledged ) {
42
+ return $ this ->writeResult ->getDeletedCount ();
43
+ }
44
+
45
+ throw BadMethodCallException::unacknowledgedWriteResultAccess (__METHOD__ );
39
46
}
40
47
41
48
/**
42
49
* Return the number of documents that were inserted.
43
50
*
44
- * This value is undefined if the write was not acknowledged.
51
+ * This method should only be called if the write was acknowledged.
45
52
*
46
53
* @see BulkWriteResult::isAcknowledged()
47
54
* @return integer
55
+ * @throws BadMethodCallException is the write result is unacknowledged
48
56
*/
49
57
public function getInsertedCount ()
50
58
{
51
- return $ this ->writeResult ->getInsertedCount ();
59
+ if ($ this ->isAcknowledged ) {
60
+ return $ this ->writeResult ->getInsertedCount ();
61
+ }
62
+
63
+ throw BadMethodCallException::unacknowledgedWriteResultAccess (__METHOD__ );
52
64
}
53
65
54
66
/**
55
67
* Return a map of the inserted documents' IDs.
56
68
*
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.
61
74
*
62
75
* @return mixed[]
63
76
*/
@@ -69,41 +82,58 @@ public function getInsertedIds()
69
82
/**
70
83
* Return the number of documents that were matched by the filter.
71
84
*
72
- * This value is undefined if the write was not acknowledged.
85
+ * This method should only be called if the write was acknowledged.
73
86
*
74
87
* @see BulkWriteResult::isAcknowledged()
75
88
* @return integer
89
+ * @throws BadMethodCallException is the write result is unacknowledged
76
90
*/
77
91
public function getMatchedCount ()
78
92
{
79
- return $ this ->writeResult ->getMatchedCount ();
93
+ if ($ this ->isAcknowledged ) {
94
+ return $ this ->writeResult ->getMatchedCount ();
95
+ }
96
+
97
+ throw BadMethodCallException::unacknowledgedWriteResultAccess (__METHOD__ );
80
98
}
81
99
82
100
/**
83
101
* Return the number of documents that were modified.
84
102
*
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.
87
107
*
88
108
* @see BulkWriteResult::isAcknowledged()
89
109
* @return integer|null
110
+ * @throws BadMethodCallException is the write result is unacknowledged
90
111
*/
91
112
public function getModifiedCount ()
92
113
{
93
- return $ this ->writeResult ->getModifiedCount ();
114
+ if ($ this ->isAcknowledged ) {
115
+ return $ this ->writeResult ->getModifiedCount ();
116
+ }
117
+
118
+ throw BadMethodCallException::unacknowledgedWriteResultAccess (__METHOD__ );
94
119
}
95
120
96
121
/**
97
122
* Return the number of documents that were upserted.
98
123
*
99
- * This value is undefined if the write was not acknowledged.
124
+ * This method should only be called if the write was acknowledged.
100
125
*
101
126
* @see BulkWriteResult::isAcknowledged()
102
127
* @return integer
128
+ * @throws BadMethodCallException is the write result is unacknowledged
103
129
*/
104
130
public function getUpsertedCount ()
105
131
{
106
- return $ this ->writeResult ->getUpsertedCount ();
132
+ if ($ this ->isAcknowledged ) {
133
+ return $ this ->writeResult ->getUpsertedCount ();
134
+ }
135
+
136
+ throw BadMethodCallException::unacknowledgedWriteResultAccess (__METHOD__ );
107
137
}
108
138
109
139
/**
@@ -114,11 +144,19 @@ public function getUpsertedCount()
114
144
* server did not need to generate an ID), this will contain its "_id". Any
115
145
* server-generated ID will be an MongoDB\Driver\ObjectID instance.
116
146
*
147
+ * This method should only be called if the write was acknowledged.
148
+ *
149
+ * @see BulkWriteResult::isAcknowledged()
117
150
* @return mixed[]
151
+ * @throws BadMethodCallException is the write result is unacknowledged
118
152
*/
119
153
public function getUpsertedIds ()
120
154
{
121
- return $ this ->writeResult ->getUpsertedIds ();
155
+ if ($ this ->isAcknowledged ) {
156
+ return $ this ->writeResult ->getUpsertedIds ();
157
+ }
158
+
159
+ throw BadMethodCallException::unacknowledgedWriteResultAccess (__METHOD__ );
122
160
}
123
161
124
162
/**
@@ -131,6 +169,6 @@ public function getUpsertedIds()
131
169
*/
132
170
public function isAcknowledged ()
133
171
{
134
- return $ this ->writeResult -> isAcknowledged () ;
172
+ return $ this ->isAcknowledged ;
135
173
}
136
174
}
0 commit comments