5
5
use MongoDB \Codec \DocumentCodec ;
6
6
use MongoDB \Codec \Encoder ;
7
7
use MongoDB \Driver \BulkWriteCommand ;
8
+ use MongoDB \Driver \Manager ;
8
9
use MongoDB \Exception \InvalidArgumentException ;
9
10
10
11
use function is_array ;
15
16
{
16
17
private function __construct (
17
18
public BulkWriteCommand $ bulkWriteCommand ,
19
+ private Manager $ manager ,
18
20
private string $ namespace ,
19
21
private Encoder $ builderEncoder ,
20
22
private ?DocumentCodec $ codec ,
@@ -43,6 +45,7 @@ public static function createWithCollection(Collection $collection, array $optio
43
45
44
46
return new self (
45
47
new BulkWriteCommand ($ options ),
48
+ $ collection ->getManager (),
46
49
$ collection ->getNamespace (),
47
50
$ collection ->getBuilderEncoder (),
48
51
$ collection ->getCodec (),
@@ -51,8 +54,18 @@ public static function createWithCollection(Collection $collection, array $optio
51
54
52
55
public function withCollection (Collection $ collection ): self
53
56
{
57
+ /* Prohibit mixing Collections associated with different Manager
58
+ * objects. This is not technically necessary, since the Collection is
59
+ * only used to derive a namespace and encoding options; however, it
60
+ * may prevent a user from inadvertently mixing writes destined for
61
+ * different deployments. */
62
+ if ($ this ->manager !== $ collection ->getManager ()) {
63
+ throw new InvalidArgumentException ('$collection is associated with a different MongoDB\Driver\Manager ' );
64
+ }
65
+
54
66
return new self (
55
67
$ this ->bulkWriteCommand ,
68
+ $ this ->manager ,
56
69
$ collection ->getNamespace (),
57
70
$ collection ->getBuilderEncoder (),
58
71
$ collection ->getCodec (),
0 commit comments