1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the miBadger package. |
5
|
|
|
* |
6
|
|
|
* @author Michael Webbers <[email protected]> |
7
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache v2 License |
8
|
|
|
* @version 1.0.0 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace miBadger\Pagination; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The pagination class. |
15
|
|
|
* |
16
|
|
|
* @since 1.0.0 |
17
|
|
|
*/ |
18
|
|
|
class Pagination implements \Countable, \IteratorAggregate |
19
|
|
|
{ |
20
|
|
|
const DEFAULT_ITEMS_PER_PAGE = 10; |
21
|
|
|
|
22
|
|
|
/** @var mixed[] The items */ |
23
|
|
|
private $items; |
24
|
|
|
|
25
|
|
|
/** @var int The number of items */ |
26
|
|
|
private $itemsPerPage; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Construct a pagiantion object with the given items en items per page. |
30
|
|
|
* |
31
|
|
|
* @param mixed[] $items |
32
|
|
|
* @param int $itemsPerPage |
33
|
|
|
*/ |
34
|
6 |
|
public function __construct(array $items, $itemsPerPage = self::DEFAULT_ITEMS_PER_PAGE) |
35
|
|
|
{ |
36
|
6 |
|
$this->items = $items; |
37
|
6 |
|
$this->itemsPerPage = $itemsPerPage; |
38
|
6 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
1 |
|
public function count() |
44
|
|
|
{ |
45
|
1 |
|
return ceil(count($this->items) / $this->itemsPerPage); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
1 |
|
public function getIterator() |
52
|
|
|
{ |
53
|
1 |
|
return new \ArrayIterator(array_chunk($this->items, $this->itemsPerPage)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the items on the given page number. |
58
|
|
|
* |
59
|
|
|
* @param int $index |
60
|
|
|
* @return mixed[] the items on the given page number. |
61
|
|
|
*/ |
62
|
1 |
|
public function get($index) |
63
|
|
|
{ |
64
|
1 |
|
return array_slice($this->items, $index * $this->itemsPerPage, $this->itemsPerPage); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns an array containing all of the elements in this pagination object. |
69
|
|
|
* |
70
|
|
|
* @return mixed[] an array containing all of the elements in this pagination object. |
71
|
|
|
*/ |
72
|
1 |
|
public function toArray() |
73
|
|
|
{ |
74
|
1 |
|
return $this->items; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the items per page. |
79
|
|
|
* |
80
|
|
|
* @return int the items per page. |
81
|
|
|
*/ |
82
|
2 |
|
public function getItemsPerPage() |
83
|
|
|
{ |
84
|
2 |
|
return $this->itemsPerPage; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns the items per page. |
89
|
|
|
* |
90
|
|
|
* @param $itemsPerPage = self::DEFAULT_ITEMS_PER_PAGE |
91
|
|
|
* @return null |
92
|
|
|
*/ |
93
|
1 |
|
public function setItemsPerPage($itemsPerPage = self::DEFAULT_ITEMS_PER_PAGE) |
94
|
|
|
{ |
95
|
1 |
|
$this->itemsPerPage = $itemsPerPage; |
96
|
1 |
|
} |
97
|
|
|
} |
98
|
|
|
|