CliPager::render()   B
last analyzed

Complexity

Conditions 8
Paths 33

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 9
c 1
b 0
f 0
nc 33
nop 1
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 8
rs 8.4444
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