Skip to content

Commit c7c765c

Browse files
authored
Merge pull request #1715 from mykidok/fix-multiple-similar-apifilter
Allow to declare multiple filters with same class
2 parents ea615b3 + 34ddf36 commit c7c765c

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

features/doctrine/search_filter.feature

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,30 @@ Feature: Search filter on collections
5050
},
5151
"hydra:search": {
5252
"@type": "hydra:IriTemplate",
53-
"hydra:template": "\/dummy_cars{?availableAt[before],availableAt[strictly_before],availableAt[after],availableAt[strictly_after],canSell,foobar[],foobargroups[],colors.prop,name}",
53+
"hydra:template": "\/dummy_cars{?availableAt[before],availableAt[strictly_before],availableAt[after],availableAt[strictly_after],canSell,foobar[],foobargroups[],foobargroups_override[],colors.prop,name}",
5454
"hydra:variableRepresentation": "BasicRepresentation",
5555
"hydra:mapping": [
5656
{
5757
"@type": "IriTemplateMapping",
58-
"variable": "availableAt[before]",
58+
"variable": "availableAt[after]",
5959
"property": "availableAt",
6060
"required": false
6161
},
6262
{
6363
"@type": "IriTemplateMapping",
64-
"variable": "availableAt[strictly_before]",
64+
"variable": "availableAt[before]",
6565
"property": "availableAt",
6666
"required": false
6767
},
6868
{
6969
"@type": "IriTemplateMapping",
70-
"variable": "availableAt[after]",
70+
"variable": "availableAt[strictly_after]",
7171
"property": "availableAt",
7272
"required": false
7373
},
7474
{
7575
"@type": "IriTemplateMapping",
76-
"variable": "availableAt[strictly_after]",
76+
"variable": "availableAt[strictly_before]",
7777
"property": "availableAt",
7878
"required": false
7979
},
@@ -83,6 +83,12 @@ Feature: Search filter on collections
8383
"property": "canSell",
8484
"required": false
8585
},
86+
{
87+
"@type": "IriTemplateMapping",
88+
"variable": "colors.prop",
89+
"property": "colors.prop",
90+
"required": false
91+
},
8692
{
8793
"@type": "IriTemplateMapping",
8894
"variable": "foobar[]",
@@ -97,8 +103,8 @@ Feature: Search filter on collections
97103
},
98104
{
99105
"@type": "IriTemplateMapping",
100-
"variable": "colors.prop",
101-
"property": "colors.prop",
106+
"variable": "foobargroups_override[]",
107+
"property": null,
102108
"required": false
103109
},
104110
{

src/Annotation/ApiFilter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
*/
2727
final class ApiFilter
2828
{
29+
/**
30+
* @var string
31+
*/
32+
public $id;
33+
2934
/**
3035
* @var string
3136
*/

src/Util/AnnotationFilterExtractorTrait.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private function readFilterAnnotations(\ReflectionClass $reflectionClass, Reader
8787

8888
foreach ($this->getFilterAnnotations($reader->getClassAnnotations($reflectionClass)) as $filterAnnotation) {
8989
$filterClass = $filterAnnotation->filterClass;
90-
$id = $this->generateFilterId($reflectionClass, $filterClass);
90+
$id = $this->generateFilterId($reflectionClass, $filterClass, $filterAnnotation->id);
9191

9292
if (!isset($filters[$id])) {
9393
$filters[$id] = [$filterAnnotation->arguments, $filterClass];
@@ -101,7 +101,7 @@ private function readFilterAnnotations(\ReflectionClass $reflectionClass, Reader
101101
foreach ($reflectionClass->getProperties() as $reflectionProperty) {
102102
foreach ($this->getFilterAnnotations($reader->getPropertyAnnotations($reflectionProperty)) as $filterAnnotation) {
103103
$filterClass = $filterAnnotation->filterClass;
104-
$id = $this->generateFilterId($reflectionClass, $filterClass);
104+
$id = $this->generateFilterId($reflectionClass, $filterClass, $filterAnnotation->id);
105105

106106
if (!isset($filters[$id])) {
107107
$filters[$id] = [$filterAnnotation->arguments, $filterClass];
@@ -131,11 +131,14 @@ private function readFilterAnnotations(\ReflectionClass $reflectionClass, Reader
131131
*
132132
* @param \ReflectionClass $reflectionClass the reflection class of a Resource
133133
* @param string $filterClass the filter class
134+
* @param string $filterId the filter id
134135
*
135136
* @return string
136137
*/
137-
private function generateFilterId(\ReflectionClass $reflectionClass, string $filterClass): string
138+
private function generateFilterId(\ReflectionClass $reflectionClass, string $filterClass, string $filterId = null): string
138139
{
139-
return 'annotated_'.Inflector::tableize(str_replace('\\', '', $reflectionClass->getName().(new \ReflectionClass($filterClass))->getName()));
140+
$suffix = null !== $filterId ? '_'.$filterId : $filterId;
141+
142+
return 'annotated_'.Inflector::tableize(str_replace('\\', '', $reflectionClass->getName().(new \ReflectionClass($filterClass))->getName().$suffix));
140143
}
141144
}

tests/Fixtures/TestBundle/Entity/DummyCar.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* @ApiFilter(BooleanFilter::class)
3636
* @ApiFilter(PropertyFilter::class, arguments={"parameterName": "foobar"})
3737
* @ApiFilter(GroupFilter::class, arguments={"parameterName": "foobargroups"})
38+
* @ApiFilter(GroupFilter::class, arguments={"parameterName": "foobargroups_override"}, id="override")
3839
*/
3940
class DummyCar
4041
{

tests/Util/AnnotationFilterExtractorTraitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public function testReadAnnotations()
6262
['parameterName' => 'foobargroups'],
6363
GroupFilter::class,
6464
],
65+
'annotated_api_platform_core_tests_fixtures_test_bundle_entity_dummy_car_api_platform_core_serializer_filter_group_filter_override' => [
66+
['parameterName' => 'foobargroups_override'],
67+
GroupFilter::class,
68+
],
6569
]);
6670
}
6771

0 commit comments

Comments
 (0)