DbPaginator::getCurrPage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class for data pagination
4
 *
5
 * @file      DbPaginator.php
6
 *
7
 * PHP version 8.0+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2021 Alexander Yancharuk
11
 * @date      Втр Авг 07 23:04:47 2012
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\DataBase;
17
18
use stdClass;
19
use Veles\Tools\SnippetBuilder;
20
21
/**
22
 * Class DbPaginator
23
 *
24
 * Additional data for rendering can be stored as public params.
25
 * For this purpose class extends stdClass.
26
 *
27
 * @author  Alexander Yancharuk <alex at itvault dot info>
28
 */
29
class DbPaginator extends stdClass
30
{
31
	public $offset = 1;
32
	public $limit  = 5;
33
	public $page_nums;
34
	public $curr_page;
35
	public $template;
36
	public $first_link;
37
	public $last_link;
38
	public $index;
39
40
	/**
41
	 * Constructor
42
	 * @param string $template Path to template
43
	 * @param int $curr_page Current page
44
	 */
45 10
	public function __construct($template, $curr_page = 1)
46
	{
47 10
		$this->template  = $template;
48 10
		$this->curr_page = (int) $curr_page;
49
	}
50
51
	/**
52
	 * Pagination rendering
53
	 */
54 1
	public function __toString()
55
	{
56 1
		$this->first_link = false;
57 1
		$this->last_link  = false;
58 1
		$this->index      = 1;
59
60 1
		if ($this->curr_page > 4) {
61 1
			$this->first_link = 1;
62 1
			$this->index = $this->curr_page - 3;
63
		}
64
65 1
		if ($this->page_nums > $this->curr_page + 3) {
66 1
			$this->last_link = $this->page_nums;
67 1
			$this->page_nums = $this->curr_page + 3;
68
		}
69
70 1
		$builder = new SnippetBuilder($this);
71
72 1
		return $builder->build($this->template);
73
	}
74
75
	/**
76
	 * Getting offset
77
	 * @return int
78
	 */
79 7
	public function getOffset()
80
	{
81 7
		return ($this->curr_page - 1) * $this->getLimit();
82
	}
83
84
	/**
85
	 * Getting limit
86
	 * @return int
87
	 */
88 11
	public function getLimit()
89
	{
90 11
		return $this->limit;
91
	}
92
93
	/**
94
	 * Elements per page setting
95
	 * @param mixed $limit Кол-во выводимых элементов на странице
96
	 */
97 4
	public function setLimit($limit)
98
	{
99 4
		if (!is_numeric($limit)) {
100 1
			return;
101
		}
102
103 3
		$this->limit = (int) $limit;
104
	}
105
106
	/**
107
	 * Getting limit for sql-request
108
	 * @return string
109
	 */
110 6
	public function getSqlLimit()
111
	{
112 6
		$offset = $this->getOffset();
113 6
		$limit  = $this->getLimit();
114 6
		return " LIMIT $offset, $limit";
115
	}
116
117
	/**
118
	 * Pages quantity
119
	 *
120
	 * @throws \Exception
121
	 */
122 1
	public function getMaxPages()
123
	{
124 1
		if (null !== $this->page_nums) {
125 1
			return $this->page_nums;
126
		}
127
128 1
		$this->calcMaxPages();
129
130 1
		return $this->page_nums;
131
	}
132
133
	/**
134
	 * Pages quantity calculation
135
	 *
136
	 * @throws \Exception
137
	 */
138 5
	public function calcMaxPages()
139
	{
140 5
		$this->page_nums = (int) ceil(Db::getFoundRows() / $this->getLimit());
141
	}
142
143
	/**
144
	 * Getting current page
145
	 */
146 1
	public function getCurrPage()
147
	{
148 1
		return $this->curr_page;
149
	}
150
}
151