|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Quantum\Libraries\Database\Sleekdb; |
|
4
|
|
|
|
|
5
|
|
|
use SleekDB\Exceptions\InvalidConfigurationException; |
|
6
|
|
|
use SleekDB\Exceptions\InvalidArgumentException; |
|
7
|
|
|
use Quantum\Libraries\Database\BasePaginator; |
|
8
|
|
|
use Quantum\Exceptions\DatabaseException; |
|
9
|
|
|
use Quantum\Exceptions\ModelException; |
|
10
|
|
|
use SleekDB\Exceptions\IOException; |
|
11
|
|
|
|
|
12
|
|
|
class Paginator extends BasePaginator |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var SleekDbal |
|
16
|
|
|
*/ |
|
17
|
|
|
private $dbal; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
public $data; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param $sleekDbal |
|
26
|
|
|
* @param int $perPage |
|
27
|
|
|
* @param int $page |
|
28
|
|
|
* @throws DatabaseException |
|
29
|
|
|
* @throws ModelException |
|
30
|
|
|
* @throws IOException |
|
31
|
|
|
* @throws InvalidArgumentException |
|
32
|
|
|
* @throws InvalidConfigurationException |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct($sleekDbal, int $perPage, int $page = 1) |
|
35
|
|
|
{ |
|
36
|
|
|
/** @var SleekDbal $sleekDbal */ |
|
37
|
|
|
$this->total = count($sleekDbal->getBuilder()->getQuery()->fetch()); |
|
38
|
|
|
$this->dbal = $sleekDbal; |
|
39
|
|
|
$this->dbal->limit($perPage)->offset($perPage * ($page - 1)); |
|
40
|
|
|
$this->data = $this->dbal->getBuilder()->getQuery()->fetch(); |
|
41
|
|
|
$this->perPage = $perPage; |
|
42
|
|
|
$this->page = $page; |
|
43
|
|
|
$this->baseUrl = base_url(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
|
|
public function firstItem() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->data[array_key_first($this->data)]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
*/ |
|
57
|
|
|
public function lastItem() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->data[array_key_last($this->data)]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return array|SleekDbal[] |
|
64
|
|
|
*/ |
|
65
|
|
|
public function data(): array |
|
66
|
|
|
{ |
|
67
|
|
|
$result = array_map(function ($element) { |
|
68
|
|
|
$item = clone $this->dbal; |
|
69
|
|
|
$item->setData($element); |
|
70
|
|
|
$item->setModifiedFields($element); |
|
71
|
|
|
$item->setIsNew(false); |
|
72
|
|
|
return $item; |
|
73
|
|
|
}, $this->data); |
|
74
|
|
|
|
|
75
|
|
|
return $result ?? []; |
|
76
|
|
|
} |
|
77
|
|
|
} |