Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
86.36% |
19 / 22 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
PagePagination | |
86.36% |
19 / 22 |
|
83.33% |
5 / 6 |
11.31 | |
0.00% |
0 / 1 |
__construct | |
50.00% |
3 / 6 |
|
0.00% |
0 / 1 |
2.50 | |||
getPageNumber | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
getDisplayedPageNumber | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getNumberOfPages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPageTitle | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
pageNumberExists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace ProofreadPage\Pagination; |
4 | |
5 | use InvalidArgumentException; |
6 | use MediaWiki\Title\Title; |
7 | use OutOfBoundsException; |
8 | |
9 | /** |
10 | * @license GPL-2.0-or-later |
11 | * |
12 | * Pagination of a book based on a set of independants pages |
13 | */ |
14 | class PagePagination extends Pagination { |
15 | |
16 | /** @var Title[] */ |
17 | private $pages = []; |
18 | |
19 | /** @var PageNumber[] */ |
20 | private $pageNumbers = []; |
21 | |
22 | /** |
23 | * @param Title[] $pages the ordered pages |
24 | * @param PageNumber[] $pageNumbers with $pageNumbers[i] the page number of the page $pages[i] |
25 | * @throws InvalidArgumentException |
26 | */ |
27 | public function __construct( array $pages, array $pageNumbers ) { |
28 | if ( count( $pages ) !== count( $pageNumbers ) ) { |
29 | throw new InvalidArgumentException( |
30 | 'The number of page numbers is not the same as the number of pages' |
31 | ); |
32 | } |
33 | |
34 | $this->pages = $pages; |
35 | $this->pageNumbers = $pageNumbers; |
36 | } |
37 | |
38 | /** |
39 | * @inheritDoc |
40 | */ |
41 | public function getPageNumber( Title $pageTitle ): int { |
42 | foreach ( $this->pages as $i => $pageTitle2 ) { |
43 | if ( $pageTitle->equals( $pageTitle2 ) ) { |
44 | return $i + 1; |
45 | } |
46 | } |
47 | throw new PageNotInPaginationException( '$page does not belong to the pagination' ); |
48 | } |
49 | |
50 | /** |
51 | * @inheritDoc |
52 | */ |
53 | public function getDisplayedPageNumber( int $pageNumber ): PageNumber { |
54 | if ( !$this->pageNumberExists( $pageNumber ) ) { |
55 | throw new OutOfBoundsException( |
56 | 'There is no page number ' . $pageNumber . ' in the pagination.' |
57 | ); |
58 | } |
59 | return $this->pageNumbers[$pageNumber - 1]; |
60 | } |
61 | |
62 | /** |
63 | * @inheritDoc |
64 | */ |
65 | public function getNumberOfPages(): int { |
66 | return count( $this->pages ); |
67 | } |
68 | |
69 | /** |
70 | * @inheritDoc |
71 | */ |
72 | public function getPageTitle( int $pageNumber ): Title { |
73 | if ( !$this->pageNumberExists( $pageNumber ) ) { |
74 | throw new OutOfBoundsException( |
75 | 'There is no page number ' . $pageNumber . ' in the pagination.' |
76 | ); |
77 | } |
78 | return $this->pages[$pageNumber - 1]; |
79 | } |
80 | |
81 | /** |
82 | * @inheritDoc |
83 | */ |
84 | protected function pageNumberExists( int $pageNumber ): bool { |
85 | return array_key_exists( $pageNumber - 1, $this->pages ); |
86 | } |
87 | } |