1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mav3rick177\RapidPagination; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Collection; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
8
|
|
|
use Illuminate\Support\Traits\Macroable; |
9
|
|
|
use mav3rick177\RapidPagination\Base\AbstractProcessor; |
10
|
|
|
use mav3rick177\RapidPagination\Base\Query; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Processor |
14
|
|
|
* |
15
|
|
|
* @see AbstractProcessor |
16
|
|
|
*/ |
17
|
|
|
class Processor extends AbstractProcessor |
18
|
|
|
{ |
19
|
|
|
use Macroable; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var mixed |
23
|
|
|
*/ |
24
|
|
|
protected $builder; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get result. |
28
|
|
|
* |
29
|
|
|
* @param Query $query |
30
|
|
|
* @param Collection|Model[]|object[] $rows |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
2 |
|
public function process(Query $query, $rows) |
34
|
|
|
{ |
35
|
2 |
|
$this->builder = $query->builder(); |
36
|
2 |
|
return parent::process($query, $rows); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Return comparable value from a row. |
41
|
|
|
* |
42
|
|
|
* @param mixed $row |
43
|
|
|
* @param string $column |
44
|
|
|
* @return int|string |
45
|
|
|
*/ |
46
|
|
|
protected function field($row, $column) |
47
|
|
|
{ |
48
|
|
|
$column = static::dropTablePrefix($column); |
49
|
|
|
if ($this->builder instanceof BelongsToMany && strpos($column, 'pivot_') === 0) { |
50
|
|
|
return $this->pivotField($row, substr($column, 6), $this->pivotAccessor()); |
51
|
|
|
} |
52
|
|
|
$value = $row->$column; |
53
|
|
|
return is_object($value) ? (string)$value : $value; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Extract pivot from a row. |
58
|
|
|
* |
59
|
|
|
* @param mixed $row |
60
|
|
|
* @param string $column |
61
|
|
|
* @param string $accessor |
62
|
|
|
* @throws \Exception |
63
|
|
|
* @return int|string |
64
|
|
|
*/ |
65
|
|
|
protected function pivotField($row, $column, $accessor) |
66
|
|
|
{ |
67
|
|
|
$pivot = $row->$accessor; |
68
|
|
|
if (!isset($pivot->$column)) { |
69
|
|
|
throw new \Exception("The column `$column` is not included in the pivot \"$accessor\"."); |
70
|
|
|
} |
71
|
|
|
return $this->field($pivot, $column); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Extract pivot accessor from a relation. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
protected function pivotAccessor() |
80
|
|
|
{ |
81
|
|
|
return $this->builder->getPivotAccessor(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Return the n-th element of collection. |
86
|
|
|
* Must return null if not exists. |
87
|
|
|
* |
88
|
|
|
* @param Collection|Model[]|object[] $rows |
89
|
|
|
* @param int $offset |
90
|
|
|
* @return Model|object |
91
|
|
|
*/ |
92
|
2 |
|
protected function offset($rows, $offset) |
93
|
|
|
{ |
94
|
2 |
|
return isset($rows[$offset]) ? $rows[$offset] : null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Slice rows, like PHP function array_slice(). |
99
|
|
|
* |
100
|
|
|
* @param Collection|Model[]|object[] $rows |
101
|
|
|
* @param int $offset |
102
|
|
|
* @param null|int $length |
103
|
|
|
* @return Collection|Model[]|object[] |
104
|
|
|
*/ |
105
|
|
|
protected function slice($rows, $offset, $length = null) |
106
|
|
|
{ |
107
|
|
|
return $rows->slice($offset, $length)->values(); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Count rows, like PHP function count(). |
112
|
|
|
* |
113
|
|
|
* @param Collection|Model[]|object[] $rows |
114
|
|
|
* @return int |
115
|
|
|
*/ |
116
|
2 |
|
protected function count($rows) |
117
|
|
|
{ |
118
|
2 |
|
return $rows->count(); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Reverse rows, like PHP function array_reverse(). |
123
|
|
|
* |
124
|
|
|
* @param Collection|Model[]|object[] $rows |
125
|
|
|
* @return Collection|Model[]|object[] |
126
|
|
|
*/ |
127
|
|
|
protected function reverse($rows) |
128
|
|
|
{ |
129
|
|
|
return $rows->reverse()->values(); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Format result. |
134
|
|
|
* |
135
|
|
|
* @param Collection|Model[]|object[] $rows |
136
|
|
|
* @param array $meta |
137
|
|
|
* @param Query $query |
138
|
|
|
* @return PaginationResult |
139
|
|
|
*/ |
140
|
|
|
protected function defaultFormat($rows, array $meta, Query $query) |
141
|
|
|
{ |
142
|
|
|
return new PaginationResult($rows, $meta); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Drop table prefix on column name. |
147
|
|
|
* |
148
|
|
|
* @param string $column |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
protected static function dropTablePrefix(string $column) |
152
|
|
|
{ |
153
|
|
|
$segments = explode('.', $column); |
154
|
|
|
|
155
|
|
|
return end($segments); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: