Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Doctrine/Odm/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,23 @@ final class Paginator implements \IteratorAggregate, PaginatorInterface, HasNext

private readonly int $totalItems;

private readonly int $count;

public function __construct(private readonly Iterator $mongoDbOdmIterator, private readonly UnitOfWork $unitOfWork, private readonly string $resourceClass, private readonly array $pipeline)
Copy link
Copy Markdown
Contributor

@GromNaN GromNaN Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can forget it.

Suggested change
public function __construct(private readonly Iterator $mongoDbOdmIterator, private readonly UnitOfWork $unitOfWork, private readonly string $resourceClass, private readonly array $pipeline)
public function __construct(Iterator $mongoDbOdmIterator, private readonly UnitOfWork $unitOfWork, private readonly string $resourceClass, private readonly array $pipeline)

{
$array = $mongoDbOdmIterator->toArray();
$resultsFacetInfo = $this->getFacetInfo('results');
$this->getFacetInfo('count');
$this->iterator = new \ArrayIterator(array_map(fn ($result): object => $this->unitOfWork->getOrCreateDocument($this->resourceClass, $result), $array[0]['results']));

/*
* Since the {@see \MongoDB\Driver\Cursor} class does not expose information about
* skip/limit parameters of the query, the values set in the facet stage are used instead.
*/
$this->firstResult = $this->getStageInfo($resultsFacetInfo, '$skip');
$this->maxResults = $this->hasLimitZeroStage($resultsFacetInfo) ? 0 : $this->getStageInfo($resultsFacetInfo, '$limit');
$this->totalItems = $mongoDbOdmIterator->toArray()[0]['count'][0]['count'] ?? 0;
$this->totalItems = $array[0]['count'][0]['count'] ?? 0;
$this->count = is_countable($array[0]['results']) ? \count($array[0]['results']) : 0;
}

/**
Expand Down Expand Up @@ -97,15 +102,15 @@ public function getTotalItems(): float
*/
public function getIterator(): \Traversable
{
return $this->iterator ?? $this->iterator = new \ArrayIterator(array_map(fn ($result): object => $this->unitOfWork->getOrCreateDocument($this->resourceClass, $result), $this->mongoDbOdmIterator->toArray()[0]['results']));
return $this->iterator;
}

/**
* {@inheritdoc}
*/
public function count(): int
{
return is_countable($this->mongoDbOdmIterator->toArray()[0]['results']) ? \count($this->mongoDbOdmIterator->toArray()[0]['results']) : 0;
return $this->count;
}

/**
Expand Down