Passed
Push — v5.x ( 7911f3...8eee51 )
by Thierry
02:06
created

PaginatorPlugin::getHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
25
/**
26
 * Usage
27
 *
28
 * $paginator = $this->response->pg->create($pageNumber, $perPage, $total);
29
 * // Or, using the response shortcut
30
 * $paginator = $this->response->paginator($pageNumber, $perPage, $total);
31
 * // Or, in a class that inherits from CallableClass
32
 * $paginator = $this->paginator($pageNumber, $perPage, $total);
33
 * $html = $this->render($pageTemplate, [
34
 *     // ...
35
 *     'pagination' => $paginator->wrapper($wrapperId),
36
 * ]);
37
 * $this->response->html($pageWrapper, $html);
38
 * $this->response->pg->render($paginator, $this->rq()->page());
39
 * // Or, using the response shortcut
40
 * $this->response->paginate($paginator, $this->rq()->page());
41
 * // Or, in a class that inherits from CallableClass
42
 * $this->paginate($paginator, $this->rq()->page());
43
 * 
44
 */
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...
45
class PaginatorPlugin extends ResponsePlugin
46
{
47
    /**
48
     * @const The plugin name
49
     */
50
    const NAME = 'pg';
51
52
    /**
53
     * @var ViewRenderer
54
     */
55
    protected $xRenderer;
56
57
    /**
58
     * The constructor.
59
     *
60
     * @param ViewRenderer $xRenderer
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
61
     */
62
    public function __construct(ViewRenderer $xRenderer)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
63
    {
64
        $this->xRenderer = $xRenderer;
65
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
66
67
    /**
68
     * @inheritDoc
69
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
70
    public function getName(): string
71
    {
72
        return self::NAME;
73
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
74
75
    /**
76
     * @inheritDoc
77
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
78
    public function getHash(): string
79
    {
80
        // Use the version number as hash
81
        return '5.0.0';
82
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
83
84
    /**
85
     * @inheritDoc
86
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
87
    public function getReadyScript(): string
88
    {
89
        return '
90
    jaxon.command.handler.register("paginate", (args) => jaxon.cmd.event.paginate(args));
91
';
92
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
93
94
    /**
95
     * Create a paginator
96
     *
97
     * @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...
98
     * @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...
99
     * @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...
100
     *
101
     * @return Paginator
102
     */
103
    public function create(int $nCurrentPage, int $nItemsPerPage, int $nTotalItems): Paginator
104
    {
105
        return new Paginator($nCurrentPage, $nItemsPerPage, $nTotalItems);
106
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
107
108
    /**
109
     * @param array<Page> $aPages
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
110
     *
111
     * @return null|Store
112
     */
113
    private function _render(array $aPages): ?Store
114
    {
115
        $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...
116
            return $this->xRenderer->render('pagination::links/' . $xPage->sType, [
117
                'page' => $xPage->nNumber,
118
                'text' => $xPage->sText,
119
            ]);
120
        }, $aPages);
121
        $aPrevPage = array_shift($aPages); // The first entry in the array
122
        $aNextPage = array_pop($aPages); // The last entry in the array
123
124
        return $this->xRenderer->render('pagination::wrapper', [
125
            'links' => $aPages,
126
            'prev' => $aPrevPage,
127
            'next' => $aNextPage,
128
        ]);
129
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
130
131
    /**
132
     * Render an HTML pagination control.
133
     *
134
     * @param Paginator $xPaginator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
135
     * @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...
136
     * @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...
137
     *
138
     * @return void
139
     */
140
    public function render(Paginator $xPaginator, Call $xCall, string $sWrapperId = '')
141
    {
142
        $aPages = $xPaginator->pages();
143
        if(count($aPages) === 0)
144
        {
145
            return;
146
        }
147
        if(!($xStore = $this->_render($aPages)))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
148
        {
149
            return;
150
        }
151
152
        // Append the page number to the parameter list, if not yet given.
153
        if(!$xCall->hasPageNumber())
154
        {
155
            $xCall->addParameter(Parameter::PAGE_NUMBER, 0);
156
        }
157
158
        // Show the pagination links
159
        $this->response()->html($sWrapperId ?? $xPaginator->wrapperId(), $xStore->__toString());
160
        // Set click handlers on the pagination links
161
        $aCommand = [
162
            'cmd' => 'paginate',
163
            'id' => $xPaginator->wrapperId(),
164
            'call' => $xCall->toArray(),
165
        ];
166
        $aPages = array_map(function(Page $xPage) {
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
167
            return [
168
                'type' => $xPage->sType,
169
                'page' => $xPage->nNumber,
170
            ];
171
        }, $aPages);
172
        $this->addCommand($aCommand, $aPages);
173
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
174
}
175