1 | <?php |
||||||
2 | /** |
||||||
3 | * Paged builder |
||||||
4 | * User: moyo |
||||||
5 | * Date: 2018/4/19 |
||||||
6 | * Time: 2:34 PM |
||||||
7 | */ |
||||||
8 | |||||||
9 | namespace Carno\Database\SQL\Paginator; |
||||||
10 | |||||||
11 | trait Paged |
||||||
12 | { |
||||||
13 | /** |
||||||
14 | * default page size |
||||||
15 | * @var int |
||||||
16 | */ |
||||||
17 | private $pSize = 20; |
||||||
18 | |||||||
19 | /** |
||||||
20 | * @param mixed ...$input |
||||||
21 | * @return Pagination |
||||||
22 | */ |
||||||
23 | public function paged(...$input) |
||||||
24 | { |
||||||
25 | list($page, $size) = |
||||||
26 | count($input) === 1 && is_object($input[0]) |
||||||
27 | ? $this->import($input[0]) |
||||||
28 | : [$input[0] ?? 1, $input[1] ?? $this->pSize] |
||||||
29 | ; |
||||||
30 | |||||||
31 | $total = yield $this->count(); |
||||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() It seems like
count() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
32 | |||||||
33 | $last = ceil($total / $size); |
||||||
34 | |||||||
35 | $next = min($page + 1, $last); |
||||||
36 | $prev = max($page - 1, 1); |
||||||
37 | |||||||
38 | $this->limit(($page - 1) * $size, $size); |
||||||
0 ignored issues
–
show
It seems like
limit() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
39 | |||||||
40 | return new Pagination($total, $size, $prev, $page, $next, $last, yield $this->list()); |
||||||
0 ignored issues
–
show
It seems like
list() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() $last of type double is incompatible with the type integer expected by parameter $last of Carno\Database\SQL\Pagin...gination::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
41 | } |
||||||
42 | |||||||
43 | /** |
||||||
44 | * @param object $source |
||||||
45 | * @return array |
||||||
46 | */ |
||||||
47 | private function import(object $source) : array |
||||||
48 | { |
||||||
49 | $page = $size = null; |
||||||
50 | |||||||
51 | if (method_exists($source, 'getPage')) { |
||||||
52 | $page = (int)$source->getPage(); |
||||||
53 | } |
||||||
54 | |||||||
55 | if (method_exists($source, 'getSize')) { |
||||||
56 | $size = (int)$source->getSize(); |
||||||
57 | } |
||||||
58 | |||||||
59 | return [$page ?: 1, $size ?: $this->pSize]; |
||||||
60 | } |
||||||
61 | } |
||||||
62 |