|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of the API Platform project. |
| 4 | + * |
| 5 | + * (c) Kévin Dunglas <[email protected]> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +declare(strict_types=1); |
| 12 | + |
| 13 | +namespace ApiPlatform\State\Pagination; |
| 14 | + |
| 15 | +use Symfony\Component\ObjectMapper\ObjectMapperInterface; |
| 16 | + |
| 17 | +final class MappedObjectPaginator implements \IteratorAggregate, PaginatorInterface |
| 18 | +{ |
| 19 | + public function __construct( |
| 20 | + private readonly iterable $entities, |
| 21 | + private readonly ObjectMapperInterface $mapper, |
| 22 | + private readonly string $resourceClass, |
| 23 | + private readonly float $totalItems = 0.0, |
| 24 | + private readonly float $currentPage = 1.0, |
| 25 | + private readonly float $lastPage = 1.0, |
| 26 | + private readonly float $itemsPerPage = 30.0, |
| 27 | + ) {} |
| 28 | + |
| 29 | + public function count(): int |
| 30 | + { |
| 31 | + return (int) $this->totalItems; |
| 32 | + } |
| 33 | + |
| 34 | + public function getLastPage(): float |
| 35 | + { |
| 36 | + return $this->lastPage; |
| 37 | + } |
| 38 | + |
| 39 | + public function getTotalItems(): float |
| 40 | + { |
| 41 | + return $this->totalItems; |
| 42 | + } |
| 43 | + |
| 44 | + public function getCurrentPage(): float |
| 45 | + { |
| 46 | + return $this->currentPage; |
| 47 | + } |
| 48 | + |
| 49 | + public function getItemsPerPage(): float |
| 50 | + { |
| 51 | + return $this->itemsPerPage; |
| 52 | + } |
| 53 | + |
| 54 | + public function getIterator(): \Traversable |
| 55 | + { |
| 56 | + foreach ($this->entities as $entity) { |
| 57 | + yield $this->mapper->map($entity, $this->resourceClass); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments