Passed
Push — v5.x ( 57c2ac...73bdb8 )
by Thierry
16:58 queued 05:55
created

PaginatorPlugin   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 32
c 2
b 0
f 0
dl 0
loc 115
rs 10

6 Methods

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