|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.9.7 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Paginator\Adapters; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Libraries\Database\Contracts\DbalInterface; |
|
18
|
|
|
use Quantum\Paginator\Contracts\PaginatorInterface; |
|
19
|
|
|
use Quantum\Paginator\Traits\PaginatorTrait; |
|
20
|
|
|
use Quantum\Model\ModelCollection; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class ModelPaginator |
|
24
|
|
|
* @package Quantum\Paginator |
|
25
|
|
|
*/ |
|
26
|
|
|
class ModelPaginator implements PaginatorInterface |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
use PaginatorTrait; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var DbalInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $ormInstance; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private $modelClass; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param DbalInterface $ormInstance |
|
43
|
|
|
* @param string $modelClass |
|
44
|
|
|
* @param int $perPage |
|
45
|
|
|
* @param int $page |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(DbalInterface $ormInstance, string $modelClass, int $perPage, int $page = 1) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->initialize($perPage, $page); |
|
50
|
|
|
$this->ormInstance = $ormInstance; |
|
51
|
|
|
$this->modelClass = $modelClass; |
|
52
|
|
|
$this->total = $this->ormInstance->count(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritDoc |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function fromArray(array $params): PaginatorInterface |
|
59
|
|
|
{ |
|
60
|
|
|
return new self( |
|
61
|
|
|
$params['orm'], |
|
62
|
|
|
$params['model'], |
|
63
|
|
|
$params['perPage'] ?? 10, |
|
64
|
|
|
$params['page'] ?? 1 |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @inheritDoc |
|
70
|
|
|
*/ |
|
71
|
|
|
public function data(): ModelCollection |
|
72
|
|
|
{ |
|
73
|
|
|
$ormInstances = $this->ormInstance |
|
74
|
|
|
->limit($this->perPage) |
|
75
|
|
|
->offset($this->perPage * ($this->page - 1)) |
|
76
|
|
|
->get(); |
|
77
|
|
|
|
|
78
|
|
|
$models = array_map(function ($item) { |
|
79
|
|
|
return wrapToModel($item, $this->modelClass); |
|
80
|
|
|
}, $ormInstances); |
|
81
|
|
|
|
|
82
|
|
|
return new ModelCollection($models); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @inheritDoc |
|
87
|
|
|
*/ |
|
88
|
|
|
public function firstItem() |
|
89
|
|
|
{ |
|
90
|
|
|
$data = $this->data(); |
|
91
|
|
|
|
|
92
|
|
|
if ($data->isEmpty()) { |
|
93
|
|
|
return null; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $data->first(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @inheritDoc |
|
101
|
|
|
*/ |
|
102
|
|
|
public function lastItem() |
|
103
|
|
|
{ |
|
104
|
|
|
$data = $this->data(); |
|
105
|
|
|
|
|
106
|
|
|
if ($data->isEmpty()) { |
|
107
|
|
|
return null; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $data->last(); |
|
111
|
|
|
} |
|
112
|
|
|
} |