Passed
Push — master ( c67529...0f0d28 )
by
unknown
13:44
created

SimplePagination::getNextPageNumber()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(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
18
namespace TYPO3\CMS\Core\Pagination;
19
20
final class SimplePagination implements PaginationInterface
21
{
22
    /**
23
     * @var PaginatorInterface
24
     */
25
    protected $paginator;
26
27
    public function __construct(PaginatorInterface $paginator)
28
    {
29
        $this->paginator = $paginator;
30
    }
31
32
    public function getPreviousPageNumber(): ?int
33
    {
34
        $previousPage = $this->paginator->getCurrentPageNumber() - 1;
35
36
        if ($previousPage > $this->paginator->getNumberOfPages()) {
37
            return null;
38
        }
39
40
        return $previousPage >= $this->getFirstPageNumber()
41
            ? $previousPage
42
            : null
43
        ;
44
    }
45
46
    public function getNextPageNumber(): ?int
47
    {
48
        $nextPage = $this->paginator->getCurrentPageNumber() + 1;
49
50
        return $nextPage <= $this->paginator->getNumberOfPages()
51
            ? $nextPage
52
            : null
53
        ;
54
    }
55
56
    public function getFirstPageNumber(): int
57
    {
58
        return 1;
59
    }
60
61
    public function getLastPageNumber(): int
62
    {
63
        return $this->paginator->getNumberOfPages();
64
    }
65
66
    public function getStartRecordNumber(): int
67
    {
68
        if ($this->paginator->getCurrentPageNumber() > $this->paginator->getNumberOfPages()) {
69
            return 0;
70
        }
71
72
        return $this->paginator->getKeyOfFirstPaginatedItem() + 1;
73
    }
74
75
    public function getEndRecordNumber(): int
76
    {
77
        if ($this->paginator->getCurrentPageNumber() > $this->paginator->getNumberOfPages()) {
78
            return 0;
79
        }
80
81
        return $this->paginator->getKeyOfLastPaginatedItem() + 1;
82
    }
83
84
    /**
85
     * @return int[]
86
     */
87
    public function getAllPageNumbers(): array
88
    {
89
        return range($this->getFirstPageNumber(), $this->getLastPageNumber());
90
    }
91
}
92