-
Notifications
You must be signed in to change notification settings - Fork 16
Refactor query mapper #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 19 commits
0dbc81e
ffa7bf7
cb3f444
83bf501
8f0ad91
4639965
4b19c66
2279ce2
a0925a7
6d9b245
75ec18d
594c2a0
832969c
8ed08be
eea3c5c
05aa445
cd4cb88
a0b555e
4494d2a
e6fab24
7184393
37dcd75
acc7834
7b22af1
d4a0b70
4cf468b
af2fbf6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| namespace BD\EzPlatformGraphQLBundle\GraphQL\InputMapper\Search\Criterion; | ||
|
|
||
| use eZ\Publish\API\Repository\Values\Content\Query\CriterionInterface; | ||
| use eZ\Publish\API\Repository\Values\Content\Query; | ||
| use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; | ||
|
|
||
| class ContentTypeId implements SearchCriterion | ||
| { | ||
| public function map($value): array | ||
| { | ||
| return [new Query\Criterion\ContentTypeId($value)]; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| namespace BD\EzPlatformGraphQLBundle\GraphQL\InputMapper\Search\Criterion; | ||
|
|
||
| use eZ\Publish\API\Repository\Values\Content\Query; | ||
|
|
||
| class Created extends DateMetadata | ||
| { | ||
| const TARGET = Query\Criterion\DateMetadata::CREATED; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
|
|
||
| namespace BD\EzPlatformGraphQLBundle\GraphQL\InputMapper\Search\Criterion; | ||
|
|
||
| use eZ\Publish\API\Repository\Values\Content\Query; | ||
|
|
||
| class DateMetadata implements SearchCriterion | ||
| { | ||
| public function map($value): array | ||
| { | ||
| $dateOperatorsMap = [ | ||
| 'on' => Query\Criterion\Operator::EQ, | ||
| 'before' => Query\Criterion\Operator::LTE, | ||
| 'after' => Query\Criterion\Operator::GTE, | ||
| ]; | ||
|
|
||
| $criteria = []; | ||
| foreach ($value as $operator => $dateString) { | ||
| if (!isset($dateOperatorsMap[$operator])) { | ||
| echo "Not a valid operator\n"; | ||
crevillo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| continue; | ||
| } | ||
|
|
||
| $criteria[] = new Query\Criterion\DateMetadata( | ||
| static::TARGET, | ||
| $dateOperatorsMap[$operator], | ||
| strtotime($dateString) | ||
| ); | ||
| } | ||
|
|
||
| return $criteria; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <?php | ||
|
|
||
| namespace BD\EzPlatformGraphQLBundle\GraphQL\InputMapper\Search\Criterion; | ||
|
|
||
| use eZ\Publish\API\Repository\Values\Content\Query; | ||
|
|
||
| class Field implements SearchCriterion | ||
| { | ||
| public function map($value): array | ||
| { | ||
| $criteria = []; | ||
|
|
||
| if (isset($value['target'])) { | ||
| $criteria[] = $this->mapInputToFieldCriterion($value); | ||
| } else { | ||
| $criteria = array_merge( | ||
| $criteria, | ||
| array_map( | ||
| function($input) { | ||
| return $this->mapInputToFieldCriterion($input); | ||
| }, | ||
| $value | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| return $criteria; | ||
| } | ||
|
|
||
| private function mapInputToFieldCriterion($input) | ||
| { | ||
| $operators = ['in', 'eq', 'like', 'contains', 'between', 'lt', 'lte', 'gt', 'gte']; | ||
| foreach ($operators as $opString) { | ||
| if (isset($input[$opString])) { | ||
| $value = $input[$opString]; | ||
| $operator = constant('eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator::' . strtoupper($opString)); | ||
| } | ||
| } | ||
|
|
||
| if (!isset($operator)) { | ||
| throw new InvalidArgumentException("Unspecified operator"); | ||
| } | ||
|
|
||
| return new Query\Criterion\Field($input['target'], $operator, $value); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| namespace BD\EzPlatformGraphQLBundle\GraphQL\InputMapper\Search\Criterion; | ||
|
|
||
| use eZ\Publish\API\Repository\Values\Content\Query; | ||
|
|
||
| class Modified extends DateMetadata | ||
| { | ||
| const TARGET = Query\Criterion\DateMetadata::MODIFIED; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| namespace BD\EzPlatformGraphQLBundle\GraphQL\InputMapper\Search\Criterion; | ||
|
|
||
| use eZ\Publish\API\Repository\Values\Content\Query\CriterionInterface; | ||
| use eZ\Publish\API\Repository\Values\Content\Query; | ||
|
|
||
| class Text implements SearchCriterion | ||
| { | ||
| public function map($value): array | ||
| { | ||
| $criteria = []; | ||
crevillo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| foreach ($value as $text) { | ||
| $criteria[] = new Query\Criterion\FullText($text); | ||
| } | ||
|
|
||
| return $criteria; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
|
|
||
| namespace EzSystems\EzPlatformGraphQL\DependencyInjection\Compiler; | ||
|
|
||
| use EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\SearchQueryMapper; | ||
| use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
| use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
| use Symfony\Component\DependencyInjection\Reference; | ||
|
|
||
| class SearchQueryMappersPass implements CompilerPassInterface | ||
| { | ||
| const ID = SearchQueryMapper::class; | ||
|
||
|
|
||
| public function process(ContainerBuilder $container) | ||
| { | ||
| if (!$container->has(self::ID)) { | ||
| return; | ||
| } | ||
|
|
||
| $definition = $container->findDefinition(self::ID); | ||
| $taggedServices = $container->findTaggedServiceIds('ezplatform_graphql.query_input_visitor'); | ||
|
|
||
| $queryInputVisitors = []; | ||
| foreach ($taggedServices as $id => $tags) { | ||
| foreach ($tags as $tag) { | ||
| if (isset($tag['inputKey'])) { | ||
| $queryInputVisitors[$tag['inputKey']] = new Reference($id); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: the advantage of this is that a query input visitor can be overridden using the key. Useful. |
||
| } | ||
| } | ||
| } | ||
|
|
||
| $definition->setArgument('$queryInputVisitors', $queryInputVisitors); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| namespace EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\Search\Criterion; | ||
|
|
||
| use eZ\Publish\API\Repository\Values\Content\Query; | ||
|
|
||
| class Created extends DateMetadata | ||
| { | ||
| const TARGET = Query\Criterion\DateMetadata::CREATED; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| namespace EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\Search\Criterion; | ||
|
|
||
| use eZ\Publish\API\Repository\Values\Content\Query; | ||
| use EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\Search\QueryBuilder; | ||
| use EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\Search\QueryInputVisitor; | ||
|
|
||
| class DateMetadata implements QueryInputVisitor | ||
crevillo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| public function visit(QueryBuilder $queryBuilder, $value): void | ||
| { | ||
| $dateOperatorsMap = [ | ||
| 'on' => Query\Criterion\Operator::EQ, | ||
| 'before' => Query\Criterion\Operator::LTE, | ||
| 'after' => Query\Criterion\Operator::GTE, | ||
| ]; | ||
|
|
||
| foreach ($value as $operator => $dateString) { | ||
| if (!isset($dateOperatorsMap[$operator])) { | ||
| echo "Not a valid operator\n"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be changed to an exception. You could add a new one to the package, in |
||
| continue; | ||
| } | ||
|
|
||
| $queryBuilder->addCriterion(new Query\Criterion\DateMetadata( | ||
| static::TARGET, | ||
| $dateOperatorsMap[$operator], | ||
| strtotime($dateString) | ||
| )); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php | ||
|
|
||
| namespace EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\Search\Criterion; | ||
|
|
||
| use eZ\Publish\API\Repository\Values\Content\Query; | ||
| use EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\Search\QueryBuilder; | ||
| use EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\Search\QueryInputVisitor; | ||
|
|
||
| class Field implements QueryInputVisitor | ||
| { | ||
| public function visit(QueryBuilder $queryBuilder, $value): void | ||
| { | ||
| $criteria = []; | ||
|
|
||
| if (isset($value['target'])) { | ||
| $criteria[] = $this->mapInputToFieldCriterion($value); | ||
| } else { | ||
| $criteria = array_merge( | ||
| $criteria, | ||
| array_map( | ||
| function($input) { | ||
| return $this->mapInputToFieldCriterion($input); | ||
| }, | ||
| $value | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| foreach ($criteria as $criterion) { | ||
| $queryBuilder->addCriterion($criterion); | ||
| } | ||
| } | ||
|
|
||
| private function mapInputToFieldCriterion($input) | ||
| { | ||
| $operators = ['in', 'eq', 'like', 'contains', 'between', 'lt', 'lte', 'gt', 'gte']; | ||
| foreach ($operators as $opString) { | ||
| if (isset($input[$opString])) { | ||
| $value = $input[$opString]; | ||
| $operator = constant('eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator::' . strtoupper($opString)); | ||
| } | ||
| } | ||
|
|
||
| if (!isset($operator)) { | ||
| throw new InvalidArgumentException("Unspecified operator"); | ||
| } | ||
|
|
||
| return new Query\Criterion\Field($input['target'], $operator, $value); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| namespace EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\Search\Criterion; | ||
|
|
||
| use eZ\Publish\API\Repository\Values\Content\Query; | ||
|
|
||
| class Modified extends DateMetadata | ||
| { | ||
| const TARGET = Query\Criterion\DateMetadata::MODIFIED; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| namespace EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\Search\Criterion; | ||
|
|
||
| use EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\Search\QueryBuilder; | ||
| use EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\Search\QueryInputVisitor; | ||
|
|
||
| class Standard implements QueryInputVisitor | ||
crevillo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| private $criterionClass; | ||
|
|
||
| public function __construct($criterionClass) | ||
| { | ||
| $this->criterionClass = $criterionClass; | ||
| } | ||
|
|
||
| public function visit(QueryBuilder $queryBuilder, $value): void | ||
| { | ||
| $queryBuilder->addCriterion(new {$this->criterionClass}($value)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| namespace EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\Search; | ||
|
|
||
| use eZ\Publish\API\Repository\Values\Content\Query; | ||
|
|
||
| class QueryBuilder | ||
| { | ||
| /** | ||
| * @var array | ||
crevillo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| private $criterions = []; | ||
crevillo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** @var Query\SortClause */ | ||
| protected $sortBy; | ||
|
|
||
| public function addCriterion(Query\Criterion $criterion): void | ||
| { | ||
| $this->criterions[] = $criterion; | ||
| } | ||
|
|
||
| public function buildQuery(): Query | ||
| { | ||
|
|
||
| $query = new Query(); | ||
|
|
||
| if (count($this->criterions) === 0) { | ||
| return $query; | ||
| } | ||
|
|
||
| $query->filter = count($this->criterions) === 1 ? $this->criterions[0] : new Query\Criterion\LogicalAnd($this->criterions); | ||
|
|
||
| if ($this->sortBy) { | ||
| $query->sortClauses = $this->sortBy; | ||
| } | ||
|
|
||
| return $query; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| namespace EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\Search; | ||
|
|
||
| use EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\Search\QueryBuilder; | ||
|
|
||
| interface QueryInputVisitor | ||
| { | ||
| public function visit(QueryBuilder $queryBuilder, $value): void; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.