Completed
Push — master ( 80faa7...c6edab )
by Jean-Christophe
05:15
created

Pagination::getObjects()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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