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 Phauthentic\Pagination\PaginationParams; |
18
|
|
|
use Phauthentic\Pagination\PaginationParamsInterface; |
19
|
|
|
use Elastica\Query; |
20
|
|
|
use Elastica\Type; |
21
|
|
|
use InvalidArgumentException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Paginator for the ruflin/elastica library |
25
|
|
|
*/ |
26
|
|
|
class ElasticaPaginator implements PaginatorInterface |
27
|
|
|
{ |
28
|
|
|
const ORDER_ADD = 'add'; |
29
|
|
|
const ORDER_REPLACE = 'replace'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Elastica Type |
33
|
|
|
* |
34
|
|
|
* @var \Elastica\Type |
35
|
|
|
*/ |
36
|
|
|
protected $type; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Order Mode |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $orderMode = self::ORDER_ADD; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor |
47
|
|
|
* |
48
|
|
|
* @param \Elastica\Type $type Elastica Type |
49
|
|
|
*/ |
50
|
2 |
|
public function __construct(Type $type) |
51
|
|
|
{ |
52
|
2 |
|
$this->type = $type; |
53
|
2 |
|
} |
54
|
|
|
|
55
|
|
|
public function setOrderMode(string $mode): self |
56
|
|
|
{ |
57
|
|
|
if ($mode !== self::ORDER_ADD || $model !== self::ORDER_REPLACE) { |
|
|
|
|
58
|
|
|
throw new InvalidArgumentException(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->orderMode = $mode; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Maps the params to the ES query |
68
|
|
|
* |
69
|
|
|
* @param \Phauthentic\Pagination\PaginationParamsInterface $paginationParams Pagination params |
70
|
|
|
* @param array $array |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
2 |
|
public function paginate($query, PaginationParamsInterface $paginationParams) |
74
|
|
|
{ |
75
|
2 |
|
if (!$query instanceof Query) { |
76
|
|
|
throw new InvalidArgumentException(); |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
$query->setSize($paginationParams->getLimit()); |
80
|
2 |
|
$query->setFrom($paginationParams->getOffset()); |
81
|
|
|
|
82
|
2 |
|
$sortBy = $paginationParams->getSortBy(); |
83
|
2 |
|
if ($sortBy !== null) { |
84
|
|
|
$sort = [ |
85
|
1 |
|
$paginationParams->getSortBy() => [ |
86
|
1 |
|
'order' => $paginationParams->getDirection() |
87
|
|
|
] |
88
|
|
|
]; |
89
|
1 |
|
if ($this->orderMode === self::ORDER_ADD) { |
90
|
1 |
|
$query->addSort($sort); |
91
|
|
|
} else { |
92
|
|
|
$query->setSort($sort); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
$result = $this->type->search($query); |
97
|
|
|
|
98
|
2 |
|
$paginationParams->setCount($result->getTotalHits()); |
99
|
|
|
|
100
|
2 |
|
return $result; |
|
|
|
|
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|