Completed
Push — master ( 8e13bf...38fa9e )
by Arman
18s queued 15s
created

Paginator::firstItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Quantum\Libraries\Database\Idiorm;
4
5
use Quantum\Libraries\Database\BasePaginator;
6
use Quantum\Exceptions\DatabaseException;
7
use IdiormResultSet;
8
9
class Paginator extends BasePaginator
10
{
11
	/**
12
	 * @var IdiormDbal
13
	 */
14
	private $dbal;
15
16
	/**
17
	 * @var array|IdiormResultSet
18
	 */
19
	public $data;
20
21
	/**
22
	 * @param $idiormDbal
23
	 * @param int $perPage
24
	 * @param int $page
25
	 * @throws DatabaseException
26
	 */
27
	public function __construct($idiormDbal, int $perPage, int $page = 1)
28
	{
29
		/** @var IdiormDbal $idiormDbal */
30
		$this->total = $idiormDbal->getOrmModel()->count();
31
		$this->dbal = $idiormDbal;
32
		$this->dbal->limit($perPage)->offset($perPage * ($page - 1));
33
		$this->data = $this->dbal->getOrmModel()->find_many();
34
		$this->perPage = $perPage;
35
		$this->page = $page;
36
		$this->baseUrl = base_dir();
37
	}
38
39
	/**
40
	 * @return mixed
41
	 */
42
	public function firstItem()
43
	{
44
		if (!is_array($this->data)){
45
			$this->data = $this->data->as_array();
46
		}
47
		return $this->data[array_key_first($this->data)];
48
	}
49
50
	/**
51
	 * @return mixed
52
	 */
53
	public function lastItem()
54
	{
55
		if (!is_array($this->data)){
56
			$this->data = $this->data->as_array();
57
		}
58
		return $this->data[array_key_last($this->data)];
59
	}
60
61
	/**
62
	 * @return array|IdiormResultSet
63
	 */
64
	public function data()
65
	{
66
		return $this->data ?? [];
67
	}
68
}