Skip to content

Commit d0c198e

Browse files
committed
Huge review
1 parent 8909c38 commit d0c198e

13 files changed

+106
-125
lines changed

psalm-baseline.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -667,11 +667,6 @@
667667
<code>$options['session']</code>
668668
</MixedAssignment>
669669
</file>
670-
<file src="src/Operation/ListSearchIndexes.php">
671-
<DocblockTypeContradiction occurrences="1">
672-
<code>! is_array($filter) &amp;&amp; ! is_object($filter)</code>
673-
</DocblockTypeContradiction>
674-
</file>
675670
<file src="src/Operation/MapReduce.php">
676671
<DocblockTypeContradiction occurrences="1">
677672
<code>! is_string($out) &amp;&amp; ! is_array($out) &amp;&amp; ! is_object($out)</code>

src/Collection.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace MongoDB;
1919

20+
use Countable;
21+
use Iterator;
2022
use MongoDB\BSON\JavascriptInterface;
2123
use MongoDB\Driver\Cursor;
2224
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
@@ -437,9 +439,8 @@ public function createSearchIndex(array $index, array $options = []): string
437439
*/
438440
public function createSearchIndexes(array $indexes, array $options = []): array
439441
{
440-
$server = select_server($this->manager, $options);
441-
442442
$operation = new CreateSearchIndexes($this->databaseName, $this->collectionName, $indexes);
443+
$server = select_server($this->manager, $options);
443444

444445
return $operation->execute($server);
445446
}
@@ -633,9 +634,8 @@ public function dropIndexes(array $options = [])
633634
*/
634635
public function dropSearchIndex(string $name, array $options = []): void
635636
{
636-
$server = select_server($this->manager, $options);
637-
638637
$operation = new DropSearchIndex($this->databaseName, $this->collectionName, $name);
638+
$server = select_server($this->manager, $options);
639639

640640
$operation->execute($server);
641641
}
@@ -1018,18 +1018,18 @@ public function listIndexes(array $options = [])
10181018
* Returns information for all Atlas Search indexes for the collection.
10191019
* Only available when used against a 7.0+ Atlas cluster.
10201020
*
1021+
* @return Iterator&Countable
10211022
* @throws InvalidArgumentException for parameter/option parsing errors
10221023
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
10231024
* @see ListSearchIndexes::__construct() for supported aggregation and list options
10241025
*/
1025-
public function listSearchIndexes(?string $name = null, array $options = []): Cursor
1026+
public function listSearchIndexes(?string $name = null, array $options = []): Iterator
10261027
{
1027-
$filter = [];
1028-
if ($name) {
1029-
$filter += ['name' => $name];
1028+
if ($name !== null) {
1029+
$options['name'] = $name;
10301030
}
10311031

1032-
$operation = new ListSearchIndexes($this->databaseName, $this->collectionName, $filter, $options);
1032+
$operation = new ListSearchIndexes($this->databaseName, $this->collectionName, $options);
10331033
$server = select_server($this->manager, $options);
10341034

10351035
return $operation->execute($server);

src/Model/SearchIndexInput.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ public function __construct(array $index)
5858
throw InvalidArgumentException::invalidType('"name" option', $index['name'], 'string');
5959
}
6060

61-
if (empty($index['name'])) {
62-
unset($index['name']);
63-
}
64-
6561
$this->index = $index;
6662
}
6763

src/Operation/CreateSearchIndexes.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function __construct(string $databaseName, string $collectionName, array
6767
if (is_object($index)) {
6868
$index = (array) $index;
6969
} elseif (! is_array($index)) {
70-
throw InvalidArgumentException::invalidType(sprintf('$index[%d]', $i), $index, 'array');
70+
throw InvalidArgumentException::invalidType(sprintf('$index[%d]', $i), $index, 'array or object');
7171
}
7272

7373
$this->indexes[] = new SearchIndexInput($index);
@@ -86,29 +86,19 @@ public function __construct(string $databaseName, string $collectionName, array
8686
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
8787
*/
8888
public function execute(Server $server): array
89-
{
90-
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
91-
92-
/** @var object{indexesCreated?: list<object{name: string}>} $result */
93-
$result = current($cursor->toArray());
94-
95-
return array_map(function ($index) {
96-
return $index->name;
97-
}, $result->indexesCreated ?? []);
98-
}
99-
100-
/**
101-
* Create one or more indexes for the collection using the createSearchIndexes command.
102-
*
103-
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
104-
*/
105-
private function createCommand(): Command
10689
{
10790
$cmd = [
10891
'createSearchIndexes' => $this->collectionName,
10992
'indexes' => $this->indexes,
11093
];
11194

112-
return new Command($cmd);
95+
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
96+
97+
/** @var object{indexesCreated: list<object{name: string}>} $result */
98+
$result = current($cursor->toArray());
99+
100+
return array_map(function ($index) {
101+
return $index->name;
102+
}, $result->indexesCreated);
113103
}
114104
}

src/Operation/DropSearchIndex.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
use MongoDB\Exception\InvalidArgumentException;
2424
use MongoDB\Exception\UnsupportedException;
2525

26-
use function current;
27-
2826
/**
2927
* Operation for the dropSearchIndexes command.
3028
*
@@ -40,52 +38,41 @@ class DropSearchIndex implements Executable
4038
private $collectionName;
4139

4240
/** @var string */
43-
private $indexName;
41+
private $name;
4442

4543
/**
4644
* Constructs a dropSearchIndex command.
4745
*
4846
* @param string $databaseName Database name
4947
* @param string $collectionName Collection name
50-
* @param string $indexName Index name
48+
* @param string $name Index name
5149
* @throws InvalidArgumentException for parameter parsing errors
5250
*/
53-
public function __construct(string $databaseName, string $collectionName, string $indexName)
51+
public function __construct(string $databaseName, string $collectionName, string $name)
5452
{
55-
if ($indexName === '') {
56-
throw new InvalidArgumentException('$indexName cannot be empty');
53+
if ($name === '') {
54+
throw new InvalidArgumentException('Index name cannot be empty');
5755
}
5856

5957
$this->databaseName = $databaseName;
6058
$this->collectionName = $collectionName;
61-
$this->indexName = $indexName;
59+
$this->name = $name;
6260
}
6361

6462
/**
6563
* Execute the operation.
6664
*
6765
* @see Executable::execute()
68-
* @return array|object Command result document
6966
* @throws UnsupportedException if write concern is used and unsupported
7067
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
7168
*/
72-
public function execute(Server $server)
73-
{
74-
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
75-
76-
return current($cursor->toArray());
77-
}
78-
79-
/**
80-
* Create the dropIndexes command.
81-
*/
82-
private function createCommand(): Command
69+
public function execute(Server $server): void
8370
{
8471
$cmd = [
8572
'dropSearchIndex' => $this->collectionName,
86-
'name' => $this->indexName,
73+
'name' => $this->name,
8774
];
8875

89-
return new Command($cmd);
76+
$server->executeCommand($this->databaseName, new Command($cmd));
9077
}
9178
}

src/Operation/ListSearchIndexes.php

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@
1717

1818
namespace MongoDB\Operation;
1919

20-
use MongoDB\Driver\Cursor;
20+
use Countable;
21+
use Iterator;
2122
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
2223
use MongoDB\Driver\Server;
2324
use MongoDB\Exception\InvalidArgumentException;
2425
use MongoDB\Exception\UnexpectedValueException;
2526
use MongoDB\Exception\UnsupportedException;
27+
use MongoDB\Model\CachingIterator;
2628

27-
use function assert;
28-
use function is_array;
29-
use function is_object;
29+
use function array_intersect_key;
3030
use function is_string;
31-
use function MongoDB\document_to_array;
3231

3332
/**
3433
* Operation for the listSearchIndexes command.
@@ -44,8 +43,8 @@ class ListSearchIndexes implements Executable
4443
/** @var string */
4544
private $collectionName;
4645

47-
/** @var array|object */
48-
private $filter;
46+
/** @var array */
47+
private $listSearchIndexesOptions;
4948

5049
/** @var array */
5150
private $aggregateOptions;
@@ -56,55 +55,44 @@ class ListSearchIndexes implements Executable
5655
/**
5756
* Constructs an aggregate command for listing Atlas Search indexes
5857
*
59-
* @param string $databaseName Database name
60-
* @param string $collectionName Collection name
61-
* @param array|object $filter Query by which to filter documents
62-
* @param array $options Command options
63-
* @throws InvalidArgumentException for parameter/option parsing errors
58+
* @param string $databaseName Database name
59+
* @param string $collectionName Collection name
60+
* @param array $options Command options
6461
*/
65-
public function __construct(string $databaseName, string $collectionName, $filter, array $options = [])
62+
public function __construct(string $databaseName, string $collectionName, array $options = [])
6663
{
67-
if (! is_array($filter) && ! is_object($filter)) {
68-
throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
69-
}
70-
71-
$filter = document_to_array($filter);
72-
73-
if (isset($filter['name']) && ! is_string($filter['name'])) {
74-
throw InvalidArgumentException::invalidType('"name" filter', $filter['name'], 'string');
64+
if (isset($options['name']) && ! is_string($options['name'])) {
65+
throw InvalidArgumentException::invalidType('"name" option', $options['name'], 'string');
7566
}
7667

7768
$this->databaseName = $databaseName;
7869
$this->collectionName = $collectionName;
79-
$this->filter = $filter;
80-
81-
/* There is currently no additional options for the $listSearchIndexes operation, the $options array can be
82-
* split into two parts: $listIndexesOptions and $aggregateOptions if options are added in the future. */
83-
$this->aggregateOptions = $options;
70+
$this->listSearchIndexesOptions = array_intersect_key($options, ['name' => 1]);
71+
$this->aggregateOptions = array_intersect_key($options, ['batchSize' => 1, 'collation' => 1, 'comment' => 1, 'maxTimeMS' => 1, 'readConcern' => 1, 'readPreference' => 1, 'session' => 1, 'typeMap']);
8472

8573
$this->aggregate = $this->createAggregate();
8674
}
8775

8876
/**
8977
* Execute the operation.
9078
*
79+
* @return Iterator&Countable
9180
* @see Executable::execute()
9281
* @throws UnexpectedValueException if the command response was malformed
9382
* @throws UnsupportedException if collation or read concern is used and unsupported
9483
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
9584
*/
96-
public function execute(Server $server): Cursor
85+
public function execute(Server $server): Iterator
9786
{
9887
$cursor = $this->aggregate->execute($server);
99-
assert($cursor instanceof Cursor);
10088

101-
return $cursor;
89+
return new CachingIterator($cursor);
10290
}
10391

10492
private function createAggregate(): Aggregate
10593
{
10694
$pipeline = [
107-
['$listSearchIndexes' => (object) $this->filter],
95+
['$listSearchIndexes' => (object) $this->listSearchIndexesOptions],
10896
];
10997

11098
return new Aggregate($this->databaseName, $this->collectionName, $pipeline, $this->aggregateOptions);

src/Operation/UpdateSearchIndex.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,6 @@ public function __construct(string $databaseName, string $collectionName, string
8080
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
8181
*/
8282
public function execute(Server $server): void
83-
{
84-
$this->executeCommand($server);
85-
}
86-
87-
/**
88-
* Create one or more indexes for the collection using the createSearchIndexes command.
89-
*
90-
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
91-
*/
92-
private function executeCommand(Server $server): void
9383
{
9484
$cmd = [
9585
'updateSearchIndex' => $this->collectionName,

0 commit comments

Comments
 (0)