|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) Phauthentic (https://github.com/Phauthentic) |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under The MIT License |
|
6
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
7
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
8
|
|
|
* |
|
9
|
|
|
* @copyright Copyright (c) Phauthentic (https://github.com/Phauthentic) |
|
10
|
|
|
* @link https://github.com/Phauthentic |
|
11
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
|
12
|
|
|
*/ |
|
13
|
|
|
declare(strict_types = 1); |
|
14
|
|
|
|
|
15
|
|
|
namespace Phauthentic\Pagination\Paginator; |
|
16
|
|
|
|
|
17
|
|
|
use Cake\Datasource\QueryInterface; |
|
18
|
|
|
use InvalidArgumentException; |
|
19
|
|
|
use Phauthentic\Pagination\PaginationParamsInterface; |
|
20
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Pagination To Cake Orm Mapper |
|
24
|
|
|
*/ |
|
25
|
|
|
class CakeOrmPaginator implements PaginatorInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Maps the params to the repository |
|
29
|
|
|
* |
|
30
|
|
|
* @param \Phauthentic\Pagination\PaginationParamsInterface $paginationParams Pagination params |
|
31
|
|
|
* @param mixed $repository |
|
32
|
|
|
* @return mixed |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function paginate($repository, PaginationParamsInterface $paginationParams) |
|
35
|
|
|
{ |
|
36
|
|
|
/** @var \Cake\Database\Query $query */ |
|
37
|
1 |
|
$query = null; |
|
38
|
1 |
|
if ($repository instanceof QueryInterface) { |
|
39
|
|
|
$query = $repository; |
|
40
|
|
|
$object = $query->getRepository(); |
|
41
|
|
|
} else { |
|
42
|
1 |
|
$query = $repository->find(); |
|
43
|
1 |
|
$object = $repository; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
if (!$query instanceof QueryInterface) { |
|
47
|
|
|
throw new InvalidArgumentException(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
$count = $query->count(); |
|
51
|
1 |
|
$paginationParams->setCount($count); |
|
52
|
|
|
|
|
53
|
1 |
|
$sortBy = $paginationParams->getSortBy(); |
|
54
|
1 |
|
if ($sortBy !== null) { |
|
55
|
1 |
|
if (strpos($sortBy, '.') === false) { |
|
56
|
1 |
|
$sortBy = $object->aliasField($sortBy); |
|
57
|
|
|
} |
|
58
|
1 |
|
if ($paginationParams->getDirection() === 'desc') { |
|
59
|
1 |
|
$query->orderDesc($sortBy); |
|
|
|
|
|
|
60
|
|
|
} else { |
|
61
|
1 |
|
$query->orderAsc($sortBy); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $query |
|
66
|
1 |
|
->limit($paginationParams->getLimit()) |
|
67
|
1 |
|
->offSet($paginationParams->getOffSet()) |
|
68
|
1 |
|
->all(); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.