20
20
use MongoDB \Codec \DocumentCodec ;
21
21
use MongoDB \Codec \Encoder ;
22
22
use MongoDB \Driver \BulkWriteCommand ;
23
+ use MongoDB \Driver \Manager ;
23
24
use MongoDB \Exception \InvalidArgumentException ;
24
25
25
26
use function is_array ;
30
31
{
31
32
private function __construct (
32
33
public BulkWriteCommand $ bulkWriteCommand ,
34
+ private Manager $ manager ,
33
35
private string $ namespace ,
34
36
private Encoder $ builderEncoder ,
35
37
private ?DocumentCodec $ codec ,
@@ -58,6 +60,7 @@ public static function createWithCollection(Collection $collection, array $optio
58
60
59
61
return new self (
60
62
new BulkWriteCommand ($ options ),
63
+ $ collection ->getManager (),
61
64
$ collection ->getNamespace (),
62
65
$ collection ->getBuilderEncoder (),
63
66
$ collection ->getCodec (),
@@ -66,8 +69,18 @@ public static function createWithCollection(Collection $collection, array $optio
66
69
67
70
public function withCollection (Collection $ collection ): self
68
71
{
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
+
69
81
return new self (
70
82
$ this ->bulkWriteCommand ,
83
+ $ this ->manager ,
71
84
$ collection ->getNamespace (),
72
85
$ collection ->getBuilderEncoder (),
73
86
$ collection ->getCodec (),
0 commit comments