|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* PaginationRenderer.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\Ui\View; |
|
16
|
|
|
|
|
17
|
|
|
use Jaxon\Request\Call\Paginator; |
|
18
|
|
|
|
|
19
|
|
|
use function array_map; |
|
20
|
|
|
|
|
21
|
|
|
class PaginationRenderer |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* The viev renderer. |
|
25
|
|
|
* |
|
26
|
|
|
* @var ViewRenderer |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $xRenderer = null; |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The class constructor |
|
32
|
|
|
* |
|
33
|
|
|
* @param ViewRenderer $xRenderer |
|
|
|
|
|
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(ViewRenderer $xRenderer) |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
$this->xRenderer = $xRenderer; |
|
38
|
|
|
} |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Render a link to a page. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $sTemplate The template for the link to the page |
|
|
|
|
|
|
44
|
|
|
* @param string $sText The text of the link if it is enabled |
|
|
|
|
|
|
45
|
|
|
* @param string $sCall The call of the link if it is enabled |
|
|
|
|
|
|
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
|
|
|
} |
|
|
|
|
|
|
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
|
|
|
} |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|