Passed
Push — v5.x ( 4cf44b...6eb5e2 )
by Thierry
02:02
created

PaginatorPlugin::wrapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * PaginatorPlugin.php - The Jaxon Paginator plugin
5
 *
6
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
7
 * @copyright 2024 Thierry Feuzeu
8
 * @license https://opensource.org/licenses/MIT MIT License
9
 * @link https://github.com/jaxon-php/jaxon-core
10
 */
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...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
11
12
namespace Jaxon\Plugin\Response\Pagination;
13
14
use Jaxon\App\View\ViewRenderer;
15
use Jaxon\App\View\Store;
16
use Jaxon\Plugin\ResponsePlugin;
17
use Jaxon\Request\Call\Call;
18
use Jaxon\Request\Call\Parameter;
19
20
use function array_map;
21
use function array_pop;
22
use function array_shift;
23
use function count;
24
use function trim;
25
26
/**
27
 * Usage
28
 *
29
 * Step 1: Render a template containing a wrapper for the pagination.
30
 *
31
 * $html = $this->render($pageTemplate, [
32
 *     // ...
33
 * ]);
34
 *
35
 * Step 2: Create a paginator and render the pagination into the wrapper.
36
 *
37
 * $this->response->pg->paginator($pageNumber, $perPage, $total)
38
 *     ->paginate($this->rq()->page(), $wrapperId);
39
 * // Or, using the response shortcut
40
 * $this->response->paginator($pageNumber, $perPage, $total)
41
 *     ->paginate($this->rq()->page(), $wrapperId);
42
 * // Or, in a class that inherits from CallableClass
43
 * $this->paginator($pageNumber, $perPage, $total)
44
 *     ->paginate($this->rq()->page(), $wrapperId);
45
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
46
class PaginatorPlugin extends ResponsePlugin
47
{
48
    /**
49
     * @const The plugin name
50
     */
51
    const NAME = 'pg';
52
53
    /**
54
     * @var ViewRenderer
55
     */
56
    protected $xRenderer;
57
58
    /**
59
     * The constructor.
60
     *
61
     * @param ViewRenderer $xRenderer
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
62
     */
63
    public function __construct(ViewRenderer $xRenderer)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
64
    {
65
        $this->xRenderer = $xRenderer;
66
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
67
68
    /**
69
     * @inheritDoc
70
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
71
    public function getName(): string
72
    {
73
        return self::NAME;
74
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
75
76
    /**
77
     * @inheritDoc
78
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
79
    public function getHash(): string
80
    {
81
        // Use the version number as hash
82
        return '5.0.0';
83
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
84
85
    /**
86
     * @param array<Page> $aPages
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
87
     *
88
     * @return null|Store
89
     */
90
    private function _render(array $aPages): ?Store
91
    {
92
        $aPages = array_map(function($xPage) {
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
93
            return $this->xRenderer->render('pagination::links/' . $xPage->sType, [
94
                'page' => $xPage->nNumber,
95
                'text' => $xPage->sText,
96
            ]);
97
        }, $aPages);
98
        $aPrevPage = array_shift($aPages); // The first entry in the array
99
        $aNextPage = array_pop($aPages); // The last entry in the array
100
101
        return $this->xRenderer->render('pagination::wrapper', [
102
            'links' => $aPages,
103
            'prev' => $aPrevPage,
104
            'next' => $aNextPage,
105
        ]);
106
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
107
108
    /**
109
     * Create a paginator
110
     *
111
     * @param int $nCurrentPage     The current page number
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 5 found
Loading history...
112
     * @param int $nItemsPerPage    The number of items per page
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
113
     * @param int $nTotalItems      The total number of items
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 6 found
Loading history...
114
     *
115
     * @return Paginator
116
     */
117
    public function paginator(int $nCurrentPage, int $nItemsPerPage, int $nTotalItems): Paginator
118
    {
119
        return new Paginator($this, $nCurrentPage, $nItemsPerPage, $nTotalItems);
120
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
121
122
    /**
123
     * Render an HTML pagination control.
124
     *
125
     * @param Paginator $xPaginator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
126
     * @param Call $xCall
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
127
     * @param string $sWrapperId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
128
     *
129
     * @return void
130
     */
131
    public function render(Paginator $xPaginator, Call $xCall, string $sWrapperId)
132
    {
133
        $aPages = $xPaginator->pages();
134
        if(count($aPages) === 0)
135
        {
136
            return;
137
        }
138
        $xStore = $this->_render($aPages);
139
        if(!$xStore)
140
        {
141
            return;
142
        }
143
144
        // Append the page number to the parameter list, if not yet given.
145
        if(!$xCall->hasPageNumber())
146
        {
147
            $xCall->addParameter(Parameter::PAGE_NUMBER, 0);
148
        }
149
150
        // Show the pagination links
151
        $sWrapperId = trim($sWrapperId);
152
        $this->response()->html($sWrapperId, $xStore->__toString());
153
        // Set click handlers on the pagination links
154
        $this->addCommand('pg.paginate', [
155
            'id' => $sWrapperId,
156
            'func' => $xCall->toArray(),
157
        ]);
158
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
159
}
160