1<?php
2
3declare(strict_types=1);
4
5/*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18namespace TYPO3\CMS\Extbase\Pagination;
19
20use TYPO3\CMS\Core\Pagination\AbstractPaginator;
21use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
22
23final class QueryResultPaginator extends AbstractPaginator
24{
25    /**
26     * @var QueryResultInterface
27     */
28    private $queryResult;
29
30    /**
31     * @var QueryResultInterface
32     */
33    private $paginatedQueryResult;
34
35    public function __construct(
36        QueryResultInterface $queryResult,
37        int $currentPageNumber = 1,
38        int $itemsPerPage = 10
39    ) {
40        $this->queryResult = $queryResult;
41        $this->setCurrentPageNumber($currentPageNumber);
42        $this->setItemsPerPage($itemsPerPage);
43
44        $this->updateInternalState();
45    }
46
47    /**
48     * @return iterable|QueryResultInterface
49     */
50    public function getPaginatedItems(): iterable
51    {
52        return $this->paginatedQueryResult;
53    }
54
55    protected function updatePaginatedItems(int $limit, int $offset): void
56    {
57        $this->paginatedQueryResult = $this->queryResult
58            ->getQuery()
59            ->setLimit($limit)
60            ->setOffset($offset)
61            ->execute();
62    }
63
64    protected function getTotalAmountOfItems(): int
65    {
66        return count($this->queryResult);
67    }
68
69    protected function getAmountOfItemsOnCurrentPage(): int
70    {
71        return count($this->paginatedQueryResult);
72    }
73}
74