Failed Conditions
Pull Request — task/2976_TYPO3.11_compatibili... (#3097)
by
unknown
44:13
created

ResultsPagination::setMaxPageNumbers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Pagination;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Core\Pagination\PaginationInterface;
18
use TYPO3\CMS\Core\Pagination\PaginatorInterface;
19
20
/**
21
 * Class ResultsPagination
22
 *
23
 * @author Rudy Gnodde <[email protected]>
24
 */
25
class ResultsPagination implements PaginationInterface
26
{
27
    /**
28
     * @var PaginatorInterface
29
     */
30
    protected $paginator;
31
32
    /**
33
     * @var int
34
     */
35
    protected $maxPageNumbers = 0;
36
37
    /**
38
     * @var bool
39
     */
40
    protected $hasMorePages = false;
41
42
    /**
43
     * @var bool
44
     */
45
    protected $hasLessPages = false;
46
47
    /**
48
     * @var int
49
     */
50
    protected $pageRangeFirst;
51
52
    /**
53
     * @var int
54
     */
55
    protected $pageRangeLast;
56
57
    /**
58
     * @param PaginatorInterface $paginator
59
     */
60
    public function __construct(PaginatorInterface $paginator)
61
    {
62
        $this->paginator = $paginator;
63
        $this->calculatePageRange();
64
    }
65
66
    /**
67
     * @return int
68
     */
69
    public function getMaxPageNumbers(): int
70
    {
71
        return $this->maxPageNumbers;
72
    }
73
74
    /**
75
     * @param int $maxPageNumbers
76
     */
77
    public function setMaxPageNumbers(int $maxPageNumbers): void
78
    {
79
        $this->maxPageNumbers = $maxPageNumbers;
80
        $this->calculatePageRange();
81
    }
82
83
    /**
84
     * @return bool
85
     */
86
    public function getHasMorePages(): bool
87
    {
88
        return $this->hasMorePages;
89
    }
90
91
    /**
92
     * @return bool
93
     */
94
    public function getHasLessPages(): bool
95
    {
96
        return $this->hasLessPages;
97
    }
98
99
    /**
100
     * @return int|null
101
     */
102
    public function getPreviousPageNumber(): ?int
103
    {
104
        $previousPage = $this->paginator->getCurrentPageNumber() - 1;
105
106
        if ($previousPage > $this->paginator->getNumberOfPages()) {
107
            return null;
108
        }
109
110
        return $previousPage >= $this->getFirstPageNumber()
111
            ? $previousPage
112
            : null
113
            ;
114
    }
115
116
    /**
117
     * @return int|null
118
     */
119
    public function getNextPageNumber(): ?int
120
    {
121
        $nextPage = $this->paginator->getCurrentPageNumber() + 1;
122
123
        return $nextPage <= $this->paginator->getNumberOfPages()
124
            ? $nextPage
125
            : null;
126
    }
127
128
    /**
129
     * @return int
130
     */
131
    public function getFirstPageNumber(): int
132
    {
133
        return 1;
134
    }
135
136
    /**
137
     * @return int
138
     */
139
    public function getLastPageNumber(): int
140
    {
141
        return $this->paginator->getNumberOfPages();
142
    }
143
144
    /**
145
     * @return int
146
     */
147
    public function getStartRecordNumber(): int
148
    {
149
        if ($this->paginator->getCurrentPageNumber() > $this->paginator->getNumberOfPages()) {
150
            return 0;
151
        }
152
153
        return $this->paginator->getKeyOfFirstPaginatedItem() + 1;
154
    }
155
156
    /**
157
     * @return int
158
     */
159
    public function getEndRecordNumber(): int
160
    {
161
        if ($this->paginator->getCurrentPageNumber() > $this->paginator->getNumberOfPages()) {
162
            return 0;
163
        }
164
165
        return $this->paginator->getKeyOfLastPaginatedItem() + 1;
166
    }
167
168
    /**
169
     * @return int[]
170
     */
171
    public function getAllPageNumbers(): array
172
    {
173
        return range($this->pageRangeFirst, $this->pageRangeLast);
174
    }
175
176
    protected function calculatePageRange(): void
177
    {
178
        $this->pageRangeFirst = 1;
179
        $this->pageRangeLast = $this->getLastPageNumber();
180
        $this->hasLessPages = false;
181
        $this->hasMorePages = false;
182
183
        $maxNumberOfLinks = $this->getMaxPageNumbers();
184
        if ($maxNumberOfLinks > 0) {
185
            $numberOfPages = $this->paginator->getNumberOfPages();
186
187
            if ($numberOfPages > $maxNumberOfLinks) {
188
                $currentPage = $this->paginator->getCurrentPageNumber();
189
                $pagesBeforeAndAfter = ($maxNumberOfLinks - 1) / 2;
190
                $this->pageRangeFirst = $currentPage - floor($pagesBeforeAndAfter);
191
                $this->pageRangeLast = $currentPage + ceil($pagesBeforeAndAfter);
192
193
                if ($this->pageRangeFirst < 1) {
194
                    $this->pageRangeLast -= $this->pageRangeFirst - 1;
195
                }
196
                if ($this->pageRangeLast > $numberOfPages) {
197
                    $this->pageRangeFirst -= $this->pageRangeLast - $numberOfPages;
198
                }
199
                $this->pageRangeFirst = max($this->pageRangeFirst, 1);
200
                $this->pageRangeLast = min($this->pageRangeLast, $numberOfPages);
201
                $this->hasLessPages = $this->pageRangeFirst > 1;
202
                $this->hasMorePages = $this->pageRangeLast < $numberOfPages;
203
            }
204
        }
205
    }
206
}
207