Skip to content

Commit b2cd286

Browse files
committed
PHPLIB-511: Allow passing multiple types to InvalidArgumentException
1 parent 8567a31 commit b2cd286

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/Exception/InvalidArgumentException.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
namespace MongoDB\Exception;
1919

2020
use MongoDB\Driver\Exception\InvalidArgumentException as DriverInvalidArgumentException;
21+
use function array_pop;
22+
use function count;
2123
use function get_class;
2224
use function gettype;
25+
use function implode;
26+
use function is_array;
2327
use function is_object;
2428
use function sprintf;
2529

@@ -28,13 +32,32 @@ class InvalidArgumentException extends DriverInvalidArgumentException implements
2832
/**
2933
* Thrown when an argument or option has an invalid type.
3034
*
31-
* @param string $name Name of the argument or option
32-
* @param mixed $value Actual value (used to derive the type)
33-
* @param string $expectedType Expected type
35+
* @param string $name Name of the argument or option
36+
* @param mixed $value Actual value (used to derive the type)
37+
* @param string|string[] $expectedType Expected type
3438
* @return self
3539
*/
3640
public static function invalidType($name, $value, $expectedType)
3741
{
42+
if (is_array($expectedType)) {
43+
switch (count($expectedType)) {
44+
case 1:
45+
$typeString = array_pop($expectedType);
46+
break;
47+
48+
case 2:
49+
$typeString = implode('" or "', $expectedType);
50+
break;
51+
52+
default:
53+
$lastType = array_pop($expectedType);
54+
$typeString = sprintf('%s", or "%s', implode('", "', $expectedType), $lastType);
55+
break;
56+
}
57+
58+
$expectedType = $typeString;
59+
}
60+
3861
return new static(sprintf('Expected %s to have type "%s" but found "%s"', $name, $expectedType, is_object($value) ? get_class($value) : gettype($value)));
3962
}
4063
}

0 commit comments

Comments
 (0)