Skip to content

Commit 20c60f4

Browse files
committed
Add countDocs method
This allows one to get the number of affected documents without actually having to retrieve all the documents from the store. This fixes #1
1 parent 4012a25 commit 20c60f4

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/DocumentStore.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,12 @@ public function getDoc(string $collectionName, string $docId): ?array;
136136
* @throws UnknownCollection
137137
*/
138138
public function filterDocs(string $collectionName, Filter $filter, int $skip = null, int $limit = null, OrderBy $orderBy = null): \Traversable;
139+
140+
/**
141+
* @param string $collectionName
142+
* @param Filter $filter
143+
* @return int The number of documents
144+
* @throws UnknownCollection
145+
*/
146+
public function countDocs(string $collectionName, Filter $filter): int;
139147
}

src/InMemoryDocumentStore.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,21 @@ private function sort(&$docs, OrderBy $orderBy)
329329
return $docCmp($docA, $docB, $orderBy);
330330
});
331331
}
332+
333+
/**
334+
* @inheritDoc
335+
*/
336+
public function countDocs(string $collectionName, Filter $filter) : int
337+
{
338+
$this->assertHasCollection($collectionName);
339+
340+
$counter = 0;
341+
foreach ($this->inMemoryConnection['documents'][$collectionName] as $docId => $doc) {
342+
if ($filter->match($doc, $docId)) {
343+
$counter++;
344+
}
345+
}
346+
347+
return $counter;
348+
}
332349
}

0 commit comments

Comments
 (0)