1 | <?php |
||
24 | trait PaginatorTrait |
||
25 | { |
||
26 | /** |
||
27 | * @internal |
||
28 | * @var PaginatorInterface |
||
29 | */ |
||
30 | private $paginator = null; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $limit = 0; |
||
36 | |||
37 | /** |
||
38 | * @var int |
||
39 | */ |
||
40 | protected $offset = 0; |
||
41 | |||
42 | /** |
||
43 | * Count elements of an object. |
||
44 | * |
||
45 | * @link http://php.net/manual/en/countable.count.php |
||
46 | * @return int |
||
47 | */ |
||
48 | abstract public function count(); |
||
49 | |||
50 | /** |
||
51 | * Set selection limit. |
||
52 | * |
||
53 | * @param int $limit |
||
54 | * @return mixed |
||
55 | */ |
||
56 | public function limit($limit = 0) |
||
57 | { |
||
58 | $this->limit = $limit; |
||
59 | |||
60 | return $this; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @return int |
||
65 | */ |
||
66 | public function getLimit() |
||
70 | |||
71 | /** |
||
72 | * Set selection offset. |
||
73 | * |
||
74 | * @param int $offset |
||
75 | * @return mixed |
||
76 | */ |
||
77 | public function offset($offset = 0) |
||
78 | { |
||
79 | $this->offset = $offset; |
||
80 | |||
81 | return $this; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @return int |
||
86 | */ |
||
87 | public function getOffset() |
||
91 | |||
92 | /** |
||
93 | * Manually set paginator instance for specific object. |
||
94 | * |
||
95 | * @param PaginatorInterface $paginator |
||
96 | * @return $this |
||
97 | */ |
||
98 | public function setPaginator(PaginatorInterface $paginator) |
||
99 | { |
||
100 | $this->paginator = $paginator; |
||
101 | |||
102 | return $this; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Get paginator for the current selection. Paginate method should be already called. |
||
107 | * |
||
108 | * @see isPaginated() |
||
109 | * @see paginate() |
||
110 | * @return PaginatorInterface|Paginator|null |
||
111 | */ |
||
112 | public function paginator() |
||
116 | |||
117 | /** |
||
118 | * Get paginator for the current selection. Paginate method should be already called. |
||
119 | * |
||
120 | * @see isPaginated() |
||
121 | * @see paginate() |
||
122 | * @return PaginatorInterface|Paginator|null |
||
123 | */ |
||
124 | public function getPaginator() |
||
128 | |||
129 | /** |
||
130 | * Indication that object was paginated. |
||
131 | * |
||
132 | * @return bool |
||
133 | */ |
||
134 | public function isPaginated() |
||
138 | |||
139 | /** |
||
140 | * Paginate current selection using Paginator class. |
||
141 | * |
||
142 | * @param int $limit Pagination limit. |
||
143 | * @param string $pageParameter Name of parameter in request query which is |
||
144 | * used to store the current page number. "page" |
||
145 | * by default. |
||
146 | * @param ServerRequestInterface $request Has to be specified if no global container set. |
||
147 | * @return $this |
||
148 | * @throws PaginationException |
||
149 | */ |
||
150 | public function paginate( |
||
190 | |||
191 | /** |
||
192 | * Apply pagination to current object. Will be applied only if internal paginator already |
||
193 | * constructed. |
||
194 | * |
||
195 | * @return $this |
||
196 | * @throws PaginationException |
||
197 | */ |
||
198 | protected function applyPagination() |
||
206 | |||
207 | /** |
||
208 | * @return ContainerInterface |
||
209 | */ |
||
210 | abstract protected function container(); |
||
211 | } |
||
212 |