-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathLocationSearchHitAdapter.php
79 lines (65 loc) · 2.26 KB
/
LocationSearchHitAdapter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\Core\Pagination\Pagerfanta;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
use eZ\Publish\API\Repository\SearchService;
use Pagerfanta\Adapter\AdapterInterface;
/**
* Pagerfanta adapter for eZ Publish location search.
* Will return results as SearchHit objects.
*/
class LocationSearchHitAdapter implements AdapterInterface
{
/** @var \eZ\Publish\API\Repository\Values\Content\LocationQuery */
private $query;
/** @var \eZ\Publish\API\Repository\SearchService */
private $searchService;
/** @var array */
private $languageFilter;
/** @var int */
private $nbResults;
public function __construct(LocationQuery $query, SearchService $searchService, array $languageFilter = [])
{
$this->query = $query;
$this->searchService = $searchService;
$this->languageFilter = $languageFilter;
}
/**
* Returns the number of results.
*
* @return int The number of results.
*/
public function getNbResults()
{
if (isset($this->nbResults)) {
return $this->nbResults;
}
$countQuery = clone $this->query;
$countQuery->limit = 0;
return $this->nbResults = $this->searchService->findLocations($countQuery)->totalCount;
}
/**
* Returns a slice of the results, as SearchHit objects.
*
* @param int $offset The offset.
* @param int $length The length.
*
* @return \eZ\Publish\API\Repository\Values\Content\Search\SearchHit[]
*/
public function getSlice($offset, $length)
{
$query = clone $this->query;
$query->offset = $offset;
$query->limit = $length;
$query->performCount = false;
$searchResult = $this->searchService->findLocations($query, $this->languageFilter);
// Set count for further use if returned by search engine despite !performCount (Solr, ES)
if (!isset($this->nbResults) && isset($searchResult->totalCount)) {
$this->nbResults = $searchResult->totalCount;
}
return $searchResult->searchHits;
}
}