1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Pagination; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Query; |
8
|
|
|
|
9
|
|
|
class CustomPaginatedCollection implements PaginatedCollectionInterface |
10
|
|
|
{ |
11
|
|
|
private $offset = 0; |
12
|
|
|
private $limit = 20; |
13
|
|
|
private $items; |
14
|
|
|
private $itemsCount; |
15
|
|
|
private $itemsIds; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* CustomPaginatedCollection constructor. |
19
|
|
|
* |
20
|
|
|
* @param Query $itemsQuery |
21
|
|
|
* @param Query $idsQuery |
22
|
|
|
* @param Query $countQuery |
23
|
|
|
* @param int $offset |
24
|
|
|
* @param int|null $limit |
25
|
|
|
* |
26
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
27
|
|
|
*/ |
28
|
36 |
|
public function __construct(Query $itemsQuery, Query $idsQuery, Query $countQuery, int $offset, ?int $limit = null) |
29
|
|
|
{ |
30
|
36 |
|
$this->offset = abs($offset); |
|
|
|
|
31
|
|
|
|
32
|
36 |
|
if ($limit !== null) { |
33
|
|
|
$this->limit = abs($limit); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
36 |
|
$idsQuery = $idsQuery->setFirstResult($this->offset)->setMaxResults($this->limit); |
37
|
36 |
|
$this->itemsIds = $idsQuery->getArrayResult(); |
38
|
36 |
|
$this->itemsCount = $countQuery->getSingleScalarResult(); |
39
|
36 |
|
$ids = $this->getIds($this->itemsIds); |
40
|
36 |
|
$itemsQuery->setParameter('ids', $ids); |
41
|
36 |
|
$this->items = $this->sortItems($itemsQuery->getArrayResult(), $ids); |
42
|
36 |
|
} |
43
|
|
|
|
44
|
36 |
|
public function getItems() |
45
|
|
|
{ |
46
|
36 |
|
return $this->items; |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
public function getItemsIds(): array |
50
|
|
|
{ |
51
|
2 |
|
return $this->itemsIds; |
52
|
|
|
} |
53
|
|
|
|
54
|
36 |
|
public function getTotal(): int |
55
|
|
|
{ |
56
|
36 |
|
return (int) $this->itemsCount; |
57
|
|
|
} |
58
|
|
|
|
59
|
36 |
|
public function getOffset(): int |
60
|
|
|
{ |
61
|
36 |
|
return $this->offset; |
62
|
|
|
} |
63
|
|
|
|
64
|
36 |
|
public function getLimit(): int |
65
|
|
|
{ |
66
|
36 |
|
return $this->limit; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* We are doing next things to improve performance: |
71
|
|
|
* 1. Fetch ids of movies in correct order, lets say [1, 2, 3] |
72
|
|
|
* 2. Then we are loading all information with joins for these movies: SELECT WITH JOINS ... WHERE id IN (1,2,3) |
73
|
|
|
* 3. As a result of step 2 we get movies but ordering is broken on this step: [3 => [data], 1 => [data], 2 => [data]] |
74
|
|
|
* 4. So that's why we need sort items again according to ids that we fetch on step 1. |
75
|
|
|
*/ |
76
|
36 |
|
private function sortItems(array $items, array $ids): array |
77
|
|
|
{ |
78
|
36 |
|
if (\count($ids) === 0) { |
79
|
5 |
|
return []; |
80
|
|
|
} |
81
|
|
|
|
82
|
33 |
|
$ids = array_flip($ids); |
83
|
|
|
usort($items, function (array $item1, array $item2) use ($ids) { |
84
|
27 |
|
return $ids[$item1['id']] <=> $ids[$item2['id']]; |
85
|
33 |
|
}); |
86
|
|
|
|
87
|
33 |
|
return $items; |
88
|
|
|
} |
89
|
|
|
|
90
|
36 |
|
private function getIds(array $ids): array |
91
|
|
|
{ |
92
|
36 |
|
if (\count($ids) === 0) { |
93
|
5 |
|
return []; |
94
|
|
|
} |
95
|
|
|
|
96
|
33 |
|
$firstValue = reset($ids); |
97
|
33 |
|
if (\is_array($firstValue) === true) { |
98
|
|
|
return array_map(function ($id) { |
99
|
33 |
|
return reset($id); |
100
|
33 |
|
}, $ids); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $ids; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.