Completed
Push — master ( 46be75...aab372 )
by Thierry
01:45
created

Renderer::__constructor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Renderer.php - Paginator renderer
5
 *
6
 * Render pagination links.
7
 *
8
 * @package jaxon-core
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
 */
14
15
namespace Jaxon\Utils\Pagination;
16
17
use Jaxon\Utils\Template\Template;
18
19
class Renderer
20
{
21
    /**
22
     * Set the paginator to be rendered.
23
     *
24
     * @var Paginator
25
     */
26
    protected $xPaginator = null;
27
28
    /**
29
     * The template manager.
30
     *
31
     * Will be used to render HTML code for links.
32
     *
33
     * @var Template
34
     */
35
    protected $xTemplate = null;
36
37
    /**
38
     * The class contructor
39
     *
40
     * @param Template          $xTemplate
41
     */
42
    public function __construct(Template $xTemplate)
43
    {
44
        $this->xTemplate = $xTemplate;
45
    }
46
47
    /**
48
     * Set the paginator to be rendered.
49
     *
50
     * @param Paginator         $xPaginator         The paginator to be rendered
51
     *
52
     * @return void
53
     */
54
    public function setPaginator(\Jaxon\Utils\Pagination\Paginator $xPaginator)
55
    {
56
        $this->xPaginator = $xPaginator;
57
    }
58
59
    /**
60
     * Render the previous link.
61
     *
62
     * @return string
63
     */
64 View Code Duplication
    protected function getPrevLink()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        if(!($sCall = $this->xPaginator->getPrevCall()))
67
        {
68
            return '';
69
        }
70
        return $this->xTemplate->render('pagination::links/prev',
71
            ['call' => $sCall, 'text' => $this->xPaginator->getPreviousText()]);
72
    }
73
74
    /**
75
     * Render the next link.
76
     *
77
     * @return string
78
     */
79 View Code Duplication
    protected function getNextLink()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
        if(!($sCall = $this->xPaginator->getNextCall()))
82
        {
83
            return '';
84
        }
85
        return $this->xTemplate->render('pagination::links/next',
86
            ['call' => $sCall, 'text' => $this->xPaginator->getNextText()]);
87
    }
88
89
    /**
90
     * Render the pagination links.
91
     *
92
     * @return string
93
     */
94
    protected function getLinks()
95
    {
96
        $sLinks = '';
97
        foreach($this->xPaginator->getPages() as $page)
98
        {
99
            if($page['call'])
100
            {
101
                $sTemplate = ($page['isCurrent'] ? 'pagination::links/current' : 'pagination::links/enabled');
102
                $sLinks .= $this->xTemplate->render($sTemplate, ['call' => $page['call'], 'text' => $page['num']]);
103
            }
104
            else
105
            {
106
                $sLinks .= $this->xTemplate->render('pagination::links/disabled', ['text' => $page['num']]);
107
            }
108
        }
109
        return $sLinks;
110
    }
111
112
    /**
113
     * Render an HTML pagination control.
114
     *
115
     * @return string
116
     */
117
    public function render()
118
    {
119
        return $this->xTemplate->render('pagination::wrapper', [
120
            'links' => $this->getLinks(),
121
            'prev' => $this->getPrevLink(),
122
            'next' => $this->getNextLink(),
123
        ]);
124
    }
125
}
126