diff --git a/src/DocumentStore.php b/src/DocumentStore.php index 20a6e56..2cddfc1 100644 --- a/src/DocumentStore.php +++ b/src/DocumentStore.php @@ -143,4 +143,12 @@ public function filterDocs(string $collectionName, Filter $filter, int $skip = n * @return array */ public function filterDocIds(string $collectionName, Filter $filter): array; + + /** + * @param string $collectionName + * @param Filter $filter + * @return int The number of documents + * @throws UnknownCollection + */ + public function countDocs(string $collectionName, Filter $filter): int; } diff --git a/src/InMemoryDocumentStore.php b/src/InMemoryDocumentStore.php index f2174b9..c0661f9 100644 --- a/src/InMemoryDocumentStore.php +++ b/src/InMemoryDocumentStore.php @@ -572,4 +572,21 @@ private function isSequentialArray(array $array): bool return \array_keys($array) === \range(0, \count($array) - 1); } + + /** + * @inheritDoc + */ + public function countDocs(string $collectionName, Filter $filter) : int + { + $this->assertHasCollection($collectionName); + + $counter = 0; + foreach ($this->inMemoryConnection['documents'][$collectionName] as $docId => $doc) { + if ($filter->match($doc, $docId)) { + $counter++; + } + } + + return $counter; + } }