Passed
Push — v5.x ( 26d105...7911f3 )
by Thierry
03:11
created

PaginatorPlugin::getReadyScript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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