2020use MongoDB \Codec \DocumentCodec ;
2121use MongoDB \Codec \Encoder ;
2222use MongoDB \Driver \BulkWriteCommand ;
23+ use MongoDB \Driver \Manager ;
2324use MongoDB \Exception \InvalidArgumentException ;
2425
2526use function is_array ;
3031{
3132 private function __construct (
3233 public BulkWriteCommand $ bulkWriteCommand ,
34+ private Manager $ manager ,
3335 private string $ namespace ,
3436 private Encoder $ builderEncoder ,
3537 private ?DocumentCodec $ codec ,
@@ -58,6 +60,7 @@ public static function createWithCollection(Collection $collection, array $optio
5860
5961 return new self (
6062 new BulkWriteCommand ($ options ),
63+ $ collection ->getManager (),
6164 $ collection ->getNamespace (),
6265 $ collection ->getBuilderEncoder (),
6366 $ collection ->getCodec (),
@@ -66,8 +69,18 @@ public static function createWithCollection(Collection $collection, array $optio
6669
6770 public function withCollection (Collection $ collection ): self
6871 {
72+ /* Prohibit mixing Collections associated with different Manager
73+ * objects. This is not technically necessary, since the Collection is
74+ * only used to derive a namespace and encoding options; however, it
75+ * may prevent a user from inadvertently mixing writes destined for
76+ * different deployments. */
77+ if ($ this ->manager !== $ collection ->getManager ()) {
78+ throw new InvalidArgumentException ('$collection is associated with a different MongoDB\Driver\Manager ' );
79+ }
80+
6981 return new self (
7082 $ this ->bulkWriteCommand ,
83+ $ this ->manager ,
7184 $ collection ->getNamespace (),
7285 $ collection ->getBuilderEncoder (),
7386 $ collection ->getCodec (),
0 commit comments