Skip to content

Commit

Permalink
Added FieldsQueryMapper
Browse files Browse the repository at this point in the history
Pre-processes the input for fields filters.
  • Loading branch information
Bertrand Dunogier committed Apr 6, 2019
1 parent 9a3669f commit f95b04a
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 2 deletions.
100 changes: 100 additions & 0 deletions src/GraphQL/InputMapper/FieldsQueryMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
namespace EzSystems\EzPlatformGraphQL\GraphQL\InputMapper;

use eZ\Publish\API\Repository\Values\Content\Query;
use EzSystems\EzPlatformGraphQL\GraphQL\DataLoader\ContentTypeLoader;
use GraphQL\Error\UserError;
use InvalidArgumentException;

/**
* Pre-processes the input to change fields passed using their identifier to the Field input key.
*/
class FieldsQueryMapper implements QueryMapper
{
/**
* @var QueryMapper
*/
private $innerMapper;
/**
* @var ContentTypeLoader
*/
private $contentTypeLoader;

public function __construct(ContentTypeLoader $contentTypeLoader, QueryMapper $innerMapper)
{
$this->innerMapper = $innerMapper;
$this->contentTypeLoader = $contentTypeLoader;
}

/**
* @param array $inputArray
* @return \eZ\Publish\API\Repository\Values\Content\Query
*/
public function mapInputToQuery(array $inputArray)
{
if (isset($inputArray['ContentTypeIdentifier'])) {
$contentType = $this->contentTypeLoader->loadByIdentifier($inputArray['ContentTypeIdentifier']);
$fieldsArgument = [];

foreach ($inputArray as $argument => $value) {
if (($fieldDefinition = $contentType->getFieldDefinition($argument)) === null) {
continue;
}

if (!$fieldDefinition->isSearchable) {
continue;
}

$fieldFilter = $this->buildFieldFilter($argument, $value);
if ($fieldFilter !== null) {
$fieldsArgument[] = $fieldFilter;
}
}

$queryArg['Fields'] = $fieldsArgument;
}

return $this->innerMapper->mapInputToQuery($inputArray);
}

private function buildFieldFilter($fieldDefinitionIdentifier, $value)
{
if (is_array($value) && count($value) === 1) {
$value = $value[0];
}
$operator = 'eq';

// @todo if 3 items, and first item is 'between', use next two items as value
if (is_array($value)) {
$operator = 'in';
} else if (is_string($value)) {
if ($value[0] === '~') {
$operator = 'like';
$value = substr($value, 1);
if (strpos($value, '%') === false) {
$value = "%$value%";
}
} elseif ($value[0] === '<') {
$value = substr($value, 1);
if ($value[0] === '=') {
$operator = 'lte';
$value = substr($value, 2);
} else {
$operator = 'lt';
$value = substr($value, 1);
}
} elseif ($value[0] === '<') {
$value = substr($value, 1);
if ($value[0] === '=') {
$operator = 'gte';
$value = substr($value, 2);
} else {
$operator = 'gt';
$value = substr($value, 1);
}
}
}

return ['target' => $fieldDefinitionIdentifier, $operator => trim($value)];
}
}
18 changes: 18 additions & 0 deletions src/GraphQL/InputMapper/QueryMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Created by PhpStorm.
* User: bdunogier
* Date: 07/04/2019
* Time: 00:07
*/

namespace EzSystems\EzPlatformGraphQL\GraphQL\InputMapper;

interface QueryMapper
{
/**
* @param array $inputArray
* @return \eZ\Publish\API\Repository\Values\Content\Query
*/
public function mapInputToQuery(array $inputArray);
}
2 changes: 1 addition & 1 deletion src/GraphQL/InputMapper/SearchQueryMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use GraphQL\Error\UserError;
use InvalidArgumentException;

class SearchQueryMapper
class SearchQueryMapper implements QueryMapper
{
/**
* @param array $inputArray
Expand Down
10 changes: 9 additions & 1 deletion src/Resources/config/services/search.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
services:
_defaults:
autoconfigure: true
autowire: true
public: false

EzSystems\EzPlatformGraphQL\DependencyInjection\Factory\SearchFeaturesFactory:
arguments:
$configurationProvider: '@ezpublish.api.repository_configuration_provider'
Expand All @@ -13,4 +18,7 @@ services:
arguments:
$converterRegistry: '@ezpublish.persistence.legacy.field_value_converter.registry'

EzSystems\EzPlatformGraphQL\Search\SolrSearchFeatures: ~
EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\FieldsQueryMapper:
decorates: EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\SearchQueryMapper
arguments:
$innerMapper: EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\FieldsQueryMapper./inner

0 comments on commit f95b04a

Please sign in to comment.