55use MongoDB \Codec \DocumentCodec ;
66use MongoDB \Codec \Encoder ;
77use MongoDB \Driver \BulkWriteCommand ;
8+ use MongoDB \Driver \Manager ;
89use MongoDB \Exception \InvalidArgumentException ;
910
1011use function is_array ;
1516{
1617 private function __construct (
1718 public BulkWriteCommand $ bulkWriteCommand ,
19+ private Manager $ manager ,
1820 private string $ namespace ,
1921 private Encoder $ builderEncoder ,
2022 private ?DocumentCodec $ codec ,
@@ -43,6 +45,7 @@ public static function createWithCollection(Collection $collection, array $optio
4345
4446 return new self (
4547 new BulkWriteCommand ($ options ),
48+ $ collection ->getManager (),
4649 $ collection ->getNamespace (),
4750 $ collection ->getBuilderEncoder (),
4851 $ collection ->getCodec (),
@@ -51,8 +54,18 @@ public static function createWithCollection(Collection $collection, array $optio
5154
5255 public function withCollection (Collection $ collection ): self
5356 {
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+
5466 return new self (
5567 $ this ->bulkWriteCommand ,
68+ $ this ->manager ,
5669 $ collection ->getNamespace (),
5770 $ collection ->getBuilderEncoder (),
5871 $ collection ->getCodec (),
0 commit comments