diff --git a/psalm-baseline.xml b/psalm-baseline.xml
index 97bdba8f9..d61391fef 100644
--- a/psalm-baseline.xml
+++ b/psalm-baseline.xml
@@ -288,8 +288,7 @@
-
- ! is_array($index['definition']) && ! is_object($index['definition'])
+
isset($index['name']) && ! is_string($index['name'])
@@ -444,11 +443,8 @@
-
- is_array($index)
-
- $index
+ (array) $index
@@ -715,11 +711,6 @@
! is_array($update) && ! is_object($update)
-
-
- ! is_array($definition) && ! is_object($definition)
-
-
isset($resumeToken) && ! is_array($resumeToken) && ! is_object($resumeToken)
@@ -785,18 +776,13 @@
$typeMap['fieldPaths'][$fieldPath]
$value
-
- array|object
+
array|object|null
array|object|null
-
- $type
-
-
+
$collectionInfo['options']['encryptedFields'] ?? null
$encryptedFieldsMap[$databaseName . '.' . $collectionName] ?? null
- toPHP(fromPHP($document), $typeMap)
diff --git a/src/Model/SearchIndexInput.php b/src/Model/SearchIndexInput.php
index 616e15f93..a75e0d63c 100644
--- a/src/Model/SearchIndexInput.php
+++ b/src/Model/SearchIndexInput.php
@@ -20,9 +20,8 @@
use MongoDB\BSON\Serializable;
use MongoDB\Exception\InvalidArgumentException;
-use function is_array;
-use function is_object;
use function is_string;
+use function MongoDB\is_document;
/**
* Search index input model class.
@@ -49,8 +48,8 @@ public function __construct(array $index)
throw new InvalidArgumentException('Required "definition" document is missing from search index specification');
}
- if (! is_array($index['definition']) && ! is_object($index['definition'])) {
- throw InvalidArgumentException::invalidType('"definition" option', $index['definition'], 'array or object');
+ if (! is_document($index['definition'])) {
+ throw InvalidArgumentException::expectedDocumentType('"definition" option', $index['definition']);
}
// Name is optional, but must be a non-empty string if provided
diff --git a/src/Operation/CreateSearchIndexes.php b/src/Operation/CreateSearchIndexes.php
index 04325b82c..77980991f 100644
--- a/src/Operation/CreateSearchIndexes.php
+++ b/src/Operation/CreateSearchIndexes.php
@@ -27,8 +27,7 @@
use function array_is_list;
use function array_map;
use function current;
-use function is_array;
-use function is_object;
+use function MongoDB\is_document;
use function sprintf;
/**
@@ -64,13 +63,11 @@ public function __construct(string $databaseName, string $collectionName, array
}
foreach ($indexes as $i => $index) {
- if (is_object($index)) {
- $index = (array) $index;
- } elseif (! is_array($index)) {
- throw InvalidArgumentException::invalidType(sprintf('$index[%d]', $i), $index, 'array or object');
+ if (! is_document($index)) {
+ throw InvalidArgumentException::expectedDocumentType(sprintf('$indexes[%d]', $i), $index);
}
- $this->indexes[] = new SearchIndexInput($index);
+ $this->indexes[] = new SearchIndexInput((array) $index);
}
$this->databaseName = $databaseName;
diff --git a/src/Operation/UpdateSearchIndex.php b/src/Operation/UpdateSearchIndex.php
index 84eb5e383..5a85a69a7 100644
--- a/src/Operation/UpdateSearchIndex.php
+++ b/src/Operation/UpdateSearchIndex.php
@@ -23,8 +23,7 @@
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
-use function is_array;
-use function is_object;
+use function MongoDB\is_document;
/**
* Operation for the createIndexes command.
@@ -61,8 +60,8 @@ public function __construct(string $databaseName, string $collectionName, string
throw new InvalidArgumentException('Index name cannot be empty');
}
- if (! is_array($definition) && ! is_object($definition)) {
- throw InvalidArgumentException::invalidType('$definition', $definition, 'array or object');
+ if (! is_document($definition)) {
+ throw InvalidArgumentException::expectedDocumentType('$definition', $definition);
}
$this->databaseName = $databaseName;
diff --git a/tests/Model/IndexInputTest.php b/tests/Model/IndexInputTest.php
index 7708055cc..e9dad4f89 100644
--- a/tests/Model/IndexInputTest.php
+++ b/tests/Model/IndexInputTest.php
@@ -15,12 +15,14 @@ class IndexInputTest extends TestCase
public function testConstructorShouldRequireKey(): void
{
$this->expectException(InvalidArgumentException::class);
+ $this->expectExceptionMessage('Required "key" document is missing from index specification');
new IndexInput([]);
}
public function testConstructorShouldRequireKeyToBeArrayOrObject(): void
{
$this->expectException(InvalidArgumentException::class);
+ $this->expectExceptionMessage('Expected "key" option to have type "document"');
new IndexInput(['key' => 'foo']);
}
@@ -28,6 +30,7 @@ public function testConstructorShouldRequireKeyToBeArrayOrObject(): void
public function testConstructorShouldRequireKeyFieldOrderToBeNumericOrString($order): void
{
$this->expectException(InvalidArgumentException::class);
+ $this->expectExceptionMessage('Expected order value for "x" field within "key" option to have type "numeric or string"');
new IndexInput(['key' => ['x' => $order]]);
}
@@ -39,6 +42,7 @@ public function provideInvalidFieldOrderValues()
public function testConstructorShouldRequireNameToBeString(): void
{
$this->expectException(InvalidArgumentException::class);
+ $this->expectExceptionMessage('Expected "name" option to have type "string"');
new IndexInput(['key' => ['x' => 1], 'name' => 1]);
}
diff --git a/tests/Model/SearchIndexInputTest.php b/tests/Model/SearchIndexInputTest.php
new file mode 100644
index 000000000..3c6fbe142
--- /dev/null
+++ b/tests/Model/SearchIndexInputTest.php
@@ -0,0 +1,48 @@
+expectException(InvalidArgumentException::class);
+ $this->expectExceptionMessage('Required "definition" document is missing from search index specification');
+ new SearchIndexInput([]);
+ }
+
+ public function testConstructorIndexDefinitionMustBeADocument(): void
+ {
+ $this->expectException(InvalidArgumentException::class);
+ $this->expectExceptionMessage('Expected "definition" option to have type "document"');
+ new SearchIndexInput(['definition' => 'foo']);
+ }
+
+ public function testConstructorShouldRequireNameToBeString(): void
+ {
+ $this->expectException(InvalidArgumentException::class);
+ $this->expectExceptionMessage('Expected "name" option to have type "string"');
+ new SearchIndexInput(['definition' => ['mapping' => ['dynamid' => true]], 'name' => 1]);
+ }
+
+ public function testBsonSerialization(): void
+ {
+ $expected = (object) [
+ 'name' => 'my_search',
+ 'definition' => ['mapping' => ['dynamic' => true]],
+ ];
+
+ $indexInput = new SearchIndexInput([
+ 'name' => 'my_search',
+ 'definition' => ['mapping' => ['dynamic' => true]],
+ ]);
+
+ $this->assertInstanceOf(Serializable::class, $indexInput);
+ $this->assertEquals($expected, $indexInput->bsonSerialize());
+ }
+}
diff --git a/tests/Operation/CreateSearchIndexesTest.php b/tests/Operation/CreateSearchIndexesTest.php
index d8e3b240f..03e6e31ee 100644
--- a/tests/Operation/CreateSearchIndexesTest.php
+++ b/tests/Operation/CreateSearchIndexesTest.php
@@ -18,7 +18,7 @@ public function testConstructorIndexesArgumentMustBeAList(): void
public function testConstructorIndexDefinitionMustBeADocument($index): void
{
$this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Expected $index[0] to have type "array or object"');
+ $this->expectExceptionMessage('Expected $indexes[0] to have type "document"');
new CreateSearchIndexes($this->getDatabaseName(), $this->getCollectionName(), [$index]);
}
@@ -41,7 +41,7 @@ public function testConstructorIndexDefinitionMustBeDefined(): void
public function testConstructorIndexDefinitionMustBeAnArray($definition): void
{
$this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Expected "definition" option to have type "array or object"');
+ $this->expectExceptionMessage('Expected "definition" option to have type "document"');
new CreateSearchIndexes($this->getDatabaseName(), $this->getCollectionName(), [['definition' => $definition]]);
}
diff --git a/tests/Operation/UpdateSearchIndexTest.php b/tests/Operation/UpdateSearchIndexTest.php
index 535f620cc..90c623fc3 100644
--- a/tests/Operation/UpdateSearchIndexTest.php
+++ b/tests/Operation/UpdateSearchIndexTest.php
@@ -17,7 +17,7 @@ public function testConstructorIndexNameMustNotBeEmpty(): void
public function testConstructorIndexDefinitionMustBeADocument($definition): void
{
$this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Expected $definition to have type "array or object"');
+ $this->expectExceptionMessage('Expected $definition to have type "document"');
new UpdateSearchIndex($this->getDatabaseName(), $this->getCollectionName(), 'index name', $definition);
}
}