Completed
Push — master ( 6c4e57...0b7137 )
by Peter
21:21
created

Pagination::setSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Mangan;
10
11
use Maslosoft\Mangan\Interfaces\Criteria\LimitableInterface;
12
use Maslosoft\Mangan\Interfaces\PaginationInterface;
13
14
/**
15
 * Basic pagination class
16
 *
17
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
18
 */
19
class Pagination implements PaginationInterface
20
{
21
22
	private $size = PaginationInterface::DefaultPageSize;
23
	private $page = PaginationInterface::FirstPageId;
24
	private $total = 0;
25
26
	public function apply(LimitableInterface $criteria)
27
	{
28
		$criteria->setLimit($this->getLimit());
29
		$criteria->setOffset($this->getOffset());
30
	}
31
32
	public function getPages()
33
	{
34
		return intval(ceil($this->total / $this->size));
35
	}
36
37
	public function setCount($total)
38
	{
39
		$this->total = $total;
40
		return $this;
41
	}
42
43
	public function getSize()
44
	{
45
		return $this->size;
46
	}
47
48
	public function getPage()
49
	{
50
		return $this->page;
51
	}
52
53
	public function setSize($size)
54
	{
55
		$this->size = $size;
56
		return $this;
57
	}
58
59
	public function setPage($page)
60
	{
61
		$this->page = $page;
62
		return $this;
63
	}
64
65
	public function getLimit()
66
	{
67
		return $this->getSize();
68
	}
69
70
	public function getOffset()
71
	{
72
		// Pages are indexed from one, so substract 1 here
73
		return ($this->getPage() - 1) * $this->getSize();
74
	}
75
76
}
77