1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Paginator.php - Jaxon Request Factory |
5
|
|
|
* |
6
|
|
|
* Create pagination links to a given class. |
7
|
|
|
* |
8
|
|
|
* @package jaxon-core |
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
10
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
11
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
12
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Jaxon\Factory\CallableObject; |
16
|
|
|
|
17
|
|
|
use Jaxon\DI\Container; |
18
|
|
|
use Jaxon\Request\Support\CallableObject; |
19
|
|
|
|
20
|
|
|
class Paginator |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The callable object this factory is attached to |
24
|
|
|
* |
25
|
|
|
* @var CallableObject |
26
|
|
|
*/ |
27
|
|
|
private $xCallable; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The total number of items |
31
|
|
|
* |
32
|
|
|
* @var integer |
33
|
|
|
*/ |
34
|
|
|
private $nItemsTotal = 0; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The number of items per page |
38
|
|
|
* |
39
|
|
|
* @var integer |
40
|
|
|
*/ |
41
|
|
|
private $nItemsPerPage = 0; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The current page |
45
|
|
|
* |
46
|
|
|
* @var integer |
47
|
|
|
*/ |
48
|
|
|
private $nCurrentPage = 0; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create a new Factory instance. |
52
|
|
|
* |
53
|
|
|
* @return void |
|
|
|
|
54
|
|
|
*/ |
55
|
|
|
public function __construct(CallableObject $xCallable) |
56
|
|
|
{ |
57
|
|
|
$this->xCallable = $xCallable; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set the paginator properties |
62
|
|
|
* |
63
|
|
|
* @param integer $nItemsTotal the total number of items |
64
|
|
|
* @param integer $nItemsPerPage the number of items per page |
65
|
|
|
* @param integer $nCurrentPage the current page |
66
|
|
|
* |
67
|
|
|
* @return Paginator |
68
|
|
|
*/ |
69
|
|
|
public function setProperties($nItemsTotal, $nItemsPerPage, $nCurrentPage) |
70
|
|
|
{ |
71
|
|
|
$this->nItemsTotal = $nItemsTotal; |
72
|
|
|
$this->nItemsPerPage = $nItemsPerPage; |
73
|
|
|
$this->nCurrentPage = $nCurrentPage; |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Generate the corresponding javascript code for a call to any method |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function __call($sMethod, $aArguments) |
84
|
|
|
{ |
85
|
|
|
// Add the paginator options to the method arguments |
86
|
|
|
$aPgArgs = [$this->nItemsTotal, $this->nItemsPerPage, $this->nCurrentPage, $sMethod]; |
87
|
|
|
$aArguments = array_merge($aPgArgs, $aArguments); |
88
|
|
|
|
89
|
|
|
// Make the request |
90
|
|
|
$factory = Container::getInstance()->getRequestFactory()->setCallable($this->xCallable); |
91
|
|
|
return call_user_func_array([$factory, 'paginate'], $aArguments); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.