Completed
Push — master ( 3555a5...e61355 )
by Jean-Christophe
03:33
created

Pagination::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
1
<?php
2
namespace Ajax\semantic\widgets\datatable;
3
4
class Pagination {
5
	private $items_per_page;
6
	private $page;
7
	private $visible;
8
	private $page_count;
9
	private $pages_visibles;
10
11
	public function __construct($items_per_page=10,$pages_visibles=4,$page=1){
12
		$this->items_per_page=$items_per_page;
13
		$this->page=$page;
14
		$this->pages_visibles=$pages_visibles;
15
		$this->visible=true;
16
	}
17
18
	public function getObjects($objects){
19
		$offset = ($this->page - 1) * $this->items_per_page;
20
		$os=$objects;
21
		if(\is_array($os)===false){
22
			$os=[];
23
			foreach ($objects as $o){
24
				$os[]=$o;
25
			}
26
		}
27
		$this->page_count = 0;
28
		$row_count=\sizeof($os);
29
		if (0 === $row_count) {
30
			$this->visible=false;
31
		} else {
32
			$this->visible=true;
33
			$this->page_count = (int)ceil($row_count / $this->items_per_page);
34
			if($this->page > $this->page_count+1) {
35
				$this->page = 1;
36
			}
37
		}
38
		return array_slice($os, $offset,$this->items_per_page);
39
	}
40
41
	public function getItemsPerPage() {
42
		return $this->items_per_page;
43
	}
44
45
	public function setItemsPerPage($items_per_page) {
46
		$this->items_per_page=$items_per_page;
47
		return $this;
48
	}
49
50
	public function getPage() {
51
		return $this->page;
52
	}
53
54
	public function setPage($page) {
55
		$this->page=$page;
56
		return $this;
57
	}
58
59
	public function getVisible() {
60
		return $this->visible;
61
	}
62
63
	public function setVisible($visible) {
64
		$this->visible=$visible;
65
		return $this;
66
	}
67
68
	public function getPageCount() {
69
		return $this->page_count;
70
	}
71
72
	public function getPagesNumbers(){
73
		$middle= (int)ceil(($this->pages_visibles-1)/ 2);
74
		$first=$this->page-$middle;
75
		if($first<1){
76
			$first=1;
77
		}
78
		$last=$first+$this->pages_visibles-1;
79
		if($last>$this->page_count){
80
			$last=$this->page_count;
81
		}
82
		return \range($first, $last);
83
	}
84
85
}