|
1
|
|
|
<?php |
|
2
|
|
|
/******************************************************************************* |
|
3
|
|
|
* This file is part of the GraphQL Bundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) YnloUltratech <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
******************************************************************************/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Filter\Resolver; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
14
|
|
|
use Ynlo\GraphQLBundle\Annotation\Filter; |
|
15
|
|
|
use Ynlo\GraphQLBundle\Definition\ExecutableDefinitionInterface; |
|
16
|
|
|
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface; |
|
17
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
|
18
|
|
|
use Ynlo\GraphQLBundle\Filter\FilterResolverInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Resolve generic filters manually settled in the list config |
|
22
|
|
|
* |
|
23
|
|
|
* Example: |
|
24
|
|
|
* |
|
25
|
|
|
* QueryList( |
|
26
|
|
|
* filters={ |
|
27
|
|
|
* "*", |
|
28
|
|
|
* "title, body": @GraphQL\Filter(resolver="App\Filter\LikeFilter", type="string") |
|
29
|
|
|
* } |
|
30
|
|
|
* }) |
|
31
|
|
|
*/ |
|
32
|
|
|
class CustomGenericFilterResolver implements FilterResolverInterface |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var Reader |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $reader; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* CustomGenericFilterResolver constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param Reader $reader |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public function __construct(Reader $reader) |
|
45
|
|
|
{ |
|
46
|
1 |
|
$this->reader = $reader; |
|
47
|
1 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritDoc |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function resolve(ExecutableDefinitionInterface $executableDefinition, ObjectDefinitionInterface $node, Endpoint $endpoint): array |
|
53
|
|
|
{ |
|
54
|
1 |
|
$resolverFilters = []; |
|
55
|
1 |
|
if ($filters = $executableDefinition->getMeta('pagination')['filters'] ?? []) { |
|
56
|
1 |
|
foreach ($filters as $fields => $filter) { |
|
57
|
1 |
|
if ($filter instanceof Filter) { |
|
58
|
1 |
|
$fields = explode(',', $fields); |
|
59
|
1 |
|
foreach ($fields as $field) { |
|
60
|
1 |
|
$filter = clone $filter; |
|
61
|
1 |
|
$filter->name = $filter->field = trim($field); |
|
62
|
1 |
|
$resolverFilters[] = $filter; |
|
63
|
|
|
} |
|
64
|
1 |
|
} elseif (\is_string($filter) && class_exists($filter)) { |
|
65
|
1 |
|
$ref = new \ReflectionClass($filter); |
|
66
|
1 |
|
$filter = $this->reader->getClassAnnotation($ref, Filter::class); |
|
67
|
1 |
|
if ($filter) { |
|
68
|
1 |
|
$fields = explode(',', $fields); |
|
69
|
1 |
|
foreach ($fields as $field) { |
|
70
|
1 |
|
$filter = clone $filter; |
|
71
|
1 |
|
$filter->name = $filter->field = trim($field); |
|
72
|
1 |
|
$filter->resolver = $ref->getName(); |
|
73
|
1 |
|
$resolverFilters[] = $filter; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
return $resolverFilters; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|