-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSearchResolver.php
More file actions
70 lines (59 loc) · 1.94 KB
/
SearchResolver.php
File metadata and controls
70 lines (59 loc) · 1.94 KB
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
<?php
namespace BD\EzPlatformGraphQLBundle\GraphQL\Relay;
use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection;
use Overblog\GraphQLBundle\Relay\Connection\Output\ConnectionBuilder;
class SearchResolver
{
/**
* @var SearchService
*/
private $searchService;
public function __construct(SearchService $searchService)
{
$this->searchService = $searchService;
}
/**
* @param $args
* @return Connection
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function searchContent($args)
{
$queryArg = $args['query'];
$query = new Query();
$criteria = [];
if (isset($queryArg['ContentTypeIdentifier'])) {
$criteria[] = new Query\Criterion\ContentTypeIdentifier($queryArg['ContentTypeIdentifier']);
}
if (isset($queryArg['Text'])) {
foreach ($queryArg['Text'] as $text) {
$criteria[] = new Query\Criterion\FullText($text);
}
}
if (count($criteria) === 0) {
return null;
}
$query->filter = count($criteria) > 1 ? new Query\Criterion\LogicalAnd($criteria) : $criteria[0];
$searchResult = $this->searchService->findContentInfo($query);
$contentItems = array_map(
function (SearchHit $hit) {
return $hit->valueObject;
},
$searchResult->searchHits
);
$connection = ConnectionBuilder::connectionFromArraySlice(
$contentItems,
$args,
[
'sliceStart' => 0,
'arrayLength' => $searchResult->totalCount,
]
);
$connection->sliceSize = count($contentItems);
return $connection;
}
}