Passed
Push — master ( 98205f...6f7a23 )
by Thierry
02:22
created

PaginationRenderer::renderLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
/**
4
 * PaginationRenderer.php - Paginator renderer
5
 *
6
 * Render pagination links.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2016 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
14
15
namespace Jaxon\Ui\View;
16
17
use Jaxon\Request\Call\Paginator;
18
19
use function array_map;
20
21
class PaginationRenderer
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class PaginationRenderer
Loading history...
22
{
23
    /**
24
     * The viev renderer.
25
     *
26
     * @var ViewRenderer
27
     */
28
    protected $xRenderer = null;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
29
30
    /**
31
     * The class constructor
32
     *
33
     * @param ViewRenderer  $xRenderer
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
34
     */
35
    public function __construct(ViewRenderer $xRenderer)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
36
    {
37
        $this->xRenderer = $xRenderer;
38
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
39
40
    /**
41
     * Render a link to a page.
42
     *
43
     * @param string $sTemplate    The template for the link to the page
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
44
     * @param string $sText    The text of the link if it is enabled
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
45
     * @param string $sCall    The call of the link if it is enabled
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
46
     *
47
     * @return null|Store
48
     */
49
    protected function renderLink(string $sTemplate, string $sText, string $sCall): ?Store
50
    {
51
        return $this->xRenderer->render('pagination::links/' . $sTemplate, [
52
            'text' => $sText,
53
            'call' => $sCall,
54
        ]);
55
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
56
57
    /**
58
     * Render an HTML pagination control.
59
     *
60
     * @param Paginator $xPaginator The paginator
61
     *
62
     * @return null|Store
63
     */
64
    public function render(Paginator $xPaginator): ?Store
65
    {
66
        $aLinks = array_map(function($aPage) {
67
            return $this->renderLink($aPage[0], $aPage[1], $aPage[2]);
68
        }, $xPaginator->links());
69
70
        $aPrevLink = array_shift($aLinks); // The first entry in the array
71
        $aNextLink = array_pop($aLinks); // The last entry in the array
72
        return $this->xRenderer->render('pagination::wrapper',
73
            ['links' => $aLinks, 'prev' => $aPrevLink, 'next' => $aNextLink]);
74
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
75
}
76