Completed
Push — master ( f99fed...57dceb )
by Jean-Christophe
03:29
created

Pagination::setPagesVisibles()   A

Complexity

Conditions 2
Paths 2

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 2
eloc 5
nc 2
nop 1
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
	private $row_count;
11
12
	public function __construct($items_per_page=10,$pages_visibles=null,$page=1,$row_count=null){
13
		$this->items_per_page=$items_per_page;
14
		$this->row_count=$row_count;
15
		$this->page=$page;
16
		$this->setPagesVisibles($pages_visibles);
17
		$this->visible=true;
18
	}
19
20
	public function getObjects($objects){
21
		$auto=(!isset($this->row_count));
22
		$os=$objects;
23
		if(!\is_array($os)){
24
			$os=[];
25
			foreach ($objects as $o){
26
				$os[]=$o;
27
			}
28
		}
29
		$this->page_count = 0;
30
		$row_count=($auto)?\sizeof($os):$this->row_count;
31
		if (0 === $row_count) {
32
			$this->visible=false;
33
		} else {
34
			$this->visible=true;
35
			$this->page_count = (int)ceil($row_count / $this->items_per_page);
36
			if($this->page > $this->page_count+1) {
37
				$this->page = 1;
38
			}
39
		}
40
		if($auto){
41
			$offset = ($this->page - 1) * $this->items_per_page;
42
			return array_slice($os, $offset,$this->items_per_page);
43
		}
44
		return $os;
45
	}
46
47
	public function getItemsPerPage() {
48
		return $this->items_per_page;
49
	}
50
51
	public function setItemsPerPage($items_per_page) {
52
		$this->items_per_page=$items_per_page;
53
		return $this;
54
	}
55
56
	public function getPage() {
57
		return $this->page;
58
	}
59
60
	public function setPage($page) {
61
		$this->page=$page;
62
		return $this;
63
	}
64
65
	public function getVisible() {
66
		return $this->visible;
67
	}
68
69
	public function setVisible($visible) {
70
		$this->visible=$visible;
71
		return $this;
72
	}
73
74
	public function getPageCount() {
75
		return $this->page_count;
76
	}
77
78
	public function getPagesNumbers(){
79
		$middle= (int)ceil(($this->pages_visibles-1)/ 2);
80
		$first=$this->page-$middle;
81
		if($first<1){
82
			$first=1;
83
		}
84
		$last=$first+$this->pages_visibles-1;
85
		if($last>$this->page_count){
86
			$last=$this->page_count;
87
		}
88
		return \range($first, $last);
89
	}
90
91
	public function setPagesVisibles($pages_visibles) {
92
		if(!isset($pages_visibles))
93
			$pages_visibles=(int)ceil($this->row_count / $this->items_per_page)+1;
94
		$this->pages_visibles=$pages_visibles;
95
		return $this;
96
	}
97
98
99
}