-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSearchQueryMapper.php
More file actions
119 lines (99 loc) · 3.57 KB
/
SearchQueryMapper.php
File metadata and controls
119 lines (99 loc) · 3.57 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* Created by PhpStorm.
* User: bdunogier
* Date: 21/09/2018
* Time: 16:50
*/
namespace BD\EzPlatformGraphQLBundle\GraphQL\InputMapper;
use eZ\Publish\API\Repository\Values\Content\Query;
use InvalidArgumentException;
class SearchQueryMapper
{
/**
* @param array $inputArray
* @return \eZ\Publish\API\Repository\Values\Content\Query
*/
public function mapInputToQuery(array $inputArray)
{
$query = new Query();
if (isset($inputArray['offset'])) {
$query->offset = $inputArray['offset'];
}
if (isset($inputArray['limit'])) {
$query->limit = $inputArray['limit'];
}
$criteria = [];
if (isset($inputArray['ContentTypeIdentifier'])) {
$criteria[] = new Query\Criterion\ContentTypeIdentifier($inputArray['ContentTypeIdentifier']);
}
if (isset($inputArray['Text'])) {
foreach ($inputArray['Text'] as $text) {
$criteria[] = new Query\Criterion\FullText($text);
}
}
if (isset($inputArray['Field']))
{
$args = $inputArray['Field'];
foreach (['in', 'eq', 'like', 'contains', 'between', 'lt', 'lte', 'gt', 'gte'] as $opString) {
if (isset($args[$opString])) {
$value = $args[$opString];
$operator = constant('eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator::' . strtoupper($opString));
}
}
if (!isset($operator)) {
throw new InvalidArgumentException("Unspecified operator");
}
$criteria[] = new Query\Criterion\Field($args['target'], $operator, $value);
}
$criteria = array_merge($criteria, $this->mapDateMetadata($inputArray, 'Modified'));
$criteria = array_merge($criteria, $this->mapDateMetadata($inputArray, 'Created'));
if (count($criteria) === 0) {
return $query;
}
if (count($criteria)) {
$query->filter = count($criteria) > 1 ? new Query\Criterion\LogicalAnd($criteria) : $criteria[0];
}
if (isset($inputArray['sortBy'])) {
$query->sortClauses = [new $inputArray['sortBy']];
}
return $query;
}
/**
* @param array $queryArg
* @param $dateMetadata
* @return \eZ\Publish\API\Repository\Values\Content\Query\Criterion\DateMetadata[]
*/
private function mapDateMetadata(array $queryArg = [], $dateMetadata)
{
if (!isset($queryArg[$dateMetadata]) || !is_array($queryArg[$dateMetadata])) {
return [];
}
$targetMap = [
'Created' => Query\Criterion\DateMetadata::CREATED,
'Modified' => Query\Criterion\DateMetadata::MODIFIED,
];
if (!isset($targetMap[$dateMetadata])) {
echo "Not a date metadata\n";
return [];
}
$dateOperatorsMap = [
'on' => Query\Criterion\Operator::EQ,
'before' => Query\Criterion\Operator::LTE,
'after' => Query\Criterion\Operator::GTE,
];
$criteria = [];
foreach ($queryArg[$dateMetadata] as $operator => $dateString) {
if (!isset($dateOperatorsMap[$operator])) {
echo "Not a valid operator\n";
continue;
}
$criteria[] = new Query\Criterion\DateMetadata(
$targetMap[$dateMetadata],
$dateOperatorsMap[$operator],
strtotime($dateString)
);
}
return $criteria;
}
}