@@ -128,9 +128,8 @@ discard block |
||
| 128 | 128 | */ |
| 129 | 129 | private function updatePagesCount(): Paginator |
| 130 | 130 | { |
| 131 | - $this->nPagesCount = ($this->nItemsPerPage === 0 ? 0 : |
|
| 132 | - (int)ceil($this->nItemsCount / $this->nItemsPerPage)); |
|
| 133 | - if($this->nPageNumber > $this->nPagesCount) |
|
| 131 | + $this->nPagesCount = ($this->nItemsPerPage === 0 ? 0 : (int)ceil($this->nItemsCount / $this->nItemsPerPage)); |
|
| 132 | + if ($this->nPageNumber > $this->nPagesCount) |
|
| 134 | 133 | { |
| 135 | 134 | $this->nPageNumber = $this->nPagesCount; |
| 136 | 135 | } |
@@ -184,8 +183,7 @@ discard block |
||
| 184 | 183 | protected function getPrevPage(): Page |
| 185 | 184 | { |
| 186 | 185 | return $this->nPageNumber <= 1 ? |
| 187 | - new Page('disabled', $this->sPreviousText, 0) : |
|
| 188 | - new Page('enabled', $this->sPreviousText, $this->nPageNumber - 1); |
|
| 186 | + new Page('disabled', $this->sPreviousText, 0) : new Page('enabled', $this->sPreviousText, $this->nPageNumber - 1); |
|
| 189 | 187 | } |
| 190 | 188 | |
| 191 | 189 | /** |
@@ -196,8 +194,7 @@ discard block |
||
| 196 | 194 | protected function getNextPage(): Page |
| 197 | 195 | { |
| 198 | 196 | return $this->nPageNumber >= $this->nPagesCount ? |
| 199 | - new Page('disabled', $this->sNextText, 0) : |
|
| 200 | - new Page('enabled', $this->sNextText, $this->nPageNumber + 1); |
|
| 197 | + new Page('disabled', $this->sNextText, 0) : new Page('enabled', $this->sNextText, $this->nPageNumber + 1); |
|
| 201 | 198 | } |
| 202 | 199 | |
| 203 | 200 | /** |
@@ -209,7 +206,7 @@ discard block |
||
| 209 | 206 | */ |
| 210 | 207 | protected function getPage(int $nNumber): Page |
| 211 | 208 | { |
| 212 | - if($nNumber < 1) |
|
| 209 | + if ($nNumber < 1) |
|
| 213 | 210 | { |
| 214 | 211 | return new Page('disabled', $this->sEllipsysText, 0); |
| 215 | 212 | } |
@@ -228,9 +225,9 @@ discard block |
||
| 228 | 225 | { |
| 229 | 226 | $aPageNumbers = []; |
| 230 | 227 | |
| 231 | - if($this->nPagesCount <= $this->nMaxPages) |
|
| 228 | + if ($this->nPagesCount <= $this->nMaxPages) |
|
| 232 | 229 | { |
| 233 | - for($i = 0; $i < $this->nPagesCount; $i++) |
|
| 230 | + for ($i = 0; $i < $this->nPagesCount; $i++) |
|
| 234 | 231 | { |
| 235 | 232 | $aPageNumbers[] = $i + 1; |
| 236 | 233 | } |
@@ -243,7 +240,7 @@ discard block |
||
| 243 | 240 | |
| 244 | 241 | $nSlidingStart = 1; |
| 245 | 242 | $nSlidingEndOffset = $nNumAdjacents + 3 - $this->nPageNumber; |
| 246 | - if($nSlidingEndOffset < 0) |
|
| 243 | + if ($nSlidingEndOffset < 0) |
|
| 247 | 244 | { |
| 248 | 245 | $nSlidingStart = $this->nPageNumber - $nNumAdjacents; |
| 249 | 246 | $nSlidingEndOffset = 0; |
@@ -251,23 +248,23 @@ discard block |
||
| 251 | 248 | |
| 252 | 249 | $nSlidingEnd = $this->nPagesCount; |
| 253 | 250 | $nSlidingStartOffset = $this->nPageNumber + $nNumAdjacents + 2 - $this->nPagesCount; |
| 254 | - if($nSlidingStartOffset < 0) |
|
| 251 | + if ($nSlidingStartOffset < 0) |
|
| 255 | 252 | { |
| 256 | 253 | $nSlidingEnd = $this->nPageNumber + $nNumAdjacents; |
| 257 | 254 | $nSlidingStartOffset = 0; |
| 258 | 255 | } |
| 259 | 256 | |
| 260 | 257 | // Build the list of page numbers. |
| 261 | - if($nSlidingStart > 1) |
|
| 258 | + if ($nSlidingStart > 1) |
|
| 262 | 259 | { |
| 263 | 260 | $aPageNumbers[] = 1; |
| 264 | 261 | $aPageNumbers[] = 0; // Ellipsys; |
| 265 | 262 | } |
| 266 | - for($i = $nSlidingStart - $nSlidingStartOffset; $i <= $nSlidingEnd + $nSlidingEndOffset; $i++) |
|
| 263 | + for ($i = $nSlidingStart - $nSlidingStartOffset; $i <= $nSlidingEnd + $nSlidingEndOffset; $i++) |
|
| 267 | 264 | { |
| 268 | 265 | $aPageNumbers[] = $i; |
| 269 | 266 | } |
| 270 | - if($nSlidingEnd < $this->nPagesCount) |
|
| 267 | + if ($nSlidingEnd < $this->nPagesCount) |
|
| 271 | 268 | { |
| 272 | 269 | $aPageNumbers[] = 0; // Ellipsys; |
| 273 | 270 | $aPageNumbers[] = $this->nPagesCount; |
@@ -293,7 +290,7 @@ discard block |
||
| 293 | 290 | */ |
| 294 | 291 | public function pages(): array |
| 295 | 292 | { |
| 296 | - if($this->nPagesCount < 2) |
|
| 293 | + if ($this->nPagesCount < 2) |
|
| 297 | 294 | { |
| 298 | 295 | return []; |
| 299 | 296 | } |
@@ -344,7 +341,7 @@ discard block |
||
| 344 | 341 | private function renderLinks(): string |
| 345 | 342 | { |
| 346 | 343 | $aPages = $this->pages(); |
| 347 | - if(count($aPages) === 0) |
|
| 344 | + if (count($aPages) === 0) |
|
| 348 | 345 | { |
| 349 | 346 | return ''; |
| 350 | 347 | } |
@@ -365,7 +362,7 @@ discard block |
||
| 365 | 362 | { |
| 366 | 363 | $sHtml = $this->renderLinks(); |
| 367 | 364 | // The HTML code must always be displayed, even if it is empty. |
| 368 | - if(is_a($this->xPlugin->response(), Response::class)) |
|
| 365 | + if (is_a($this->xPlugin->response(), Response::class)) |
|
| 369 | 366 | { |
| 370 | 367 | /** @var Response */ |
| 371 | 368 | $xResponse = $this->xPlugin->response(); |
@@ -388,13 +385,13 @@ discard block |
||
| 388 | 385 | */ |
| 389 | 386 | public function render(JsExpr $xCall, string $sWrapperId = ''): void |
| 390 | 387 | { |
| 391 | - if(($xFunc = $xCall->func()) === null) |
|
| 388 | + if (($xFunc = $xCall->func()) === null) |
|
| 392 | 389 | { |
| 393 | 390 | return; |
| 394 | 391 | } |
| 395 | 392 | |
| 396 | 393 | $aParams = $this->showLinks(trim($sWrapperId)); |
| 397 | - if($aParams !== null) |
|
| 394 | + if ($aParams !== null) |
|
| 398 | 395 | { |
| 399 | 396 | // Set click handlers on the pagination links |
| 400 | 397 | $aParams['func'] = $xFunc->withPage()->jsonSerialize(); |
@@ -59,52 +59,52 @@ discard block |
||
| 59 | 59 | |
| 60 | 60 | class Paginator |
| 61 | 61 | { |
| 62 | - /** |
|
| 62 | +/** |
|
| 63 | 63 | * @var integer |
| 64 | 64 | */ |
| 65 | - protected $nItemsCount = 0; |
|
| 65 | +protected $nItemsCount = 0; |
|
| 66 | 66 | |
| 67 | - /** |
|
| 67 | +/** |
|
| 68 | 68 | * @var integer |
| 69 | 69 | */ |
| 70 | - protected $nPagesCount = 0; |
|
| 70 | +protected $nPagesCount = 0; |
|
| 71 | 71 | |
| 72 | - /** |
|
| 72 | +/** |
|
| 73 | 73 | * @var integer |
| 74 | 74 | */ |
| 75 | - protected $nItemsPerPage = 0; |
|
| 75 | +protected $nItemsPerPage = 0; |
|
| 76 | 76 | |
| 77 | - /** |
|
| 77 | +/** |
|
| 78 | 78 | * @var integer |
| 79 | 79 | */ |
| 80 | - protected $nPageNumber = 0; |
|
| 80 | +protected $nPageNumber = 0; |
|
| 81 | 81 | |
| 82 | - /** |
|
| 82 | +/** |
|
| 83 | 83 | * @var integer |
| 84 | 84 | */ |
| 85 | - protected $nMaxPages = 10; |
|
| 85 | +protected $nMaxPages = 10; |
|
| 86 | 86 | |
| 87 | - /** |
|
| 87 | +/** |
|
| 88 | 88 | * @var string |
| 89 | 89 | */ |
| 90 | - protected $sPreviousText = '«'; |
|
| 90 | +protected $sPreviousText = '«'; |
|
| 91 | 91 | |
| 92 | - /** |
|
| 92 | +/** |
|
| 93 | 93 | * @var string |
| 94 | 94 | */ |
| 95 | - protected $sNextText = '»'; |
|
| 95 | +protected $sNextText = '»'; |
|
| 96 | 96 | |
| 97 | - /** |
|
| 97 | +/** |
|
| 98 | 98 | * @var string |
| 99 | 99 | */ |
| 100 | - protected $sEllipsysText = '...'; |
|
| 100 | +protected $sEllipsysText = '...'; |
|
| 101 | 101 | |
| 102 | - /** |
|
| 102 | +/** |
|
| 103 | 103 | * @var PaginatorPlugin |
| 104 | 104 | */ |
| 105 | - private $xPlugin; |
|
| 105 | +private $xPlugin; |
|
| 106 | 106 | |
| 107 | - /** |
|
| 107 | +/** |
|
| 108 | 108 | * The constructor. |
| 109 | 109 | * |
| 110 | 110 | * @param PaginatorPlugin $xPlugin |
@@ -112,293 +112,293 @@ discard block |
||
| 112 | 112 | * @param int $nItemsPerPage The number of items per page |
| 113 | 113 | * @param int $nItemsCount The total number of items |
| 114 | 114 | */ |
| 115 | - public function __construct(PaginatorPlugin $xPlugin, int $nPageNumber, int $nItemsPerPage, int $nItemsCount) |
|
| 116 | - { |
|
| 117 | - $this->xPlugin = $xPlugin; |
|
| 118 | - $this->nItemsPerPage = $nItemsPerPage > 0 ? $nItemsPerPage : 0; |
|
| 119 | - $this->nItemsCount = $nItemsCount > 0 ? $nItemsCount : 0; |
|
| 120 | - $this->nPageNumber = $nPageNumber < 1 ? 1 : $nPageNumber; |
|
| 121 | - $this->updatePagesCount(); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 115 | +public function __construct(PaginatorPlugin $xPlugin, int $nPageNumber, int $nItemsPerPage, int $nItemsCount) |
|
| 116 | +{ |
|
| 117 | +$this->xPlugin = $xPlugin; |
|
| 118 | +$this->nItemsPerPage = $nItemsPerPage > 0 ? $nItemsPerPage : 0; |
|
| 119 | +$this->nItemsCount = $nItemsCount > 0 ? $nItemsCount : 0; |
|
| 120 | +$this->nPageNumber = $nPageNumber < 1 ? 1 : $nPageNumber; |
|
| 121 | +$this->updatePagesCount(); |
|
| 122 | +} |
|
| 123 | + |
|
| 124 | +/** |
|
| 125 | 125 | * Update the number of pages |
| 126 | 126 | * |
| 127 | 127 | * @return Paginator |
| 128 | 128 | */ |
| 129 | - private function updatePagesCount(): Paginator |
|
| 130 | - { |
|
| 131 | - $this->nPagesCount = ($this->nItemsPerPage === 0 ? 0 : |
|
| 132 | - (int)ceil($this->nItemsCount / $this->nItemsPerPage)); |
|
| 133 | - if($this->nPageNumber > $this->nPagesCount) |
|
| 134 | - { |
|
| 135 | - $this->nPageNumber = $this->nPagesCount; |
|
| 136 | - } |
|
| 137 | - return $this; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 129 | +private function updatePagesCount(): Paginator |
|
| 130 | +{ |
|
| 131 | +$this->nPagesCount = ($this->nItemsPerPage === 0 ? 0 : |
|
| 132 | +(int)ceil($this->nItemsCount / $this->nItemsPerPage)); |
|
| 133 | +if($this->nPageNumber > $this->nPagesCount) |
|
| 134 | +{ |
|
| 135 | +$this->nPageNumber = $this->nPagesCount; |
|
| 136 | +} |
|
| 137 | +return $this; |
|
| 138 | +} |
|
| 139 | + |
|
| 140 | +/** |
|
| 141 | 141 | * Set the text for the previous page link |
| 142 | 142 | * |
| 143 | 143 | * @param string $sText The text for the previous page link |
| 144 | 144 | * |
| 145 | 145 | * @return Paginator |
| 146 | 146 | */ |
| 147 | - public function setPreviousText(string $sText): Paginator |
|
| 148 | - { |
|
| 149 | - $this->sPreviousText = $sText; |
|
| 150 | - return $this; |
|
| 151 | - } |
|
| 147 | +public function setPreviousText(string $sText): Paginator |
|
| 148 | +{ |
|
| 149 | +$this->sPreviousText = $sText; |
|
| 150 | +return $this; |
|
| 151 | +} |
|
| 152 | 152 | |
| 153 | - /** |
|
| 153 | +/** |
|
| 154 | 154 | * Set the text for the next page link |
| 155 | 155 | * |
| 156 | 156 | * @param string $sText The text for the previous page link |
| 157 | 157 | * |
| 158 | 158 | * @return Paginator |
| 159 | 159 | */ |
| 160 | - public function setNextText(string $sText): Paginator |
|
| 161 | - { |
|
| 162 | - $this->sNextText = $sText; |
|
| 163 | - return $this; |
|
| 164 | - } |
|
| 160 | +public function setNextText(string $sText): Paginator |
|
| 161 | +{ |
|
| 162 | +$this->sNextText = $sText; |
|
| 163 | +return $this; |
|
| 164 | +} |
|
| 165 | 165 | |
| 166 | - /** |
|
| 166 | +/** |
|
| 167 | 167 | * Set the max number of pages to show |
| 168 | 168 | * |
| 169 | 169 | * @param int $nMaxPages The max number of pages to show |
| 170 | 170 | * |
| 171 | 171 | * @return Paginator |
| 172 | 172 | */ |
| 173 | - public function setMaxPages(int $nMaxPages): Paginator |
|
| 174 | - { |
|
| 175 | - $this->nMaxPages = max($nMaxPages, 4); |
|
| 176 | - return $this; |
|
| 177 | - } |
|
| 173 | +public function setMaxPages(int $nMaxPages): Paginator |
|
| 174 | +{ |
|
| 175 | +$this->nMaxPages = max($nMaxPages, 4); |
|
| 176 | +return $this; |
|
| 177 | +} |
|
| 178 | 178 | |
| 179 | - /** |
|
| 179 | +/** |
|
| 180 | 180 | * Get the previous page data. |
| 181 | 181 | * |
| 182 | 182 | * @return Page |
| 183 | 183 | */ |
| 184 | - protected function getPrevPage(): Page |
|
| 185 | - { |
|
| 186 | - return $this->nPageNumber <= 1 ? |
|
| 187 | - new Page('disabled', $this->sPreviousText, 0) : |
|
| 188 | - new Page('enabled', $this->sPreviousText, $this->nPageNumber - 1); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 184 | +protected function getPrevPage(): Page |
|
| 185 | +{ |
|
| 186 | +return $this->nPageNumber <= 1 ? |
|
| 187 | +new Page('disabled', $this->sPreviousText, 0) : |
|
| 188 | +new Page('enabled', $this->sPreviousText, $this->nPageNumber - 1); |
|
| 189 | +} |
|
| 190 | + |
|
| 191 | +/** |
|
| 192 | 192 | * Get the next page data. |
| 193 | 193 | * |
| 194 | 194 | * @return Page |
| 195 | 195 | */ |
| 196 | - protected function getNextPage(): Page |
|
| 197 | - { |
|
| 198 | - return $this->nPageNumber >= $this->nPagesCount ? |
|
| 199 | - new Page('disabled', $this->sNextText, 0) : |
|
| 200 | - new Page('enabled', $this->sNextText, $this->nPageNumber + 1); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 196 | +protected function getNextPage(): Page |
|
| 197 | +{ |
|
| 198 | +return $this->nPageNumber >= $this->nPagesCount ? |
|
| 199 | +new Page('disabled', $this->sNextText, 0) : |
|
| 200 | +new Page('enabled', $this->sNextText, $this->nPageNumber + 1); |
|
| 201 | +} |
|
| 202 | + |
|
| 203 | +/** |
|
| 204 | 204 | * Get a page data. |
| 205 | 205 | * |
| 206 | 206 | * @param integer $nNumber The page number |
| 207 | 207 | * |
| 208 | 208 | * @return Page |
| 209 | 209 | */ |
| 210 | - protected function getPage(int $nNumber): Page |
|
| 211 | - { |
|
| 212 | - if($nNumber < 1) |
|
| 213 | - { |
|
| 214 | - return new Page('disabled', $this->sEllipsysText, 0); |
|
| 215 | - } |
|
| 216 | - $sType = ($nNumber === $this->nPageNumber ? 'current' : 'enabled'); |
|
| 217 | - return new Page($sType, "$nNumber", $nNumber); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 210 | +protected function getPage(int $nNumber): Page |
|
| 211 | +{ |
|
| 212 | +if($nNumber < 1) |
|
| 213 | +{ |
|
| 214 | +return new Page('disabled', $this->sEllipsysText, 0); |
|
| 215 | +} |
|
| 216 | +$sType = ($nNumber === $this->nPageNumber ? 'current' : 'enabled'); |
|
| 217 | +return new Page($sType, "$nNumber", $nNumber); |
|
| 218 | +} |
|
| 219 | + |
|
| 220 | +/** |
|
| 221 | 221 | * Get the array of page numbers to be printed. |
| 222 | 222 | * |
| 223 | 223 | * Example: [1, 0, 4, 5, 6, 0, 10] |
| 224 | 224 | * |
| 225 | 225 | * @return array |
| 226 | 226 | */ |
| 227 | - protected function getPageNumbers(): array |
|
| 228 | - { |
|
| 229 | - $aPageNumbers = []; |
|
| 230 | - |
|
| 231 | - if($this->nPagesCount <= $this->nMaxPages) |
|
| 232 | - { |
|
| 233 | - for($i = 0; $i < $this->nPagesCount; $i++) |
|
| 234 | - { |
|
| 235 | - $aPageNumbers[] = $i + 1; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - return $aPageNumbers; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - // Determine the sliding range, centered around the current page. |
|
| 242 | - $nNumAdjacents = (int)floor(($this->nMaxPages - 4) / 2); |
|
| 243 | - |
|
| 244 | - $nSlidingStart = 1; |
|
| 245 | - $nSlidingEndOffset = $nNumAdjacents + 3 - $this->nPageNumber; |
|
| 246 | - if($nSlidingEndOffset < 0) |
|
| 247 | - { |
|
| 248 | - $nSlidingStart = $this->nPageNumber - $nNumAdjacents; |
|
| 249 | - $nSlidingEndOffset = 0; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - $nSlidingEnd = $this->nPagesCount; |
|
| 253 | - $nSlidingStartOffset = $this->nPageNumber + $nNumAdjacents + 2 - $this->nPagesCount; |
|
| 254 | - if($nSlidingStartOffset < 0) |
|
| 255 | - { |
|
| 256 | - $nSlidingEnd = $this->nPageNumber + $nNumAdjacents; |
|
| 257 | - $nSlidingStartOffset = 0; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - // Build the list of page numbers. |
|
| 261 | - if($nSlidingStart > 1) |
|
| 262 | - { |
|
| 263 | - $aPageNumbers[] = 1; |
|
| 264 | - $aPageNumbers[] = 0; // Ellipsys; |
|
| 265 | - } |
|
| 266 | - for($i = $nSlidingStart - $nSlidingStartOffset; $i <= $nSlidingEnd + $nSlidingEndOffset; $i++) |
|
| 267 | - { |
|
| 268 | - $aPageNumbers[] = $i; |
|
| 269 | - } |
|
| 270 | - if($nSlidingEnd < $this->nPagesCount) |
|
| 271 | - { |
|
| 272 | - $aPageNumbers[] = 0; // Ellipsys; |
|
| 273 | - $aPageNumbers[] = $this->nPagesCount; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - return $aPageNumbers; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 227 | +protected function getPageNumbers(): array |
|
| 228 | +{ |
|
| 229 | +$aPageNumbers = []; |
|
| 230 | + |
|
| 231 | +if($this->nPagesCount <= $this->nMaxPages) |
|
| 232 | +{ |
|
| 233 | +for($i = 0; $i < $this->nPagesCount; $i++) |
|
| 234 | +{ |
|
| 235 | + $aPageNumbers[] = $i + 1; |
|
| 236 | +} |
|
| 237 | + |
|
| 238 | +return $aPageNumbers; |
|
| 239 | +} |
|
| 240 | + |
|
| 241 | +// Determine the sliding range, centered around the current page. |
|
| 242 | +$nNumAdjacents = (int)floor(($this->nMaxPages - 4) / 2); |
|
| 243 | + |
|
| 244 | +$nSlidingStart = 1; |
|
| 245 | +$nSlidingEndOffset = $nNumAdjacents + 3 - $this->nPageNumber; |
|
| 246 | +if($nSlidingEndOffset < 0) |
|
| 247 | +{ |
|
| 248 | +$nSlidingStart = $this->nPageNumber - $nNumAdjacents; |
|
| 249 | +$nSlidingEndOffset = 0; |
|
| 250 | +} |
|
| 251 | + |
|
| 252 | +$nSlidingEnd = $this->nPagesCount; |
|
| 253 | +$nSlidingStartOffset = $this->nPageNumber + $nNumAdjacents + 2 - $this->nPagesCount; |
|
| 254 | +if($nSlidingStartOffset < 0) |
|
| 255 | +{ |
|
| 256 | +$nSlidingEnd = $this->nPageNumber + $nNumAdjacents; |
|
| 257 | +$nSlidingStartOffset = 0; |
|
| 258 | +} |
|
| 259 | + |
|
| 260 | +// Build the list of page numbers. |
|
| 261 | +if($nSlidingStart > 1) |
|
| 262 | +{ |
|
| 263 | +$aPageNumbers[] = 1; |
|
| 264 | +$aPageNumbers[] = 0; // Ellipsys; |
|
| 265 | +} |
|
| 266 | +for($i = $nSlidingStart - $nSlidingStartOffset; $i <= $nSlidingEnd + $nSlidingEndOffset; $i++) |
|
| 267 | +{ |
|
| 268 | +$aPageNumbers[] = $i; |
|
| 269 | +} |
|
| 270 | +if($nSlidingEnd < $this->nPagesCount) |
|
| 271 | +{ |
|
| 272 | +$aPageNumbers[] = 0; // Ellipsys; |
|
| 273 | +$aPageNumbers[] = $this->nPagesCount; |
|
| 274 | +} |
|
| 275 | + |
|
| 276 | +return $aPageNumbers; |
|
| 277 | +} |
|
| 278 | + |
|
| 279 | +/** |
|
| 280 | 280 | * Get the current page number. |
| 281 | 281 | * |
| 282 | 282 | * @return int |
| 283 | 283 | */ |
| 284 | - public function currentPage(): int |
|
| 285 | - { |
|
| 286 | - return $this->nPageNumber; |
|
| 287 | - } |
|
| 284 | +public function currentPage(): int |
|
| 285 | +{ |
|
| 286 | +return $this->nPageNumber; |
|
| 287 | +} |
|
| 288 | 288 | |
| 289 | - /** |
|
| 289 | +/** |
|
| 290 | 290 | * Get the links (pages raw data). |
| 291 | 291 | * |
| 292 | 292 | * @return array<Page> |
| 293 | 293 | */ |
| 294 | - public function pages(): array |
|
| 295 | - { |
|
| 296 | - if($this->nPagesCount < 2) |
|
| 297 | - { |
|
| 298 | - return []; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - $aPageNumbers = $this->getPageNumbers(); |
|
| 302 | - $aPages = [$this->getPrevPage()]; |
|
| 303 | - array_walk($aPageNumbers, function($nNumber) use(&$aPages) { |
|
| 304 | - $aPages[] = $this->getPage($nNumber); |
|
| 305 | - }); |
|
| 306 | - $aPages[] = $this->getNextPage(); |
|
| 307 | - |
|
| 308 | - return $aPages; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 294 | +public function pages(): array |
|
| 295 | +{ |
|
| 296 | +if($this->nPagesCount < 2) |
|
| 297 | +{ |
|
| 298 | +return []; |
|
| 299 | +} |
|
| 300 | + |
|
| 301 | +$aPageNumbers = $this->getPageNumbers(); |
|
| 302 | +$aPages = [$this->getPrevPage()]; |
|
| 303 | +array_walk($aPageNumbers, function($nNumber) use(&$aPages) { |
|
| 304 | +$aPages[] = $this->getPage($nNumber); |
|
| 305 | +}); |
|
| 306 | +$aPages[] = $this->getNextPage(); |
|
| 307 | + |
|
| 308 | +return $aPages; |
|
| 309 | +} |
|
| 310 | + |
|
| 311 | +/** |
|
| 312 | 312 | * Call a closure that will receive the page number as parameter. |
| 313 | 313 | * |
| 314 | 314 | * @param Closure $fPageCallback |
| 315 | 315 | * |
| 316 | 316 | * @return Paginator |
| 317 | 317 | */ |
| 318 | - public function page(Closure $fPageCallback): Paginator |
|
| 319 | - { |
|
| 320 | - $fPageCallback($this->nPageNumber); |
|
| 318 | +public function page(Closure $fPageCallback): Paginator |
|
| 319 | +{ |
|
| 320 | +$fPageCallback($this->nPageNumber); |
|
| 321 | 321 | |
| 322 | - return $this; |
|
| 323 | - } |
|
| 322 | +return $this; |
|
| 323 | +} |
|
| 324 | 324 | |
| 325 | - /** |
|
| 325 | +/** |
|
| 326 | 326 | * Call a closure that will receive the pagination offset as parameter. |
| 327 | 327 | * |
| 328 | 328 | * @param Closure $fOffsetCallback |
| 329 | 329 | * |
| 330 | 330 | * @return Paginator |
| 331 | 331 | */ |
| 332 | - public function offset(Closure $fOffsetCallback): Paginator |
|
| 333 | - { |
|
| 334 | - $fOffsetCallback(($this->nPageNumber - 1) * $this->nItemsPerPage); |
|
| 332 | +public function offset(Closure $fOffsetCallback): Paginator |
|
| 333 | +{ |
|
| 334 | +$fOffsetCallback(($this->nPageNumber - 1) * $this->nItemsPerPage); |
|
| 335 | 335 | |
| 336 | - return $this; |
|
| 337 | - } |
|
| 336 | +return $this; |
|
| 337 | +} |
|
| 338 | 338 | |
| 339 | - /** |
|
| 339 | +/** |
|
| 340 | 340 | * Show the pagination links |
| 341 | 341 | * |
| 342 | 342 | * @return string |
| 343 | 343 | */ |
| 344 | - private function renderLinks(): string |
|
| 345 | - { |
|
| 346 | - $aPages = $this->pages(); |
|
| 347 | - if(count($aPages) === 0) |
|
| 348 | - { |
|
| 349 | - return ''; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - $xPrevPage = array_shift($aPages); // The first entry in the array |
|
| 353 | - $xNextPage = array_pop($aPages); // The last entry in the array |
|
| 354 | - return $this->xPlugin->renderer()->render($aPages, $xPrevPage, $xNextPage); |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 344 | +private function renderLinks(): string |
|
| 345 | +{ |
|
| 346 | +$aPages = $this->pages(); |
|
| 347 | +if(count($aPages) === 0) |
|
| 348 | +{ |
|
| 349 | +return ''; |
|
| 350 | +} |
|
| 351 | + |
|
| 352 | +$xPrevPage = array_shift($aPages); // The first entry in the array |
|
| 353 | +$xNextPage = array_pop($aPages); // The last entry in the array |
|
| 354 | +return $this->xPlugin->renderer()->render($aPages, $xPrevPage, $xNextPage); |
|
| 355 | +} |
|
| 356 | + |
|
| 357 | +/** |
|
| 358 | 358 | * Show the pagination links |
| 359 | 359 | * |
| 360 | 360 | * @param string $sWrapperId |
| 361 | 361 | * |
| 362 | 362 | * @return array|null |
| 363 | 363 | */ |
| 364 | - private function showLinks(string $sWrapperId): ?array |
|
| 365 | - { |
|
| 366 | - $sHtml = $this->renderLinks(); |
|
| 367 | - // The HTML code must always be displayed, even if it is empty. |
|
| 368 | - if(is_a($this->xPlugin->response(), Response::class)) |
|
| 369 | - { |
|
| 370 | - /** @var Response */ |
|
| 371 | - $xResponse = $this->xPlugin->response(); |
|
| 372 | - $xResponse->html($sWrapperId, $sHtml); |
|
| 373 | - return !$sHtml ? null : ['id' => $sWrapperId]; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - // The wrapper id is not needed for the NodeResponse |
|
| 377 | - /** @var NodeResponse */ |
|
| 378 | - $xResponse = $this->xPlugin->response(); |
|
| 379 | - $xResponse->html($sHtml); |
|
| 380 | - return !$sHtml ? null : []; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - /** |
|
| 364 | +private function showLinks(string $sWrapperId): ?array |
|
| 365 | +{ |
|
| 366 | +$sHtml = $this->renderLinks(); |
|
| 367 | +// The HTML code must always be displayed, even if it is empty. |
|
| 368 | +if(is_a($this->xPlugin->response(), Response::class)) |
|
| 369 | +{ |
|
| 370 | +/** @var Response */ |
|
| 371 | +$xResponse = $this->xPlugin->response(); |
|
| 372 | +$xResponse->html($sWrapperId, $sHtml); |
|
| 373 | +return !$sHtml ? null : ['id' => $sWrapperId]; |
|
| 374 | +} |
|
| 375 | + |
|
| 376 | +// The wrapper id is not needed for the NodeResponse |
|
| 377 | +/** @var NodeResponse */ |
|
| 378 | +$xResponse = $this->xPlugin->response(); |
|
| 379 | +$xResponse->html($sHtml); |
|
| 380 | +return !$sHtml ? null : []; |
|
| 381 | +} |
|
| 382 | + |
|
| 383 | +/** |
|
| 384 | 384 | * @param JsExpr $xCall |
| 385 | 385 | * @param string $sWrapperId |
| 386 | 386 | * |
| 387 | 387 | * @return void |
| 388 | 388 | */ |
| 389 | - public function render(JsExpr $xCall, string $sWrapperId = ''): void |
|
| 390 | - { |
|
| 391 | - if(($xFunc = $xCall->func()) === null) |
|
| 392 | - { |
|
| 393 | - return; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - $aParams = $this->showLinks(trim($sWrapperId)); |
|
| 397 | - if($aParams !== null) |
|
| 398 | - { |
|
| 399 | - // Set click handlers on the pagination links |
|
| 400 | - $aParams['func'] = $xFunc->withPage()->jsonSerialize(); |
|
| 401 | - $this->xPlugin->addCommand('pg.paginate', $aParams); |
|
| 402 | - } |
|
| 403 | - } |
|
| 389 | +public function render(JsExpr $xCall, string $sWrapperId = ''): void |
|
| 390 | +{ |
|
| 391 | +if(($xFunc = $xCall->func()) === null) |
|
| 392 | +{ |
|
| 393 | +return; |
|
| 394 | +} |
|
| 395 | + |
|
| 396 | +$aParams = $this->showLinks(trim($sWrapperId)); |
|
| 397 | +if($aParams !== null) |
|
| 398 | +{ |
|
| 399 | +// Set click handlers on the pagination links |
|
| 400 | +$aParams['func'] = $xFunc->withPage()->jsonSerialize(); |
|
| 401 | +$this->xPlugin->addCommand('pg.paginate', $aParams); |
|
| 402 | +} |
|
| 403 | +} |
|
| 404 | 404 | } |
@@ -53,7 +53,7 @@ |
||
| 53 | 53 | { |
| 54 | 54 | $this->sDirectory = ''; |
| 55 | 55 | $this->sExtension = ''; |
| 56 | - if(isset($this->aDirectories[$sNamespace])) |
|
| 56 | + if (isset($this->aDirectories[$sNamespace])) |
|
| 57 | 57 | { |
| 58 | 58 | // Make sure there's only one '/' at the end of the string |
| 59 | 59 | $this->sDirectory = rtrim($this->aDirectories[$sNamespace]['path'], '/') . '/'; |
@@ -7,28 +7,28 @@ discard block |
||
| 7 | 7 | |
| 8 | 8 | trait ViewTrait |
| 9 | 9 | { |
| 10 | - /** |
|
| 10 | +/** |
|
| 11 | 11 | * The template directories |
| 12 | 12 | * |
| 13 | 13 | * @var array |
| 14 | 14 | */ |
| 15 | - protected $aDirectories = []; |
|
| 15 | +protected $aDirectories = []; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 17 | +/** |
|
| 18 | 18 | * The directory of the current template |
| 19 | 19 | * |
| 20 | 20 | * @var string |
| 21 | 21 | */ |
| 22 | - protected $sDirectory = ''; |
|
| 22 | +protected $sDirectory = ''; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 24 | +/** |
|
| 25 | 25 | * The extension of the current template |
| 26 | 26 | * |
| 27 | 27 | * @var string |
| 28 | 28 | */ |
| 29 | - protected $sExtension = ''; |
|
| 29 | +protected $sExtension = ''; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 31 | +/** |
|
| 32 | 32 | * Add a namespace to the view renderer |
| 33 | 33 | * |
| 34 | 34 | * @param string $sNamespace The namespace name |
@@ -37,28 +37,28 @@ discard block |
||
| 37 | 37 | * |
| 38 | 38 | * @return void |
| 39 | 39 | */ |
| 40 | - public function addNamespace(string $sNamespace, string $sDirectory, string $sExtension = ''): void |
|
| 41 | - { |
|
| 42 | - $this->aDirectories[$sNamespace] = ['path' => $sDirectory, 'ext' => $sExtension]; |
|
| 43 | - } |
|
| 40 | +public function addNamespace(string $sNamespace, string $sDirectory, string $sExtension = ''): void |
|
| 41 | +{ |
|
| 42 | +$this->aDirectories[$sNamespace] = ['path' => $sDirectory, 'ext' => $sExtension]; |
|
| 43 | +} |
|
| 44 | 44 | |
| 45 | - /** |
|
| 45 | +/** |
|
| 46 | 46 | * Set the namespace of the template being rendered |
| 47 | 47 | * |
| 48 | 48 | * @param string $sNamespace The namespace name |
| 49 | 49 | * |
| 50 | 50 | * @return void |
| 51 | 51 | */ |
| 52 | - public function setCurrentNamespace(string $sNamespace): void |
|
| 53 | - { |
|
| 54 | - $this->sDirectory = ''; |
|
| 55 | - $this->sExtension = ''; |
|
| 56 | - if(isset($this->aDirectories[$sNamespace])) |
|
| 57 | - { |
|
| 58 | - // Make sure there's only one '/' at the end of the string |
|
| 59 | - $this->sDirectory = rtrim($this->aDirectories[$sNamespace]['path'], '/') . '/'; |
|
| 60 | - // Make sure there's only one '.' at the beginning of the string |
|
| 61 | - $this->sExtension = '.' . ltrim($this->aDirectories[$sNamespace]['ext'], '.'); |
|
| 62 | - } |
|
| 63 | - } |
|
| 52 | +public function setCurrentNamespace(string $sNamespace): void |
|
| 53 | +{ |
|
| 54 | +$this->sDirectory = ''; |
|
| 55 | +$this->sExtension = ''; |
|
| 56 | +if(isset($this->aDirectories[$sNamespace])) |
|
| 57 | +{ |
|
| 58 | +// Make sure there's only one '/' at the end of the string |
|
| 59 | +$this->sDirectory = rtrim($this->aDirectories[$sNamespace]['path'], '/') . '/'; |
|
| 60 | +// Make sure there's only one '.' at the beginning of the string |
|
| 61 | +$this->sExtension = '.' . ltrim($this->aDirectories[$sNamespace]['ext'], '.'); |
|
| 62 | +} |
|
| 63 | +} |
|
| 64 | 64 | } |
@@ -59,7 +59,7 @@ |
||
| 59 | 59 | $sViewName = $store->getViewName(); |
| 60 | 60 | $sNamespace = $store->getNamespace(); |
| 61 | 61 | // In this view renderer, the namespace must always be prepended to the view name. |
| 62 | - if(substr($sViewName, 0, strlen($sNamespace) + 2) != $sNamespace . '::') |
|
| 62 | + if (substr($sViewName, 0, strlen($sNamespace) + 2) != $sNamespace . '::') |
|
| 63 | 63 | { |
| 64 | 64 | $sViewName = $sNamespace . '::' . $sViewName; |
| 65 | 65 | } |
@@ -18,22 +18,22 @@ discard block |
||
| 18 | 18 | |
| 19 | 19 | class TemplateView implements ViewInterface |
| 20 | 20 | { |
| 21 | - /** |
|
| 21 | +/** |
|
| 22 | 22 | * The Jaxon template engine |
| 23 | 23 | * |
| 24 | 24 | * @var TemplateEngine |
| 25 | 25 | */ |
| 26 | - protected $xTemplateEngine; |
|
| 26 | +protected $xTemplateEngine; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 28 | +/** |
|
| 29 | 29 | * The class constructor |
| 30 | 30 | */ |
| 31 | - public function __construct(TemplateEngine $xTemplateEngine) |
|
| 32 | - { |
|
| 33 | - $this->xTemplateEngine = $xTemplateEngine; |
|
| 34 | - } |
|
| 31 | +public function __construct(TemplateEngine $xTemplateEngine) |
|
| 32 | +{ |
|
| 33 | +$this->xTemplateEngine = $xTemplateEngine; |
|
| 34 | +} |
|
| 35 | 35 | |
| 36 | - /** |
|
| 36 | +/** |
|
| 37 | 37 | * Add a namespace to this view renderer |
| 38 | 38 | * |
| 39 | 39 | * @param string $sNamespace The namespace name |
@@ -42,28 +42,28 @@ discard block |
||
| 42 | 42 | * |
| 43 | 43 | * @return void |
| 44 | 44 | */ |
| 45 | - public function addNamespace(string $sNamespace, string $sDirectory, string $sExtension = ''): void |
|
| 46 | - { |
|
| 47 | - $this->xTemplateEngine->addNamespace($sNamespace, $sDirectory, $sExtension); |
|
| 48 | - } |
|
| 45 | +public function addNamespace(string $sNamespace, string $sDirectory, string $sExtension = ''): void |
|
| 46 | +{ |
|
| 47 | +$this->xTemplateEngine->addNamespace($sNamespace, $sDirectory, $sExtension); |
|
| 48 | +} |
|
| 49 | 49 | |
| 50 | - /** |
|
| 50 | +/** |
|
| 51 | 51 | * Render a view |
| 52 | 52 | * |
| 53 | 53 | * @param Store $store A store populated with the view data |
| 54 | 54 | * |
| 55 | 55 | * @return string The string representation of the view |
| 56 | 56 | */ |
| 57 | - public function render(Store $store): string |
|
| 58 | - { |
|
| 59 | - $sViewName = $store->getViewName(); |
|
| 60 | - $sNamespace = $store->getNamespace(); |
|
| 61 | - // In this view renderer, the namespace must always be prepended to the view name. |
|
| 62 | - if(substr($sViewName, 0, strlen($sNamespace) + 2) != $sNamespace . '::') |
|
| 63 | - { |
|
| 64 | - $sViewName = $sNamespace . '::' . $sViewName; |
|
| 65 | - } |
|
| 66 | - // Render the template |
|
| 67 | - return trim($this->xTemplateEngine->render($sViewName, $store->getViewData()), " \t\n"); |
|
| 68 | - } |
|
| 57 | +public function render(Store $store): string |
|
| 58 | +{ |
|
| 59 | +$sViewName = $store->getViewName(); |
|
| 60 | +$sNamespace = $store->getNamespace(); |
|
| 61 | +// In this view renderer, the namespace must always be prepended to the view name. |
|
| 62 | +if(substr($sViewName, 0, strlen($sNamespace) + 2) != $sNamespace . '::') |
|
| 63 | +{ |
|
| 64 | +$sViewName = $sNamespace . '::' . $sViewName; |
|
| 65 | +} |
|
| 66 | +// Render the template |
|
| 67 | +return trim($this->xTemplateEngine->render($sViewName, $store->getViewData()), " \t\n"); |
|
| 68 | +} |
|
| 69 | 69 | } |
@@ -59,7 +59,7 @@ |
||
| 59 | 59 | */ |
| 60 | 60 | public function on(string $event, JsExpr $xJsExpr): self |
| 61 | 61 | { |
| 62 | - if($this->sSelector === '') |
|
| 62 | + if ($this->sSelector === '') |
|
| 63 | 63 | { |
| 64 | 64 | return $this; |
| 65 | 65 | } |
@@ -23,33 +23,33 @@ discard block |
||
| 23 | 23 | |
| 24 | 24 | class EventAttr |
| 25 | 25 | { |
| 26 | - /** |
|
| 26 | +/** |
|
| 27 | 27 | * @var array |
| 28 | 28 | */ |
| 29 | - private array $aHandlers = []; |
|
| 29 | +private array $aHandlers = []; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 31 | +/** |
|
| 32 | 32 | * The constructor |
| 33 | 33 | * |
| 34 | 34 | * @param string $sSelector |
| 35 | 35 | */ |
| 36 | - public function __construct(private string $sSelector) |
|
| 37 | - {} |
|
| 36 | +public function __construct(private string $sSelector) |
|
| 37 | +{} |
|
| 38 | 38 | |
| 39 | - /** |
|
| 39 | +/** |
|
| 40 | 40 | * Set a selector for the next event handler |
| 41 | 41 | * |
| 42 | 42 | * @param string $sSelector |
| 43 | 43 | * |
| 44 | 44 | * @return self |
| 45 | 45 | */ |
| 46 | - public function select(string $sSelector): self |
|
| 47 | - { |
|
| 48 | - $this->sSelector = trim($sSelector); |
|
| 49 | - return $this; |
|
| 50 | - } |
|
| 46 | +public function select(string $sSelector): self |
|
| 47 | +{ |
|
| 48 | +$this->sSelector = trim($sSelector); |
|
| 49 | +return $this; |
|
| 50 | +} |
|
| 51 | 51 | |
| 52 | - /** |
|
| 52 | +/** |
|
| 53 | 53 | * Set an event handler with the "on" keyword |
| 54 | 54 | * |
| 55 | 55 | * @param string $event |
@@ -57,43 +57,43 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * @return self |
| 59 | 59 | */ |
| 60 | - public function on(string $event, JsExpr $xJsExpr): self |
|
| 61 | - { |
|
| 62 | - if($this->sSelector === '') |
|
| 63 | - { |
|
| 64 | - return $this; |
|
| 65 | - } |
|
| 60 | +public function on(string $event, JsExpr $xJsExpr): self |
|
| 61 | +{ |
|
| 62 | +if($this->sSelector === '') |
|
| 63 | +{ |
|
| 64 | +return $this; |
|
| 65 | +} |
|
| 66 | 66 | |
| 67 | - $this->aHandlers[] = [ |
|
| 68 | - 'select' => $this->sSelector, |
|
| 69 | - 'event' => trim($event), |
|
| 70 | - 'handler' => $xJsExpr, |
|
| 71 | - ]; |
|
| 72 | - $this->sSelector = ''; |
|
| 73 | - return $this; |
|
| 74 | - } |
|
| 67 | +$this->aHandlers[] = [ |
|
| 68 | +'select' => $this->sSelector, |
|
| 69 | +'event' => trim($event), |
|
| 70 | +'handler' => $xJsExpr, |
|
| 71 | +]; |
|
| 72 | +$this->sSelector = ''; |
|
| 73 | +return $this; |
|
| 74 | +} |
|
| 75 | 75 | |
| 76 | - /** |
|
| 76 | +/** |
|
| 77 | 77 | * Shortcut to set a click event handler |
| 78 | 78 | * |
| 79 | 79 | * @param JsExpr $xJsExpr |
| 80 | 80 | * |
| 81 | 81 | * @return self |
| 82 | 82 | */ |
| 83 | - public function click(JsExpr $xJsExpr): self |
|
| 84 | - { |
|
| 85 | - return $this->on('click', $xJsExpr); |
|
| 86 | - } |
|
| 83 | +public function click(JsExpr $xJsExpr): self |
|
| 84 | +{ |
|
| 85 | +return $this->on('click', $xJsExpr); |
|
| 86 | +} |
|
| 87 | 87 | |
| 88 | - /** |
|
| 88 | +/** |
|
| 89 | 89 | * Convert to string. |
| 90 | 90 | * |
| 91 | 91 | * @return string |
| 92 | 92 | */ |
| 93 | - public function __toString(): string |
|
| 94 | - { |
|
| 95 | - // No output if no handler is defined. |
|
| 96 | - return count($this->aHandlers) === 0 ? '' : 'jxn-event="' . |
|
| 97 | - htmlentities(json_encode($this->aHandlers)) . '"'; |
|
| 98 | - } |
|
| 93 | +public function __toString(): string |
|
| 94 | +{ |
|
| 95 | +// No output if no handler is defined. |
|
| 96 | +return count($this->aHandlers) === 0 ? '' : 'jxn-event="' . |
|
| 97 | +htmlentities(json_encode($this->aHandlers)) . '"'; |
|
| 98 | +} |
|
| 99 | 99 | } |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | public function html(JxnCall $xJsCall): string |
| 55 | 55 | { |
| 56 | 56 | $sClassName = $xJsCall->_class(); |
| 57 | - if(!$sClassName) |
|
| 57 | + if (!$sClassName) |
|
| 58 | 58 | { |
| 59 | 59 | return ''; |
| 60 | 60 | } |
@@ -88,8 +88,7 @@ discard block |
||
| 88 | 88 | public function pagination(JxnCall $xJsCall): string |
| 89 | 89 | { |
| 90 | 90 | // The pagination is always rendered with the same Pagination component. |
| 91 | - $sComponent = $this->sPaginationComponent ?: |
|
| 92 | - ($this->sPaginationComponent = rq(Pagination::class)->_class()); |
|
| 91 | + $sComponent = $this->sPaginationComponent ?: ($this->sPaginationComponent = rq(Pagination::class)->_class()); |
|
| 93 | 92 | $sItem = $xJsCall->_class(); |
| 94 | 93 | return "jxn-bind=\"$sComponent\" jxn-item=\"$sItem\""; |
| 95 | 94 | } |
@@ -154,15 +153,14 @@ discard block |
||
| 154 | 153 | */ |
| 155 | 154 | private function setEventHandler(array $aHandler, ?EventAttr $xAttr = null): EventAttr|null |
| 156 | 155 | { |
| 157 | - if(!$this->eventIsValid($aHandler)) |
|
| 156 | + if (!$this->eventIsValid($aHandler)) |
|
| 158 | 157 | { |
| 159 | 158 | return $xAttr; |
| 160 | 159 | } |
| 161 | 160 | // The array content is valid. |
| 162 | 161 | [$sSelector, $sEvent, $xJsExpr] = $aHandler; |
| 163 | 162 | return !$xAttr ? |
| 164 | - $this->select($sSelector)->on($sEvent, $xJsExpr) : |
|
| 165 | - $xAttr->select($sSelector)->on($sEvent, $xJsExpr); |
|
| 163 | + $this->select($sSelector)->on($sEvent, $xJsExpr) : $xAttr->select($sSelector)->on($sEvent, $xJsExpr); |
|
| 166 | 164 | } |
| 167 | 165 | |
| 168 | 166 | /** |
@@ -207,8 +205,7 @@ discard block |
||
| 207 | 205 | }; |
| 208 | 206 | $sCode = trim($sCode); |
| 209 | 207 | |
| 210 | - return $sType !== 'ready' || $sCode === '' ? $sCode : |
|
| 211 | - // Call the ready code with the jaxon.dom.ready function. |
|
| 208 | + return $sType !== 'ready' || $sCode === '' ? $sCode : // Call the ready code with the jaxon.dom.ready function. |
|
| 212 | 209 | "jaxon.dom.ready(() => $sCode)"; |
| 213 | 210 | } |
| 214 | 211 | } |
@@ -31,40 +31,40 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | class HtmlAttrHelper |
| 33 | 33 | { |
| 34 | - /** |
|
| 34 | +/** |
|
| 35 | 35 | * @var string|null |
| 36 | 36 | */ |
| 37 | - private string|null $sPaginationComponent = null; |
|
| 37 | +private string|null $sPaginationComponent = null; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 39 | +/** |
|
| 40 | 40 | * The constructor |
| 41 | 41 | * |
| 42 | 42 | * @param ComponentContainer $cdi |
| 43 | 43 | */ |
| 44 | - public function __construct(protected ComponentContainer $cdi) |
|
| 45 | - {} |
|
| 44 | +public function __construct(protected ComponentContainer $cdi) |
|
| 45 | +{} |
|
| 46 | 46 | |
| 47 | - /** |
|
| 47 | +/** |
|
| 48 | 48 | * Get the component HTML code |
| 49 | 49 | * |
| 50 | 50 | * @param JxnCall $xJsCall |
| 51 | 51 | * |
| 52 | 52 | * @return string |
| 53 | 53 | */ |
| 54 | - public function html(JxnCall $xJsCall): string |
|
| 55 | - { |
|
| 56 | - $sClassName = $xJsCall->_class(); |
|
| 57 | - if(!$sClassName) |
|
| 58 | - { |
|
| 59 | - return ''; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - $xComponent = $this->cdi->makeComponent($sClassName); |
|
| 63 | - return is_a($xComponent, NodeComponent::class) ? |
|
| 64 | - (string)$xComponent->html() : ''; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 54 | +public function html(JxnCall $xJsCall): string |
|
| 55 | +{ |
|
| 56 | +$sClassName = $xJsCall->_class(); |
|
| 57 | +if(!$sClassName) |
|
| 58 | +{ |
|
| 59 | +return ''; |
|
| 60 | +} |
|
| 61 | + |
|
| 62 | +$xComponent = $this->cdi->makeComponent($sClassName); |
|
| 63 | +return is_a($xComponent, NodeComponent::class) ? |
|
| 64 | +(string)$xComponent->html() : ''; |
|
| 65 | +} |
|
| 66 | + |
|
| 67 | +/** |
|
| 68 | 68 | * Attach a component to a DOM node |
| 69 | 69 | * |
| 70 | 70 | * @param JxnCall $xJsCall |
@@ -72,41 +72,41 @@ discard block |
||
| 72 | 72 | * |
| 73 | 73 | * @return string |
| 74 | 74 | */ |
| 75 | - public function bind(JxnCall $xJsCall, string $item = ''): string |
|
| 76 | - { |
|
| 77 | - $item = trim($item); |
|
| 78 | - return 'jxn-bind="' . $xJsCall->_class() . (!$item ? '"' : '" jxn-item="' . $item . '"'); |
|
| 79 | - } |
|
| 75 | +public function bind(JxnCall $xJsCall, string $item = ''): string |
|
| 76 | +{ |
|
| 77 | +$item = trim($item); |
|
| 78 | +return 'jxn-bind="' . $xJsCall->_class() . (!$item ? '"' : '" jxn-item="' . $item . '"'); |
|
| 79 | +} |
|
| 80 | 80 | |
| 81 | - /** |
|
| 81 | +/** |
|
| 82 | 82 | * Attach the pagination component to a DOM node |
| 83 | 83 | * |
| 84 | 84 | * @param JxnCall $xJsCall |
| 85 | 85 | * |
| 86 | 86 | * @return string |
| 87 | 87 | */ |
| 88 | - public function pagination(JxnCall $xJsCall): string |
|
| 89 | - { |
|
| 90 | - // The pagination is always rendered with the same Pagination component. |
|
| 91 | - $sComponent = $this->sPaginationComponent ?: |
|
| 92 | - ($this->sPaginationComponent = rq(Pagination::class)->_class()); |
|
| 93 | - $sItem = $xJsCall->_class(); |
|
| 94 | - return "jxn-bind=\"$sComponent\" jxn-item=\"$sItem\""; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 88 | +public function pagination(JxnCall $xJsCall): string |
|
| 89 | +{ |
|
| 90 | +// The pagination is always rendered with the same Pagination component. |
|
| 91 | +$sComponent = $this->sPaginationComponent ?: |
|
| 92 | +($this->sPaginationComponent = rq(Pagination::class)->_class()); |
|
| 93 | +$sItem = $xJsCall->_class(); |
|
| 94 | +return "jxn-bind=\"$sComponent\" jxn-item=\"$sItem\""; |
|
| 95 | +} |
|
| 96 | + |
|
| 97 | +/** |
|
| 98 | 98 | * Set a selector for the next event handler |
| 99 | 99 | * |
| 100 | 100 | * @param string $sSelector |
| 101 | 101 | * |
| 102 | 102 | * @return EventAttr |
| 103 | 103 | */ |
| 104 | - public function select(string $sSelector): EventAttr |
|
| 105 | - { |
|
| 106 | - return new EventAttr($sSelector); |
|
| 107 | - } |
|
| 104 | +public function select(string $sSelector): EventAttr |
|
| 105 | +{ |
|
| 106 | +return new EventAttr($sSelector); |
|
| 107 | +} |
|
| 108 | 108 | |
| 109 | - /** |
|
| 109 | +/** |
|
| 110 | 110 | * Set an event handler |
| 111 | 111 | * |
| 112 | 112 | * @param string $event |
@@ -114,83 +114,83 @@ discard block |
||
| 114 | 114 | * |
| 115 | 115 | * @return string |
| 116 | 116 | */ |
| 117 | - public function on(string $event, JsExpr $xJsExpr): string |
|
| 118 | - { |
|
| 119 | - $event = trim($event); |
|
| 120 | - $sCall = htmlentities(json_encode($xJsExpr->jsonSerialize())); |
|
| 121 | - return "jxn-on=\"$event\" jxn-call=\"$sCall\""; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 117 | +public function on(string $event, JsExpr $xJsExpr): string |
|
| 118 | +{ |
|
| 119 | +$event = trim($event); |
|
| 120 | +$sCall = htmlentities(json_encode($xJsExpr->jsonSerialize())); |
|
| 121 | +return "jxn-on=\"$event\" jxn-call=\"$sCall\""; |
|
| 122 | +} |
|
| 123 | + |
|
| 124 | +/** |
|
| 125 | 125 | * Shortcut to set a click event handler |
| 126 | 126 | * |
| 127 | 127 | * @param JsExpr $xJsExpr |
| 128 | 128 | * |
| 129 | 129 | * @return string |
| 130 | 130 | */ |
| 131 | - public function click(JsExpr $xJsExpr): string |
|
| 132 | - { |
|
| 133 | - return $this->on('click', $xJsExpr); |
|
| 134 | - } |
|
| 131 | +public function click(JsExpr $xJsExpr): string |
|
| 132 | +{ |
|
| 133 | +return $this->on('click', $xJsExpr); |
|
| 134 | +} |
|
| 135 | 135 | |
| 136 | - /** |
|
| 136 | +/** |
|
| 137 | 137 | * @param array $aHandler |
| 138 | 138 | * |
| 139 | 139 | * @return bool |
| 140 | 140 | */ |
| 141 | - private function eventIsValid(array $aHandler): bool |
|
| 142 | - { |
|
| 143 | - return count($aHandler) === 3 && |
|
| 144 | - isset($aHandler[0]) && isset($aHandler[1]) && isset($aHandler[2]) && |
|
| 145 | - is_string($aHandler[0]) && is_string($aHandler[1]) && |
|
| 146 | - is_a($aHandler[2], JsExpr::class); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 141 | +private function eventIsValid(array $aHandler): bool |
|
| 142 | +{ |
|
| 143 | +return count($aHandler) === 3 && |
|
| 144 | +isset($aHandler[0]) && isset($aHandler[1]) && isset($aHandler[2]) && |
|
| 145 | +is_string($aHandler[0]) && is_string($aHandler[1]) && |
|
| 146 | +is_a($aHandler[2], JsExpr::class); |
|
| 147 | +} |
|
| 148 | + |
|
| 149 | +/** |
|
| 150 | 150 | * @param array $aHandler |
| 151 | 151 | * @param EventAttr|null $xAttr |
| 152 | 152 | * |
| 153 | 153 | * @return EventAttr|null |
| 154 | 154 | */ |
| 155 | - private function setEventHandler(array $aHandler, ?EventAttr $xAttr = null): EventAttr|null |
|
| 156 | - { |
|
| 157 | - if(!$this->eventIsValid($aHandler)) |
|
| 158 | - { |
|
| 159 | - return $xAttr; |
|
| 160 | - } |
|
| 161 | - // The array content is valid. |
|
| 162 | - [$sSelector, $sEvent, $xJsExpr] = $aHandler; |
|
| 163 | - return !$xAttr ? |
|
| 164 | - $this->select($sSelector)->on($sEvent, $xJsExpr) : |
|
| 165 | - $xAttr->select($sSelector)->on($sEvent, $xJsExpr); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 155 | +private function setEventHandler(array $aHandler, ?EventAttr $xAttr = null): EventAttr|null |
|
| 156 | +{ |
|
| 157 | +if(!$this->eventIsValid($aHandler)) |
|
| 158 | +{ |
|
| 159 | +return $xAttr; |
|
| 160 | +} |
|
| 161 | +// The array content is valid. |
|
| 162 | +[$sSelector, $sEvent, $xJsExpr] = $aHandler; |
|
| 163 | +return !$xAttr ? |
|
| 164 | +$this->select($sSelector)->on($sEvent, $xJsExpr) : |
|
| 165 | +$xAttr->select($sSelector)->on($sEvent, $xJsExpr); |
|
| 166 | +} |
|
| 167 | + |
|
| 168 | +/** |
|
| 169 | 169 | * Set an event handler |
| 170 | 170 | * |
| 171 | 171 | * @param array $aHandler |
| 172 | 172 | * |
| 173 | 173 | * @return string |
| 174 | 174 | */ |
| 175 | - public function event(array $aHandler): string |
|
| 176 | - { |
|
| 177 | - return $this->setEventHandler($aHandler)?->__toString() ?? ''; |
|
| 178 | - } |
|
| 175 | +public function event(array $aHandler): string |
|
| 176 | +{ |
|
| 177 | +return $this->setEventHandler($aHandler)?->__toString() ?? ''; |
|
| 178 | +} |
|
| 179 | 179 | |
| 180 | - /** |
|
| 180 | +/** |
|
| 181 | 181 | * Set event handlers |
| 182 | 182 | * |
| 183 | 183 | * @param array $aHandlers |
| 184 | 184 | * |
| 185 | 185 | * @return string |
| 186 | 186 | */ |
| 187 | - public function events(array $aHandlers): string |
|
| 188 | - { |
|
| 189 | - return array_reduce($aHandlers, fn(EventAttr|null $xAttr, array $aHandler) |
|
| 190 | - => $this->setEventHandler($aHandler, $xAttr), null)?->__toString() ?? ''; |
|
| 191 | - } |
|
| 187 | +public function events(array $aHandlers): string |
|
| 188 | +{ |
|
| 189 | +return array_reduce($aHandlers, fn(EventAttr|null $xAttr, array $aHandler) |
|
| 190 | +=> $this->setEventHandler($aHandler, $xAttr), null)?->__toString() ?? ''; |
|
| 191 | +} |
|
| 192 | 192 | |
| 193 | - /** |
|
| 193 | +/** |
|
| 194 | 194 | * Get the HTML or Javascript ready code for a package |
| 195 | 195 | * |
| 196 | 196 | * @param string $sClass |
@@ -198,17 +198,17 @@ discard block |
||
| 198 | 198 | * |
| 199 | 199 | * @return string |
| 200 | 200 | */ |
| 201 | - public function package(string $sClass, string $sType = 'html'): string |
|
| 202 | - { |
|
| 203 | - $sCode = match($sType) { |
|
| 204 | - 'html' => jaxon()->package($sClass)?->getHtml() ?? '', |
|
| 205 | - 'ready' => jaxon()->package($sClass)?->getReadyScript() ?? '', |
|
| 206 | - default => '' |
|
| 207 | - }; |
|
| 208 | - $sCode = trim($sCode); |
|
| 209 | - |
|
| 210 | - return $sType !== 'ready' || $sCode === '' ? $sCode : |
|
| 211 | - // Call the ready code with the jaxon.dom.ready function. |
|
| 212 | - "jaxon.dom.ready(() => $sCode)"; |
|
| 213 | - } |
|
| 201 | +public function package(string $sClass, string $sType = 'html'): string |
|
| 202 | +{ |
|
| 203 | +$sCode = match($sType) { |
|
| 204 | +'html' => jaxon()->package($sClass)?->getHtml() ?? '', |
|
| 205 | +'ready' => jaxon()->package($sClass)?->getReadyScript() ?? '', |
|
| 206 | +default => '' |
|
| 207 | +}; |
|
| 208 | +$sCode = trim($sCode); |
|
| 209 | + |
|
| 210 | +return $sType !== 'ready' || $sCode === '' ? $sCode : |
|
| 211 | +// Call the ready code with the jaxon.dom.ready function. |
|
| 212 | +"jaxon.dom.ready(() => $sCode)"; |
|
| 213 | +} |
|
| 214 | 214 | } |
@@ -9,35 +9,35 @@ discard block |
||
| 9 | 9 | |
| 10 | 10 | class Store implements JsonSerializable, Stringable |
| 11 | 11 | { |
| 12 | - /** |
|
| 12 | +/** |
|
| 13 | 13 | * The view renderer |
| 14 | 14 | * |
| 15 | 15 | * @var ViewInterface |
| 16 | 16 | */ |
| 17 | - protected $xRenderer; |
|
| 17 | +protected $xRenderer; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 19 | +/** |
|
| 20 | 20 | * The view namespace |
| 21 | 21 | * |
| 22 | 22 | * @var string |
| 23 | 23 | */ |
| 24 | - protected $sNamespace; |
|
| 24 | +protected $sNamespace; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 26 | +/** |
|
| 27 | 27 | * The view name |
| 28 | 28 | * |
| 29 | 29 | * @var string |
| 30 | 30 | */ |
| 31 | - protected $sViewName; |
|
| 31 | +protected $sViewName; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 33 | +/** |
|
| 34 | 34 | * The view data |
| 35 | 35 | * |
| 36 | 36 | * @var array |
| 37 | 37 | */ |
| 38 | - protected $aViewData = []; |
|
| 38 | +protected $aViewData = []; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 40 | +/** |
|
| 41 | 41 | * Make a piece of data available for the rendered view |
| 42 | 42 | * |
| 43 | 43 | * @param string $sName The data name |
@@ -45,26 +45,26 @@ discard block |
||
| 45 | 45 | * |
| 46 | 46 | * @return Store |
| 47 | 47 | */ |
| 48 | - public function with(string $sName, $xValue): Store |
|
| 49 | - { |
|
| 50 | - $this->aViewData[$sName] = $xValue; |
|
| 51 | - return $this; |
|
| 52 | - } |
|
| 48 | +public function with(string $sName, $xValue): Store |
|
| 49 | +{ |
|
| 50 | +$this->aViewData[$sName] = $xValue; |
|
| 51 | +return $this; |
|
| 52 | +} |
|
| 53 | 53 | |
| 54 | - /** |
|
| 54 | +/** |
|
| 55 | 55 | * Set the data to be rendered |
| 56 | 56 | * |
| 57 | 57 | * @param array $aViewData The view data |
| 58 | 58 | * |
| 59 | 59 | * @return Store |
| 60 | 60 | */ |
| 61 | - public function setData(array $aViewData): Store |
|
| 62 | - { |
|
| 63 | - $this->aViewData = array_merge($this->aViewData, $aViewData); |
|
| 64 | - return $this; |
|
| 65 | - } |
|
| 61 | +public function setData(array $aViewData): Store |
|
| 62 | +{ |
|
| 63 | +$this->aViewData = array_merge($this->aViewData, $aViewData); |
|
| 64 | +return $this; |
|
| 65 | +} |
|
| 66 | 66 | |
| 67 | - /** |
|
| 67 | +/** |
|
| 68 | 68 | * Set the view to be rendered, with optional data |
| 69 | 69 | * |
| 70 | 70 | * @param ViewInterface $xRenderer The view renderer |
@@ -74,65 +74,65 @@ discard block |
||
| 74 | 74 | * |
| 75 | 75 | * @return Store |
| 76 | 76 | */ |
| 77 | - public function setView(ViewInterface $xRenderer, |
|
| 78 | - string $sNamespace, string $sViewName, array $aViewData = []): Store |
|
| 79 | - { |
|
| 80 | - $this->xRenderer = $xRenderer; |
|
| 81 | - $this->sNamespace = trim($sNamespace); |
|
| 82 | - $this->sViewName = trim($sViewName); |
|
| 83 | - $this->aViewData = array_merge($this->aViewData, $aViewData); |
|
| 84 | - return $this; |
|
| 85 | - } |
|
| 77 | +public function setView(ViewInterface $xRenderer, |
|
| 78 | +string $sNamespace, string $sViewName, array $aViewData = []): Store |
|
| 79 | +{ |
|
| 80 | +$this->xRenderer = $xRenderer; |
|
| 81 | +$this->sNamespace = trim($sNamespace); |
|
| 82 | +$this->sViewName = trim($sViewName); |
|
| 83 | +$this->aViewData = array_merge($this->aViewData, $aViewData); |
|
| 84 | +return $this; |
|
| 85 | +} |
|
| 86 | 86 | |
| 87 | - /** |
|
| 87 | +/** |
|
| 88 | 88 | * Get the view namespace |
| 89 | 89 | * |
| 90 | 90 | * @return string The view namespace |
| 91 | 91 | */ |
| 92 | - public function getNamespace(): string |
|
| 93 | - { |
|
| 94 | - return $this->sNamespace; |
|
| 95 | - } |
|
| 92 | +public function getNamespace(): string |
|
| 93 | +{ |
|
| 94 | +return $this->sNamespace; |
|
| 95 | +} |
|
| 96 | 96 | |
| 97 | - /** |
|
| 97 | +/** |
|
| 98 | 98 | * Get the view name |
| 99 | 99 | * |
| 100 | 100 | * @return string The view name |
| 101 | 101 | */ |
| 102 | - public function getViewName(): string |
|
| 103 | - { |
|
| 104 | - return $this->sViewName; |
|
| 105 | - } |
|
| 102 | +public function getViewName(): string |
|
| 103 | +{ |
|
| 104 | +return $this->sViewName; |
|
| 105 | +} |
|
| 106 | 106 | |
| 107 | - /** |
|
| 107 | +/** |
|
| 108 | 108 | * Get the view data |
| 109 | 109 | * |
| 110 | 110 | * @return array The view data |
| 111 | 111 | */ |
| 112 | - public function getViewData(): array |
|
| 113 | - { |
|
| 114 | - return $this->aViewData; |
|
| 115 | - } |
|
| 112 | +public function getViewData(): array |
|
| 113 | +{ |
|
| 114 | +return $this->aViewData; |
|
| 115 | +} |
|
| 116 | 116 | |
| 117 | - /** |
|
| 117 | +/** |
|
| 118 | 118 | * Render a view using third party view system |
| 119 | 119 | * |
| 120 | 120 | * @return string The string representation of the view |
| 121 | 121 | */ |
| 122 | - public function __toString(): string |
|
| 123 | - { |
|
| 124 | - return !$this->xRenderer ? '' : $this->xRenderer->render($this); |
|
| 125 | - } |
|
| 122 | +public function __toString(): string |
|
| 123 | +{ |
|
| 124 | +return !$this->xRenderer ? '' : $this->xRenderer->render($this); |
|
| 125 | +} |
|
| 126 | 126 | |
| 127 | - /** |
|
| 127 | +/** |
|
| 128 | 128 | * Convert this object to string for json. |
| 129 | 129 | * |
| 130 | 130 | * This is a method of the JsonSerializable interface. |
| 131 | 131 | * |
| 132 | 132 | * @return string |
| 133 | 133 | */ |
| 134 | - public function jsonSerialize(): string |
|
| 135 | - { |
|
| 136 | - return $this->__toString(); |
|
| 137 | - } |
|
| 134 | +public function jsonSerialize(): string |
|
| 135 | +{ |
|
| 136 | +return $this->__toString(); |
|
| 137 | +} |
|
| 138 | 138 | } |
@@ -16,58 +16,58 @@ discard block |
||
| 16 | 16 | |
| 17 | 17 | class ViewRenderer |
| 18 | 18 | { |
| 19 | - /** |
|
| 19 | +/** |
|
| 20 | 20 | * @var Container |
| 21 | 21 | */ |
| 22 | - protected $di; |
|
| 22 | +protected $di; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 24 | +/** |
|
| 25 | 25 | * The view data store |
| 26 | 26 | * |
| 27 | 27 | * @var Store |
| 28 | 28 | */ |
| 29 | - protected $xStore = null; |
|
| 29 | +protected $xStore = null; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 31 | +/** |
|
| 32 | 32 | * The view data store |
| 33 | 33 | * |
| 34 | 34 | * @var Store |
| 35 | 35 | */ |
| 36 | - protected $xEmptyStore = null; |
|
| 36 | +protected $xEmptyStore = null; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 38 | +/** |
|
| 39 | 39 | * The view namespaces |
| 40 | 40 | * |
| 41 | 41 | * @var array |
| 42 | 42 | */ |
| 43 | - protected $aNamespaces = []; |
|
| 43 | +protected $aNamespaces = []; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 45 | +/** |
|
| 46 | 46 | * The default namespace |
| 47 | 47 | * |
| 48 | 48 | * @var string |
| 49 | 49 | */ |
| 50 | - protected $sDefaultNamespace = 'jaxon'; |
|
| 50 | +protected $sDefaultNamespace = 'jaxon'; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 52 | +/** |
|
| 53 | 53 | * The view global data |
| 54 | 54 | * |
| 55 | 55 | * @var array |
| 56 | 56 | */ |
| 57 | - protected $aViewData = []; |
|
| 57 | +protected $aViewData = []; |
|
| 58 | 58 | |
| 59 | - /** |
|
| 59 | +/** |
|
| 60 | 60 | * The class constructor |
| 61 | 61 | * |
| 62 | 62 | * @param Container $di |
| 63 | 63 | */ |
| 64 | - public function __construct(Container $di) |
|
| 65 | - { |
|
| 66 | - $this->di = $di; |
|
| 67 | - $this->xEmptyStore = new Store(); |
|
| 68 | - } |
|
| 64 | +public function __construct(Container $di) |
|
| 65 | +{ |
|
| 66 | +$this->di = $di; |
|
| 67 | +$this->xEmptyStore = new Store(); |
|
| 68 | +} |
|
| 69 | 69 | |
| 70 | - /** |
|
| 70 | +/** |
|
| 71 | 71 | * Add a view namespace, and set the corresponding renderer. |
| 72 | 72 | * |
| 73 | 73 | * @param string $sNamespace The namespace name |
@@ -77,59 +77,59 @@ discard block |
||
| 77 | 77 | * |
| 78 | 78 | * @return void |
| 79 | 79 | */ |
| 80 | - public function addNamespace(string $sNamespace, string $sDirectory, |
|
| 81 | - string $sExtension, string $sRenderer): void |
|
| 82 | - { |
|
| 83 | - $aNamespace = [ |
|
| 84 | - 'directory' => $sDirectory, |
|
| 85 | - 'extension' => $sExtension, |
|
| 86 | - 'renderer' => $sRenderer, |
|
| 87 | - ]; |
|
| 88 | - $this->aNamespaces[$sNamespace] = $aNamespace; |
|
| 89 | - } |
|
| 80 | +public function addNamespace(string $sNamespace, string $sDirectory, |
|
| 81 | +string $sExtension, string $sRenderer): void |
|
| 82 | +{ |
|
| 83 | +$aNamespace = [ |
|
| 84 | +'directory' => $sDirectory, |
|
| 85 | +'extension' => $sExtension, |
|
| 86 | +'renderer' => $sRenderer, |
|
| 87 | +]; |
|
| 88 | +$this->aNamespaces[$sNamespace] = $aNamespace; |
|
| 89 | +} |
|
| 90 | 90 | |
| 91 | - /** |
|
| 91 | +/** |
|
| 92 | 92 | * Set the view namespaces. |
| 93 | 93 | * |
| 94 | 94 | * @param Config $xAppConfig The config options provided in the library |
| 95 | 95 | * |
| 96 | 96 | * @return void |
| 97 | 97 | */ |
| 98 | - public function addNamespaces(Config $xAppConfig): void |
|
| 99 | - { |
|
| 100 | - if(empty($aNamespaces = $xAppConfig->getOptionNames('views'))) |
|
| 101 | - { |
|
| 102 | - return; |
|
| 103 | - } |
|
| 98 | +public function addNamespaces(Config $xAppConfig): void |
|
| 99 | +{ |
|
| 100 | +if(empty($aNamespaces = $xAppConfig->getOptionNames('views'))) |
|
| 101 | +{ |
|
| 102 | +return; |
|
| 103 | +} |
|
| 104 | 104 | |
| 105 | - $sPackage = $xAppConfig->getOption('package', ''); |
|
| 106 | - foreach($aNamespaces as $sNamespace => $sOption) |
|
| 107 | - { |
|
| 108 | - // Save the namespace |
|
| 109 | - $aNamespace = $xAppConfig->getOption($sOption); |
|
| 110 | - $aNamespace['package'] = $sPackage; |
|
| 111 | - if(!isset($aNamespace['renderer'])) |
|
| 112 | - { |
|
| 113 | - $aNamespace['renderer'] = 'jaxon'; // 'jaxon' is the default renderer. |
|
| 114 | - } |
|
| 115 | - $this->aNamespaces[$sNamespace] = $aNamespace; |
|
| 116 | - } |
|
| 117 | - } |
|
| 105 | +$sPackage = $xAppConfig->getOption('package', ''); |
|
| 106 | +foreach($aNamespaces as $sNamespace => $sOption) |
|
| 107 | +{ |
|
| 108 | +// Save the namespace |
|
| 109 | +$aNamespace = $xAppConfig->getOption($sOption); |
|
| 110 | +$aNamespace['package'] = $sPackage; |
|
| 111 | +if(!isset($aNamespace['renderer'])) |
|
| 112 | +{ |
|
| 113 | + $aNamespace['renderer'] = 'jaxon'; // 'jaxon' is the default renderer. |
|
| 114 | +} |
|
| 115 | +$this->aNamespaces[$sNamespace] = $aNamespace; |
|
| 116 | +} |
|
| 117 | +} |
|
| 118 | 118 | |
| 119 | - /** |
|
| 119 | +/** |
|
| 120 | 120 | * Get the view renderer |
| 121 | 121 | * |
| 122 | 122 | * @param string $sId The unique identifier of the view renderer |
| 123 | 123 | * |
| 124 | 124 | * @return ViewInterface |
| 125 | 125 | */ |
| 126 | - public function getRenderer(string $sId): ViewInterface |
|
| 127 | - { |
|
| 128 | - // Return the view renderer with the given id |
|
| 129 | - return $this->di->g("jaxon.app.view.$sId"); |
|
| 130 | - } |
|
| 126 | +public function getRenderer(string $sId): ViewInterface |
|
| 127 | +{ |
|
| 128 | +// Return the view renderer with the given id |
|
| 129 | +return $this->di->g("jaxon.app.view.$sId"); |
|
| 130 | +} |
|
| 131 | 131 | |
| 132 | - /** |
|
| 132 | +/** |
|
| 133 | 133 | * Add a view renderer with an id |
| 134 | 134 | * |
| 135 | 135 | * @param string $sId The unique identifier of the view renderer |
@@ -137,25 +137,25 @@ discard block |
||
| 137 | 137 | * |
| 138 | 138 | * @return void |
| 139 | 139 | */ |
| 140 | - public function addRenderer(string $sId, Closure $xClosure): void |
|
| 141 | - { |
|
| 142 | - // Return the initialized view renderer |
|
| 143 | - $this->di->set("jaxon.app.view.$sId", function($di) use($sId, $xClosure) { |
|
| 144 | - // Get the defined renderer |
|
| 145 | - $xRenderer = $xClosure($di); |
|
| 146 | - // Init the renderer with the template namespaces |
|
| 147 | - $aNamespaces = array_filter($this->aNamespaces, function($aOptions) use($sId) { |
|
| 148 | - return $aOptions['renderer'] === $sId; |
|
| 149 | - }); |
|
| 150 | - foreach($aNamespaces as $sName => $aOptions) |
|
| 151 | - { |
|
| 152 | - $xRenderer->addNamespace($sName, $aOptions['directory'], $aOptions['extension']); |
|
| 153 | - } |
|
| 154 | - return $xRenderer; |
|
| 155 | - }); |
|
| 156 | - } |
|
| 140 | +public function addRenderer(string $sId, Closure $xClosure): void |
|
| 141 | +{ |
|
| 142 | +// Return the initialized view renderer |
|
| 143 | +$this->di->set("jaxon.app.view.$sId", function($di) use($sId, $xClosure) { |
|
| 144 | +// Get the defined renderer |
|
| 145 | +$xRenderer = $xClosure($di); |
|
| 146 | +// Init the renderer with the template namespaces |
|
| 147 | +$aNamespaces = array_filter($this->aNamespaces, function($aOptions) use($sId) { |
|
| 148 | + return $aOptions['renderer'] === $sId; |
|
| 149 | +}); |
|
| 150 | +foreach($aNamespaces as $sName => $aOptions) |
|
| 151 | +{ |
|
| 152 | + $xRenderer->addNamespace($sName, $aOptions['directory'], $aOptions['extension']); |
|
| 153 | +} |
|
| 154 | +return $xRenderer; |
|
| 155 | +}); |
|
| 156 | +} |
|
| 157 | 157 | |
| 158 | - /** |
|
| 158 | +/** |
|
| 159 | 159 | * Add a view renderer with an id |
| 160 | 160 | * |
| 161 | 161 | * @param string $sId The unique identifier of the view renderer |
@@ -164,55 +164,55 @@ discard block |
||
| 164 | 164 | * |
| 165 | 165 | * @return void |
| 166 | 166 | */ |
| 167 | - public function setDefaultRenderer(string $sId, string $sExtension, Closure $xClosure): void |
|
| 168 | - { |
|
| 169 | - $this->setDefaultNamespace($sId); |
|
| 170 | - $this->addNamespace($sId, '', $sExtension, $sId); |
|
| 171 | - $this->addRenderer($sId, $xClosure); |
|
| 172 | - } |
|
| 167 | +public function setDefaultRenderer(string $sId, string $sExtension, Closure $xClosure): void |
|
| 168 | +{ |
|
| 169 | +$this->setDefaultNamespace($sId); |
|
| 170 | +$this->addNamespace($sId, '', $sExtension, $sId); |
|
| 171 | +$this->addRenderer($sId, $xClosure); |
|
| 172 | +} |
|
| 173 | 173 | |
| 174 | - /** |
|
| 174 | +/** |
|
| 175 | 175 | * Get the view renderer for a given namespace |
| 176 | 176 | * |
| 177 | 177 | * @param string $sNamespace The namespace name |
| 178 | 178 | * |
| 179 | 179 | * @return ViewInterface|null |
| 180 | 180 | */ |
| 181 | - public function getNamespaceRenderer(string $sNamespace): ?ViewInterface |
|
| 182 | - { |
|
| 183 | - if(!isset($this->aNamespaces[$sNamespace])) |
|
| 184 | - { |
|
| 185 | - return null; |
|
| 186 | - } |
|
| 187 | - // Return the view renderer with the configured id |
|
| 188 | - return $this->getRenderer($this->aNamespaces[$sNamespace]['renderer']); |
|
| 189 | - } |
|
| 181 | +public function getNamespaceRenderer(string $sNamespace): ?ViewInterface |
|
| 182 | +{ |
|
| 183 | +if(!isset($this->aNamespaces[$sNamespace])) |
|
| 184 | +{ |
|
| 185 | +return null; |
|
| 186 | +} |
|
| 187 | +// Return the view renderer with the configured id |
|
| 188 | +return $this->getRenderer($this->aNamespaces[$sNamespace]['renderer']); |
|
| 189 | +} |
|
| 190 | 190 | |
| 191 | - /** |
|
| 191 | +/** |
|
| 192 | 192 | * Set the default namespace |
| 193 | 193 | * |
| 194 | 194 | * @param string $sDefaultNamespace |
| 195 | 195 | */ |
| 196 | - public function setDefaultNamespace(string $sDefaultNamespace): void |
|
| 197 | - { |
|
| 198 | - $this->sDefaultNamespace = $sDefaultNamespace; |
|
| 199 | - } |
|
| 196 | +public function setDefaultNamespace(string $sDefaultNamespace): void |
|
| 197 | +{ |
|
| 198 | +$this->sDefaultNamespace = $sDefaultNamespace; |
|
| 199 | +} |
|
| 200 | 200 | |
| 201 | - /** |
|
| 201 | +/** |
|
| 202 | 202 | * Get the current store or create a new store |
| 203 | 203 | * |
| 204 | 204 | * @return Store |
| 205 | 205 | */ |
| 206 | - protected function store(): Store |
|
| 207 | - { |
|
| 208 | - if(!$this->xStore) |
|
| 209 | - { |
|
| 210 | - $this->xStore = new Store(); |
|
| 211 | - } |
|
| 212 | - return $this->xStore; |
|
| 213 | - } |
|
| 206 | +protected function store(): Store |
|
| 207 | +{ |
|
| 208 | +if(!$this->xStore) |
|
| 209 | +{ |
|
| 210 | +$this->xStore = new Store(); |
|
| 211 | +} |
|
| 212 | +return $this->xStore; |
|
| 213 | +} |
|
| 214 | 214 | |
| 215 | - /** |
|
| 215 | +/** |
|
| 216 | 216 | * Make a piece of data available for the rendered view |
| 217 | 217 | * |
| 218 | 218 | * @param string $sName The data name |
@@ -220,13 +220,13 @@ discard block |
||
| 220 | 220 | * |
| 221 | 221 | * @return ViewRenderer |
| 222 | 222 | */ |
| 223 | - public function set(string $sName, $xValue): ViewRenderer |
|
| 224 | - { |
|
| 225 | - $this->store()->with($sName, $xValue); |
|
| 226 | - return $this; |
|
| 227 | - } |
|
| 223 | +public function set(string $sName, $xValue): ViewRenderer |
|
| 224 | +{ |
|
| 225 | +$this->store()->with($sName, $xValue); |
|
| 226 | +return $this; |
|
| 227 | +} |
|
| 228 | 228 | |
| 229 | - /** |
|
| 229 | +/** |
|
| 230 | 230 | * Make a piece of data available for all views |
| 231 | 231 | * |
| 232 | 232 | * @param string $sName The data name |
@@ -234,29 +234,29 @@ discard block |
||
| 234 | 234 | * |
| 235 | 235 | * @return ViewRenderer |
| 236 | 236 | */ |
| 237 | - public function share(string $sName, $xValue): ViewRenderer |
|
| 238 | - { |
|
| 239 | - $this->aViewData[$sName] = $xValue; |
|
| 240 | - return $this; |
|
| 241 | - } |
|
| 237 | +public function share(string $sName, $xValue): ViewRenderer |
|
| 238 | +{ |
|
| 239 | +$this->aViewData[$sName] = $xValue; |
|
| 240 | +return $this; |
|
| 241 | +} |
|
| 242 | 242 | |
| 243 | - /** |
|
| 243 | +/** |
|
| 244 | 244 | * Make an array of data available for all views |
| 245 | 245 | * |
| 246 | 246 | * @param array $aValues The data values |
| 247 | 247 | * |
| 248 | 248 | * @return ViewRenderer |
| 249 | 249 | */ |
| 250 | - public function shareValues(array $aValues): ViewRenderer |
|
| 251 | - { |
|
| 252 | - foreach($aValues as $sName => $xValue) |
|
| 253 | - { |
|
| 254 | - $this->share($sName, $xValue); |
|
| 255 | - } |
|
| 256 | - return $this; |
|
| 257 | - } |
|
| 250 | +public function shareValues(array $aValues): ViewRenderer |
|
| 251 | +{ |
|
| 252 | +foreach($aValues as $sName => $xValue) |
|
| 253 | +{ |
|
| 254 | +$this->share($sName, $xValue); |
|
| 255 | +} |
|
| 256 | +return $this; |
|
| 257 | +} |
|
| 258 | 258 | |
| 259 | - /** |
|
| 259 | +/** |
|
| 260 | 260 | * Render a view using a store |
| 261 | 261 | * |
| 262 | 262 | * The store returned by this function will later be used with the make() method to render the view. |
@@ -266,32 +266,32 @@ discard block |
||
| 266 | 266 | * |
| 267 | 267 | * @return Store A store populated with the view data |
| 268 | 268 | */ |
| 269 | - public function render(string $sViewName, array $aViewData = []): Store |
|
| 270 | - { |
|
| 271 | - $xStore = $this->store(); |
|
| 272 | - // Get the default view namespace |
|
| 273 | - $sNamespace = $this->sDefaultNamespace; |
|
| 274 | - // Get the namespace from the view name |
|
| 275 | - $nSeparatorPosition = strrpos($sViewName, '::'); |
|
| 276 | - if($nSeparatorPosition !== false) |
|
| 277 | - { |
|
| 278 | - $sNamespace = substr($sViewName, 0, $nSeparatorPosition); |
|
| 279 | - $sViewName = substr($sViewName, $nSeparatorPosition + 2); |
|
| 280 | - } |
|
| 269 | +public function render(string $sViewName, array $aViewData = []): Store |
|
| 270 | +{ |
|
| 271 | +$xStore = $this->store(); |
|
| 272 | +// Get the default view namespace |
|
| 273 | +$sNamespace = $this->sDefaultNamespace; |
|
| 274 | +// Get the namespace from the view name |
|
| 275 | +$nSeparatorPosition = strrpos($sViewName, '::'); |
|
| 276 | +if($nSeparatorPosition !== false) |
|
| 277 | +{ |
|
| 278 | +$sNamespace = substr($sViewName, 0, $nSeparatorPosition); |
|
| 279 | +$sViewName = substr($sViewName, $nSeparatorPosition + 2); |
|
| 280 | +} |
|
| 281 | 281 | |
| 282 | - $xRenderer = $this->getNamespaceRenderer($sNamespace); |
|
| 283 | - if(!$xRenderer) |
|
| 284 | - { |
|
| 285 | - // Cannot render a view if there's no renderer corresponding to the namespace. |
|
| 286 | - return $this->xEmptyStore; |
|
| 287 | - } |
|
| 282 | +$xRenderer = $this->getNamespaceRenderer($sNamespace); |
|
| 283 | +if(!$xRenderer) |
|
| 284 | +{ |
|
| 285 | +// Cannot render a view if there's no renderer corresponding to the namespace. |
|
| 286 | +return $this->xEmptyStore; |
|
| 287 | +} |
|
| 288 | 288 | |
| 289 | - $xStore->setData(array_merge($this->aViewData, $aViewData)) |
|
| 290 | - ->setView($xRenderer, $sNamespace, $sViewName); |
|
| 289 | +$xStore->setData(array_merge($this->aViewData, $aViewData)) |
|
| 290 | +->setView($xRenderer, $sNamespace, $sViewName); |
|
| 291 | 291 | |
| 292 | - // Set the store to null so a new store will be created for the next view. |
|
| 293 | - $this->xStore = null; |
|
| 294 | - // Return the store |
|
| 295 | - return $xStore; |
|
| 296 | - } |
|
| 292 | +// Set the store to null so a new store will be created for the next view. |
|
| 293 | +$this->xStore = null; |
|
| 294 | +// Return the store |
|
| 295 | +return $xStore; |
|
| 296 | +} |
|
| 297 | 297 | } |
@@ -97,18 +97,18 @@ discard block |
||
| 97 | 97 | */ |
| 98 | 98 | public function addNamespaces(Config $xAppConfig): void |
| 99 | 99 | { |
| 100 | - if(empty($aNamespaces = $xAppConfig->getOptionNames('views'))) |
|
| 100 | + if (empty($aNamespaces = $xAppConfig->getOptionNames('views'))) |
|
| 101 | 101 | { |
| 102 | 102 | return; |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | $sPackage = $xAppConfig->getOption('package', ''); |
| 106 | - foreach($aNamespaces as $sNamespace => $sOption) |
|
| 106 | + foreach ($aNamespaces as $sNamespace => $sOption) |
|
| 107 | 107 | { |
| 108 | 108 | // Save the namespace |
| 109 | 109 | $aNamespace = $xAppConfig->getOption($sOption); |
| 110 | 110 | $aNamespace['package'] = $sPackage; |
| 111 | - if(!isset($aNamespace['renderer'])) |
|
| 111 | + if (!isset($aNamespace['renderer'])) |
|
| 112 | 112 | { |
| 113 | 113 | $aNamespace['renderer'] = 'jaxon'; // 'jaxon' is the default renderer. |
| 114 | 114 | } |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | $aNamespaces = array_filter($this->aNamespaces, function($aOptions) use($sId) { |
| 148 | 148 | return $aOptions['renderer'] === $sId; |
| 149 | 149 | }); |
| 150 | - foreach($aNamespaces as $sName => $aOptions) |
|
| 150 | + foreach ($aNamespaces as $sName => $aOptions) |
|
| 151 | 151 | { |
| 152 | 152 | $xRenderer->addNamespace($sName, $aOptions['directory'], $aOptions['extension']); |
| 153 | 153 | } |
@@ -180,7 +180,7 @@ discard block |
||
| 180 | 180 | */ |
| 181 | 181 | public function getNamespaceRenderer(string $sNamespace): ?ViewInterface |
| 182 | 182 | { |
| 183 | - if(!isset($this->aNamespaces[$sNamespace])) |
|
| 183 | + if (!isset($this->aNamespaces[$sNamespace])) |
|
| 184 | 184 | { |
| 185 | 185 | return null; |
| 186 | 186 | } |
@@ -205,7 +205,7 @@ discard block |
||
| 205 | 205 | */ |
| 206 | 206 | protected function store(): Store |
| 207 | 207 | { |
| 208 | - if(!$this->xStore) |
|
| 208 | + if (!$this->xStore) |
|
| 209 | 209 | { |
| 210 | 210 | $this->xStore = new Store(); |
| 211 | 211 | } |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | */ |
| 250 | 250 | public function shareValues(array $aValues): ViewRenderer |
| 251 | 251 | { |
| 252 | - foreach($aValues as $sName => $xValue) |
|
| 252 | + foreach ($aValues as $sName => $xValue) |
|
| 253 | 253 | { |
| 254 | 254 | $this->share($sName, $xValue); |
| 255 | 255 | } |
@@ -273,14 +273,14 @@ discard block |
||
| 273 | 273 | $sNamespace = $this->sDefaultNamespace; |
| 274 | 274 | // Get the namespace from the view name |
| 275 | 275 | $nSeparatorPosition = strrpos($sViewName, '::'); |
| 276 | - if($nSeparatorPosition !== false) |
|
| 276 | + if ($nSeparatorPosition !== false) |
|
| 277 | 277 | { |
| 278 | 278 | $sNamespace = substr($sViewName, 0, $nSeparatorPosition); |
| 279 | 279 | $sViewName = substr($sViewName, $nSeparatorPosition + 2); |
| 280 | 280 | } |
| 281 | 281 | |
| 282 | 282 | $xRenderer = $this->getNamespaceRenderer($sNamespace); |
| 283 | - if(!$xRenderer) |
|
| 283 | + if (!$xRenderer) |
|
| 284 | 284 | { |
| 285 | 285 | // Cannot render a view if there's no renderer corresponding to the namespace. |
| 286 | 286 | return $this->xEmptyStore; |
@@ -6,53 +6,53 @@ |
||
| 6 | 6 | |
| 7 | 7 | abstract class PageComponent extends NodeComponent |
| 8 | 8 | { |
| 9 | - /** |
|
| 9 | +/** |
|
| 10 | 10 | * The current page number. |
| 11 | 11 | * |
| 12 | 12 | * @var int |
| 13 | 13 | */ |
| 14 | - private int $currentPage = 1; |
|
| 14 | +private int $currentPage = 1; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 16 | +/** |
|
| 17 | 17 | * Get the total number of items to paginate. |
| 18 | 18 | * |
| 19 | 19 | * @return int |
| 20 | 20 | */ |
| 21 | - abstract protected function count(): int; |
|
| 21 | +abstract protected function count(): int; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 23 | +/** |
|
| 24 | 24 | * Get the max number of items per page. |
| 25 | 25 | * |
| 26 | 26 | * @return int |
| 27 | 27 | */ |
| 28 | - abstract protected function limit(): int; |
|
| 28 | +abstract protected function limit(): int; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 30 | +/** |
|
| 31 | 31 | * Get the paginator for the component. |
| 32 | 32 | * |
| 33 | 33 | * @param int $pageNumber |
| 34 | 34 | * |
| 35 | 35 | * @return Paginator |
| 36 | 36 | */ |
| 37 | - protected function paginator(int $pageNumber): Paginator |
|
| 38 | - { |
|
| 39 | - return $this->cl(Component\Pagination::class) |
|
| 40 | - // Use the js class name as component item identifier. |
|
| 41 | - ->item($this->rq()->_class()) |
|
| 42 | - ->paginator($pageNumber > 0 ? $pageNumber : 1, $this->limit(), $this->count()) |
|
| 43 | - // This callback will receive the final value of the current page number. |
|
| 44 | - ->page(function(int $currentPage) { |
|
| 45 | - $this->currentPage = $currentPage; |
|
| 46 | - }); |
|
| 47 | - } |
|
| 37 | +protected function paginator(int $pageNumber): Paginator |
|
| 38 | +{ |
|
| 39 | +return $this->cl(Component\Pagination::class) |
|
| 40 | +// Use the js class name as component item identifier. |
|
| 41 | +->item($this->rq()->_class()) |
|
| 42 | +->paginator($pageNumber > 0 ? $pageNumber : 1, $this->limit(), $this->count()) |
|
| 43 | +// This callback will receive the final value of the current page number. |
|
| 44 | +->page(function(int $currentPage) { |
|
| 45 | + $this->currentPage = $currentPage; |
|
| 46 | +}); |
|
| 47 | +} |
|
| 48 | 48 | |
| 49 | - /** |
|
| 49 | +/** |
|
| 50 | 50 | * Get the current page number |
| 51 | 51 | * |
| 52 | 52 | * @return int |
| 53 | 53 | */ |
| 54 | - protected function currentPage(): int |
|
| 55 | - { |
|
| 56 | - return $this->currentPage; |
|
| 57 | - } |
|
| 54 | +protected function currentPage(): int |
|
| 55 | +{ |
|
| 56 | +return $this->currentPage; |
|
| 57 | +} |
|
| 58 | 58 | } |
@@ -6,81 +6,81 @@ |
||
| 6 | 6 | |
| 7 | 7 | trait PageDatabagTrait |
| 8 | 8 | { |
| 9 | - /** |
|
| 9 | +/** |
|
| 10 | 10 | * The current page number. |
| 11 | 11 | * |
| 12 | 12 | * @var int |
| 13 | 13 | */ |
| 14 | - private int $currentPage = 1; |
|
| 14 | +private int $currentPage = 1; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 16 | +/** |
|
| 17 | 17 | * Get the pagination databag name. |
| 18 | 18 | * |
| 19 | 19 | * @return string |
| 20 | 20 | */ |
| 21 | - abstract protected function bagName(): string; |
|
| 21 | +abstract protected function bagName(): string; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 23 | +/** |
|
| 24 | 24 | * Get the pagination databag attribute. |
| 25 | 25 | * |
| 26 | 26 | * @return string |
| 27 | 27 | */ |
| 28 | - abstract protected function bagAttr(): string; |
|
| 28 | +abstract protected function bagAttr(): string; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 30 | +/** |
|
| 31 | 31 | * Get the page number. |
| 32 | 32 | * |
| 33 | 33 | * @param int $pageNumber |
| 34 | 34 | * |
| 35 | 35 | * @return int |
| 36 | 36 | */ |
| 37 | - private function getPageNumber(int $pageNumber): int |
|
| 38 | - { |
|
| 39 | - // If no page number is provided, then get the value from the databag. |
|
| 40 | - return $pageNumber > 0 ? $pageNumber : |
|
| 41 | - (int)$this->bag($this->bagName())->get($this->bagAttr(), 1); |
|
| 42 | - } |
|
| 37 | +private function getPageNumber(int $pageNumber): int |
|
| 38 | +{ |
|
| 39 | +// If no page number is provided, then get the value from the databag. |
|
| 40 | +return $pageNumber > 0 ? $pageNumber : |
|
| 41 | +(int)$this->bag($this->bagName())->get($this->bagAttr(), 1); |
|
| 42 | +} |
|
| 43 | 43 | |
| 44 | - /** |
|
| 44 | +/** |
|
| 45 | 45 | * Set the page number. |
| 46 | 46 | * |
| 47 | 47 | * @param int $currentPage |
| 48 | 48 | * |
| 49 | 49 | * @return void |
| 50 | 50 | */ |
| 51 | - private function setCurrentPage(int $currentPage): void |
|
| 52 | - { |
|
| 53 | - // Save the current page in the databag. |
|
| 54 | - $this->bag($this->bagName())->set($this->bagAttr(), $currentPage); |
|
| 55 | - $this->currentPage = $currentPage; |
|
| 56 | - } |
|
| 51 | +private function setCurrentPage(int $currentPage): void |
|
| 52 | +{ |
|
| 53 | +// Save the current page in the databag. |
|
| 54 | +$this->bag($this->bagName())->set($this->bagAttr(), $currentPage); |
|
| 55 | +$this->currentPage = $currentPage; |
|
| 56 | +} |
|
| 57 | 57 | |
| 58 | - /** |
|
| 58 | +/** |
|
| 59 | 59 | * Get the paginator for the component. |
| 60 | 60 | * |
| 61 | 61 | * @param int $pageNumber |
| 62 | 62 | * |
| 63 | 63 | * @return Paginator |
| 64 | 64 | */ |
| 65 | - protected function paginator(int $pageNumber): Paginator |
|
| 66 | - { |
|
| 67 | - return $this->cl(Component\Pagination::class) |
|
| 68 | - // Use the js class name as component item identifier. |
|
| 69 | - ->item($this->rq()->_class()) |
|
| 70 | - ->paginator($this->getPageNumber($pageNumber), $this->limit(), $this->count()) |
|
| 71 | - // This callback will receive the final value of the current page number. |
|
| 72 | - ->page(function(int $currentPage) { |
|
| 73 | - $this->setCurrentPage($currentPage); |
|
| 74 | - }); |
|
| 75 | - } |
|
| 65 | +protected function paginator(int $pageNumber): Paginator |
|
| 66 | +{ |
|
| 67 | +return $this->cl(Component\Pagination::class) |
|
| 68 | +// Use the js class name as component item identifier. |
|
| 69 | +->item($this->rq()->_class()) |
|
| 70 | +->paginator($this->getPageNumber($pageNumber), $this->limit(), $this->count()) |
|
| 71 | +// This callback will receive the final value of the current page number. |
|
| 72 | +->page(function(int $currentPage) { |
|
| 73 | + $this->setCurrentPage($currentPage); |
|
| 74 | +}); |
|
| 75 | +} |
|
| 76 | 76 | |
| 77 | - /** |
|
| 77 | +/** |
|
| 78 | 78 | * Get the current page number |
| 79 | 79 | * |
| 80 | 80 | * @return int |
| 81 | 81 | */ |
| 82 | - protected function currentPage(): int |
|
| 83 | - { |
|
| 84 | - return $this->currentPage; |
|
| 85 | - } |
|
| 82 | +protected function currentPage(): int |
|
| 83 | +{ |
|
| 84 | +return $this->currentPage; |
|
| 85 | +} |
|
| 86 | 86 | } |
@@ -37,8 +37,7 @@ |
||
| 37 | 37 | private function getPageNumber(int $pageNumber): int |
| 38 | 38 | { |
| 39 | 39 | // If no page number is provided, then get the value from the databag. |
| 40 | - return $pageNumber > 0 ? $pageNumber : |
|
| 41 | - (int)$this->bag($this->bagName())->get($this->bagAttr(), 1); |
|
| 40 | + return $pageNumber > 0 ? $pageNumber : (int)$this->bag($this->bagName())->get($this->bagAttr(), 1); |
|
| 42 | 41 | } |
| 43 | 42 | |
| 44 | 43 | /** |