Skip to content

Commit c2695fd

Browse files
committed
Throw an UnsupportedValueException instead of skipping the codec for array input
1 parent af23cb2 commit c2695fd

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

src/Operation/BulkWrite.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use MongoDB\Driver\WriteConcern;
2929
use MongoDB\Exception\InvalidArgumentException;
3030
use MongoDB\Exception\UnsupportedException;
31+
use MongoDB\Exception\UnsupportedValueException;
3132

3233
use function array_is_list;
3334
use function array_key_exists;
@@ -306,7 +307,11 @@ private function validateOperations(array $operations, ?DocumentCodec $codec, En
306307
case self::INSERT_ONE:
307308
// $args[0] was already validated above. Since DocumentCodec::encode will always return a Document
308309
// instance, there is no need to re-validate the returned value here.
309-
if ($codec && is_object($args[0])) {
310+
if ($codec) {
311+
if (! is_object($args[0])) {
312+
throw UnsupportedValueException::invalidEncodableValue($args[0]);
313+
}
314+
310315
$operations[$i][$type][0] = $codec->encode($args[0]);
311316
}
312317

@@ -341,7 +346,11 @@ private function validateOperations(array $operations, ?DocumentCodec $codec, En
341346
throw new InvalidArgumentException(sprintf('Missing second argument for $operations[%d]["%s"]', $i, $type));
342347
}
343348

344-
if ($codec && is_object($args[1])) {
349+
if ($codec) {
350+
if (! is_object($args[1])) {
351+
throw UnsupportedValueException::invalidEncodableValue($args[1]);
352+
}
353+
345354
$operations[$i][$type][1] = $codec->encode($args[1]);
346355
}
347356

src/Operation/FindOneAndReplace.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use MongoDB\Driver\Server;
2323
use MongoDB\Exception\InvalidArgumentException;
2424
use MongoDB\Exception\UnsupportedException;
25+
use MongoDB\Exception\UnsupportedValueException;
2526

2627
use function array_key_exists;
2728
use function is_integer;
@@ -172,6 +173,10 @@ public function getCommandDocument(): array
172173
private function validateReplacement(array|object $replacement, ?DocumentCodec $codec): array|object
173174
{
174175
if ($codec && is_object($replacement)) {
176+
if (! is_object($replacement)) {
177+
throw UnsupportedValueException::invalidEncodableValue($replacement);
178+
}
179+
175180
$replacement = $codec->encode($replacement);
176181
} elseif (! is_document($replacement)) {
177182
throw InvalidArgumentException::expectedDocumentType('$replacement', $replacement);

src/Operation/InsertMany.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use MongoDB\Driver\WriteConcern;
2626
use MongoDB\Exception\InvalidArgumentException;
2727
use MongoDB\Exception\UnsupportedException;
28+
use MongoDB\Exception\UnsupportedValueException;
2829
use MongoDB\InsertManyResult;
2930

3031
use function array_is_list;
@@ -190,7 +191,11 @@ private function validateDocuments(array $documents, ?DocumentCodec $codec): arr
190191
}
191192

192193
foreach ($documents as $i => $document) {
193-
if ($codec && is_object($document)) {
194+
if ($codec) {
195+
if (! is_object($document)) {
196+
throw UnsupportedValueException::invalidEncodableValue($document);
197+
}
198+
194199
$documents[$i] = $codec->encode($document);
195200
} elseif (! is_document($document)) {
196201
throw InvalidArgumentException::expectedDocumentType(sprintf('$documents[%d]', $i), $document);

src/Operation/InsertOne.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use MongoDB\Driver\WriteConcern;
2626
use MongoDB\Exception\InvalidArgumentException;
2727
use MongoDB\Exception\UnsupportedException;
28+
use MongoDB\Exception\UnsupportedValueException;
2829
use MongoDB\InsertOneResult;
2930

3031
use function is_bool;
@@ -157,7 +158,11 @@ private function createExecuteOptions(): array
157158

158159
private function validateDocument(array|object $document, ?DocumentCodec $codec): array|object
159160
{
160-
if ($codec && is_object($document)) {
161+
if ($codec) {
162+
if (! is_object($document)) {
163+
throw UnsupportedValueException::invalidEncodableValue($document);
164+
}
165+
161166
$document = $codec->encode($document);
162167
} elseif (! is_document($document)) {
163168
throw InvalidArgumentException::expectedDocumentType('$document', $document);

src/Operation/ReplaceOne.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use MongoDB\Driver\Server;
2323
use MongoDB\Exception\InvalidArgumentException;
2424
use MongoDB\Exception\UnsupportedException;
25+
use MongoDB\Exception\UnsupportedValueException;
2526
use MongoDB\UpdateResult;
2627

2728
use function is_object;
@@ -120,7 +121,11 @@ public function execute(Server $server): UpdateResult
120121

121122
private function validateReplacement(array|object $replacement, ?DocumentCodec $codec): array|object
122123
{
123-
if ($codec && is_object($replacement)) {
124+
if ($codec) {
125+
if (! is_object($replacement)) {
126+
throw UnsupportedValueException::invalidEncodableValue($replacement);
127+
}
128+
124129
$replacement = $codec->encode($replacement);
125130
} elseif (! is_document($replacement)) {
126131
throw InvalidArgumentException::expectedDocumentType('$replacement', $replacement);

0 commit comments

Comments
 (0)