TDisplayPages::getDisplayPages()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 12
c 0
b 0
f 0
nc 6
nop 0
dl 0
loc 21
ccs 13
cts 13
cp 1
crap 5
rs 9.5555
1
<?php
2
3
namespace kalanis\kw_paging\Traits;
4
5
6
use kalanis\kw_paging\Interfaces\IPositions;
7
8
9
/**
10
 * Trait TDisplayPages
11
 * @package kalanis\kw_paging\Render
12
 * Trait to select pages to render
13
 */
14
trait TDisplayPages
15
{
16
    use TPositions;
17
18
    protected int $displayPagesCount = IPositions::DEFAULT_DISPLAY_PAGES_COUNT;
19
20
    /**
21
     * Return array of page numbers, which will be rendered for current pager state. If we want another way to render, just overwrite this method.
22
     * @return int[]
23
     */
24 9
    protected function getDisplayPages(): array
25
    {
26 9
        $actualPage = $this->getPositions()->getPager()->getActualPage(); // 2
27 9
        $count = $this->getPositions()->getPager()->getPagesCount(); // 20
28 9
        $whole = $this->displayPagesCount; // 10
29
30 9
        $half = floor($whole / 2); // 5
31 9
        $tail = $count - $actualPage; // 18
32
33 9
        $i = ($tail > $half) ? intval($actualPage - $half) : intval($count - $whole + 1); // 3
34 9
        $result = [];
35
36
        // ++ < 10 && 3 <= 20
37 9
        while ((count($result) < $this->displayPagesCount) && ($i <= $count)) {
38 9
            if ($this->getPositions()->getPager()->pageExists($i)) {
39 9
                $result[] = $i;
40
            }
41 9
            $i++;
42
        }
43
44 9
        return $result;
45
    }
46
}
47