Skip to content

Commit f95b04a

Browse files
author
Bertrand Dunogier
committed
Added FieldsQueryMapper
Pre-processes the input for fields filters.
1 parent 9a3669f commit f95b04a

File tree

4 files changed

+128
-2
lines changed

4 files changed

+128
-2
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
namespace EzSystems\EzPlatformGraphQL\GraphQL\InputMapper;
3+
4+
use eZ\Publish\API\Repository\Values\Content\Query;
5+
use EzSystems\EzPlatformGraphQL\GraphQL\DataLoader\ContentTypeLoader;
6+
use GraphQL\Error\UserError;
7+
use InvalidArgumentException;
8+
9+
/**
10+
* Pre-processes the input to change fields passed using their identifier to the Field input key.
11+
*/
12+
class FieldsQueryMapper implements QueryMapper
13+
{
14+
/**
15+
* @var QueryMapper
16+
*/
17+
private $innerMapper;
18+
/**
19+
* @var ContentTypeLoader
20+
*/
21+
private $contentTypeLoader;
22+
23+
public function __construct(ContentTypeLoader $contentTypeLoader, QueryMapper $innerMapper)
24+
{
25+
$this->innerMapper = $innerMapper;
26+
$this->contentTypeLoader = $contentTypeLoader;
27+
}
28+
29+
/**
30+
* @param array $inputArray
31+
* @return \eZ\Publish\API\Repository\Values\Content\Query
32+
*/
33+
public function mapInputToQuery(array $inputArray)
34+
{
35+
if (isset($inputArray['ContentTypeIdentifier'])) {
36+
$contentType = $this->contentTypeLoader->loadByIdentifier($inputArray['ContentTypeIdentifier']);
37+
$fieldsArgument = [];
38+
39+
foreach ($inputArray as $argument => $value) {
40+
if (($fieldDefinition = $contentType->getFieldDefinition($argument)) === null) {
41+
continue;
42+
}
43+
44+
if (!$fieldDefinition->isSearchable) {
45+
continue;
46+
}
47+
48+
$fieldFilter = $this->buildFieldFilter($argument, $value);
49+
if ($fieldFilter !== null) {
50+
$fieldsArgument[] = $fieldFilter;
51+
}
52+
}
53+
54+
$queryArg['Fields'] = $fieldsArgument;
55+
}
56+
57+
return $this->innerMapper->mapInputToQuery($inputArray);
58+
}
59+
60+
private function buildFieldFilter($fieldDefinitionIdentifier, $value)
61+
{
62+
if (is_array($value) && count($value) === 1) {
63+
$value = $value[0];
64+
}
65+
$operator = 'eq';
66+
67+
// @todo if 3 items, and first item is 'between', use next two items as value
68+
if (is_array($value)) {
69+
$operator = 'in';
70+
} else if (is_string($value)) {
71+
if ($value[0] === '~') {
72+
$operator = 'like';
73+
$value = substr($value, 1);
74+
if (strpos($value, '%') === false) {
75+
$value = "%$value%";
76+
}
77+
} elseif ($value[0] === '<') {
78+
$value = substr($value, 1);
79+
if ($value[0] === '=') {
80+
$operator = 'lte';
81+
$value = substr($value, 2);
82+
} else {
83+
$operator = 'lt';
84+
$value = substr($value, 1);
85+
}
86+
} elseif ($value[0] === '<') {
87+
$value = substr($value, 1);
88+
if ($value[0] === '=') {
89+
$operator = 'gte';
90+
$value = substr($value, 2);
91+
} else {
92+
$operator = 'gt';
93+
$value = substr($value, 1);
94+
}
95+
}
96+
}
97+
98+
return ['target' => $fieldDefinitionIdentifier, $operator => trim($value)];
99+
}
100+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: bdunogier
5+
* Date: 07/04/2019
6+
* Time: 00:07
7+
*/
8+
9+
namespace EzSystems\EzPlatformGraphQL\GraphQL\InputMapper;
10+
11+
interface QueryMapper
12+
{
13+
/**
14+
* @param array $inputArray
15+
* @return \eZ\Publish\API\Repository\Values\Content\Query
16+
*/
17+
public function mapInputToQuery(array $inputArray);
18+
}

src/GraphQL/InputMapper/SearchQueryMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use GraphQL\Error\UserError;
66
use InvalidArgumentException;
77

8-
class SearchQueryMapper
8+
class SearchQueryMapper implements QueryMapper
99
{
1010
/**
1111
* @param array $inputArray

src/Resources/config/services/search.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
services:
2+
_defaults:
3+
autoconfigure: true
4+
autowire: true
5+
public: false
6+
27
EzSystems\EzPlatformGraphQL\DependencyInjection\Factory\SearchFeaturesFactory:
38
arguments:
49
$configurationProvider: '@ezpublish.api.repository_configuration_provider'
@@ -13,4 +18,7 @@ services:
1318
arguments:
1419
$converterRegistry: '@ezpublish.persistence.legacy.field_value_converter.registry'
1520

16-
EzSystems\EzPlatformGraphQL\Search\SolrSearchFeatures: ~
21+
EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\FieldsQueryMapper:
22+
decorates: EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\SearchQueryMapper
23+
arguments:
24+
$innerMapper: EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\FieldsQueryMapper./inner

0 commit comments

Comments
 (0)