-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bertrand Dunogier
committed
Aug 31, 2019
1 parent
46fa215
commit b211121
Showing
14 changed files
with
413 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?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 EzSystems\EzPlatformGraphQL\DependencyInjection\Factory; | ||
|
||
use eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider; | ||
|
||
class SearchFeaturesFactory | ||
{ | ||
/** | ||
* @var \eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider | ||
*/ | ||
private $configurationProvider; | ||
|
||
/** | ||
* @var \EzSystems\EzPlatformGraphQL\Search\SearchFeatures[] | ||
*/ | ||
private $searchFeatures = []; | ||
|
||
public function __construct(RepositoryConfigurationProvider $configurationProvider, array $searchFeatures) | ||
{ | ||
$this->configurationProvider = $configurationProvider; | ||
$this->searchFeatures = $searchFeatures; | ||
} | ||
|
||
public function build() | ||
{ | ||
$searchEngine = $this->configurationProvider->getRepositoryConfig()['search']['engine']; | ||
|
||
if (isset($this->searchFeatures[$searchEngine])) { | ||
return $this->searchFeatures[$searchEngine]; | ||
} else { | ||
throw new \InvalidArgumentException('Search engine not found'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?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 EzSystems\EzPlatformGraphQL\GraphQL\InputMapper; | ||
|
||
use EzSystems\EzPlatformGraphQL\GraphQL\DataLoader\ContentTypeLoader; | ||
|
||
/** | ||
* 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']) && isset($inputArray['fieldsFilters'])) { | ||
$contentType = $this->contentTypeLoader->loadByIdentifier($inputArray['ContentTypeIdentifier']); | ||
$fieldsArgument = []; | ||
|
||
foreach ($inputArray['fieldsFilters'] as $fieldDefinitionIdentifier => $value) { | ||
if (($fieldDefinition = $contentType->getFieldDefinition($fieldDefinitionIdentifier)) === null) { | ||
continue; | ||
} | ||
|
||
if (!$fieldDefinition->isSearchable) { | ||
continue; | ||
} | ||
|
||
$fieldFilter = $this->buildFieldFilter($fieldDefinitionIdentifier, $value); | ||
if ($fieldFilter !== null) { | ||
$fieldsArgument[] = $fieldFilter; | ||
} | ||
} | ||
|
||
$inputArray['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'; | ||
} elseif (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)]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?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 EzSystems\EzPlatformGraphQL\GraphQL\InputMapper; | ||
|
||
interface QueryMapper | ||
{ | ||
/** | ||
* @param array $inputArray | ||
* | ||
* @return \eZ\Publish\API\Repository\Values\Content\Query | ||
*/ | ||
public function mapInputToQuery(array $inputArray); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
services: | ||
_defaults: | ||
autoconfigure: true | ||
autowire: true | ||
public: false | ||
|
||
EzSystems\EzPlatformGraphQL\DependencyInjection\Factory\SearchFeaturesFactory: | ||
arguments: | ||
$configurationProvider: '@ezpublish.api.repository_configuration_provider' | ||
$searchFeatures: | ||
solr: '@EzSystems\EzPlatformGraphQL\Search\SolrSearchFeatures' | ||
legacy: '@EzSystems\EzPlatformGraphQL\Search\LegacySearchFeatures' | ||
|
||
|
||
EzSystems\EzPlatformGraphQL\Search\SearchFeatures: | ||
factory: ['@EzSystems\EzPlatformGraphQL\DependencyInjection\Factory\SearchFeaturesFactory', build] | ||
|
||
EzSystems\EzPlatformGraphQL\Search\SolrSearchFeatures: ~ | ||
|
||
EzSystems\EzPlatformGraphQL\Search\LegacySearchFeatures: | ||
arguments: | ||
$converterRegistry: '@ezpublish.persistence.legacy.field_value_converter.registry' | ||
|
||
EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\QueryMapper: '@EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\SearchQueryMapper' | ||
|
||
EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\SearchQueryMapper: ~ | ||
|
||
EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\FieldsQueryMapper: | ||
decorates: EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\SearchQueryMapper | ||
arguments: | ||
$innerMapper: '@EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\FieldsQueryMapper.inner' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Schema/Domain/Content/Worker/ContentType/DefineDomainContentFilter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?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 EzSystems\EzPlatformGraphQL\Schema\Domain\Content\Worker\ContentType; | ||
|
||
use eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroup; | ||
use EzSystems\EzPlatformGraphQL\Schema\Domain\Content\Worker\BaseWorker; | ||
use EzSystems\EzPlatformGraphQL\Schema\Worker; | ||
use EzSystems\EzPlatformGraphQL\Schema\Builder; | ||
use EzSystems\EzPlatformGraphQL\Schema\Builder\Input; | ||
use eZ\Publish\API\Repository\Values\ContentType\ContentType; | ||
|
||
class DefineDomainContentFilter extends BaseWorker implements Worker | ||
{ | ||
public function work(Builder $schema, array $args) | ||
{ | ||
$schema->addType(new Input\Type($this->filterType($args), 'input-object')); | ||
$schema->addArgToField( | ||
$this->groupType($args), | ||
$this->connectionField($args), | ||
new Input\Arg('filter', $this->filterType($args)) | ||
); | ||
} | ||
|
||
public function canWork(Builder $schema, array $args) | ||
{ | ||
return isset($args['ContentTypeGroup']) && $args['ContentTypeGroup'] instanceof ContentTypeGroup | ||
&& isset($args['ContentType']) && $args['ContentType'] instanceof ContentType | ||
&& !$schema->hasType($this->filterType($args)); | ||
} | ||
|
||
protected function filterType(array $args): string | ||
{ | ||
return $this->getNameHelper()->filterType($args['ContentType']); | ||
} | ||
|
||
protected function groupType(array $args): string | ||
{ | ||
return $this->getNameHelper()->domainGroupName($args['ContentTypeGroup']); | ||
} | ||
|
||
protected function connectionField(array $args): string | ||
{ | ||
return $this->getNameHelper()->domainContentCollectionField($args['ContentType']); | ||
} | ||
} |
Oops, something went wrong.