|
| 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 | +} |
0 commit comments