1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NilPortugues\Foundation\Infrastructure\Model\Repository\EloquentMongoDB; |
4
|
|
|
|
5
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Page; |
6
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Pageable; |
7
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\PageRepository; |
8
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Page as ResultPage; |
9
|
|
|
|
10
|
|
|
class EloquentPageRepository extends BaseEloquentRepository implements PageRepository |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Returns a Page of entities meeting the paging restriction provided in the Pageable object. |
14
|
|
|
* |
15
|
|
|
* @param Pageable $pageable |
16
|
|
|
* |
17
|
|
|
* @return Page |
18
|
|
|
*/ |
19
|
|
|
public function findAll(Pageable $pageable = null) |
20
|
|
|
{ |
21
|
|
|
$model = self::$instance; |
22
|
|
|
$query = $model->query(); |
23
|
|
|
|
24
|
|
|
if ($pageable) { |
25
|
|
|
$fields = $pageable->fields(); |
26
|
|
|
$columns = (!$fields->isNull()) ? $fields->get() : ['*']; |
27
|
|
|
|
28
|
|
|
$filter = $pageable->filters(); |
29
|
|
|
if (!$filter->isNull()) { |
30
|
|
|
EloquentFilter::filter($query, $filter); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$sort = $pageable->sortings(); |
34
|
|
|
if (!$sort->isNull()) { |
35
|
|
|
EloquentSorter::sort($query, $sort); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$model = $model |
39
|
|
|
->take($pageable->pageSize()) |
40
|
|
|
->offset($pageable->pageSize() * ($pageable->pageNumber() - 1)); |
41
|
|
|
|
42
|
|
|
if (count($distinctFields = $pageable->distinctFields()->get()) > 0) { |
43
|
|
|
$model = $model->distinct(); |
44
|
|
|
$columns = $distinctFields; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return new ResultPage( |
48
|
|
|
$model->get($columns)->toArray(), |
49
|
|
|
$model->count(), |
50
|
|
|
$pageable->pageNumber(), |
51
|
|
|
ceil($query->paginate()->total() / $pageable->pageSize()) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return new ResultPage( |
56
|
|
|
$query->paginate($query->paginate()->total(), ['*'], 'page', 1)->items(), |
57
|
|
|
$query->paginate()->total(), |
58
|
|
|
1, |
59
|
|
|
1 |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.