CliPager   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 45
ccs 19
cts 19
cp 1
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getPager() 0 3 1
A __toString() 0 3 1
B render() 0 14 8
1
<?php
2
3
namespace kalanis\kw_paging\Render;
4
5
6
use kalanis\kw_pager\Interfaces\IPager;
7
use kalanis\kw_paging\Interfaces\IOutput;
8
use kalanis\kw_paging\Interfaces\IPGTranslations;
9
use kalanis\kw_paging\Interfaces\IPositions;
10
use kalanis\kw_paging\Traits;
11
use kalanis\kw_paging\Translations;
12
13
14
/**
15
 * Class CliPager
16
 * @package kalanis\kw_paging\Render
17
 * Pager for displaying on CLI
18
 */
19
class CliPager implements IOutput
20
{
21
    use Traits\TDisplayPages;
22
    use Traits\THelpingText;
23
24
    public const SELECT_PAGE = '*';
25
    public const NONE_PAGE = '-';
26
    public const PREV_PAGE = '<';
27
    public const NEXT_PAGE = '>';
28
29 9
    public function __construct(
30
        IPositions $positions,
31
        int $displayPages = IPositions::DEFAULT_DISPLAY_PAGES_COUNT,
32
        ?IPGTranslations $lang = null
33
    )
34
    {
35 9
        $this->positions = $positions;
36 9
        $this->displayPagesCount = $displayPages;
37 9
        $this->setKpgLang($lang ?: new Translations());
38 9
    }
39
40 8
    public function __toString()
41
    {
42 8
        return $this->render();
43
    }
44
45 4
    public function render(bool $showPositions = true): string
46
    {
47 4
        if (!$this->getPositions()->prevPageExists() && !$this->getPositions()->nextPageExists()) {
48 1
            return '';
49
        }
50 3
        $pages = [];
51
52 3
        $pages[] = $this->getPositions()->prevPageExists() ? static::PREV_PAGE . static::PREV_PAGE . ' ' . $this->getPositions()->getFirstPage() : static::NONE_PAGE . static::NONE_PAGE ;
53 3
        $pages[] = $this->getPositions()->prevPageExists() ? static::PREV_PAGE . ' ' . $this->getPositions()->getPrevPage() : static::NONE_PAGE ;
54 3
        $pages[] = $this->getPositions()->getPager()->getActualPage() ;
55 3
        $pages[] = $this->getPositions()->nextPageExists() ? $this->getPositions()->getNextPage() . ' ' . static::NEXT_PAGE : static::NONE_PAGE ;
56 3
        $pages[] = $this->getPositions()->nextPageExists() ? $this->getPositions()->getLastPage() . ' ' . static::NEXT_PAGE . static::NEXT_PAGE : static::NONE_PAGE . static::NONE_PAGE ;
57
58 3
        return implode(' | ', $pages) . ($showPositions ? ( PHP_EOL . $this->getFilledText($this->getPositions()) ) : '' );
59
    }
60
61 1
    public function getPager(): IPager
62
    {
63 1
        return $this->getPositions()->getPager();
64
    }
65
}
66