Completed
Push — master ( 3567ad...cbc564 )
by Michael
02:57
created

Pagination   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 80
ccs 17
cts 17
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A count() 0 4 1
A getIterator() 0 4 1
A get() 0 4 1
A toArray() 0 4 1
A getItemsPerPage() 0 4 1
A setItemsPerPage() 0 4 1
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