Thank you again for your great work !
It would be easier to add a filter in the entity with annotations instead of adding a service.
For example :
<?php
// src/AppBundle/Entity/Offer.php
namespace AppBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter; // for example
/**
* @ApiResource()
*/
class Offer
{
/**
* @ApiFilter('type="search_filter", precision="exact")
*/
private $id;
/**
* @ApiFilter('type="date_filter")
*/
private $date;
...
}
The goal here is to simplify the use of filters with Api-Platform, and I think it's important because the great success of Api-Platform is it's simplicity.
I think this is a good idea. We should wait for @api-platform/core-team approval first, but would you ike to work on a PR for this feature?
I like the idea, may I suggest an alternative syntax?
<?php
// src/AppBundle/Entity/Offer.php
namespace AppBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
/**
* @ApiResource
*/
class Offer
{
/**
* @ApiFilter(SearchFilter::class, strategy="exact")
*/
private $id;
/**
* @ApiFilter(DateFilter::class)
*/
private $date;
...
}
I would also allow to add the @ApiFilter on the class to enable the filter for all properties.
I would love to see this feature! But we should also think about filters applied to multiple fields (i.e. search_filter)
We'll need a bridge with the Symfony DI component:
abstract one with all properties of this class configuredattributes key (we should also keep the current system to allow to apply a filter only on a specific operation). * @ApiFilter(SearchFilter::class, strategy="exact")
This is really neat, it'll help using custom filters too :).
I would also allow to add the @ApiFilter on the class to enable the filter for all properties.
This might work well on simple filters, or if you want the same behavior for every properties. I doubt that this would be useful in real case scenarios.
For example with the search_filter:
/**
* @ApiFilter(SearchFilter::class, strategy="exact")
*/
class Dummy {
/**
* @ApiFilter(SearchFilter::class, strategy="partial")
*/
private $foo;
}
This would mean that properties filters override the resource ones?
What about multiple filters on one property? Would there be multiple annotations? (or @ApiFilters()).
Last but not least, on the class if say we put a date_filter:
/**
* @ApiFilter(DateFilter::class, strategy=DateFilter::EXCLUDE_NULL)
*/
class Dummy {
}
This should only apply to Dates properties on the given class.
I'd love such feature! Happy to prototype it :)
What about multiple filters on one property? Would there be multiple annotations?
Yes, exactly like for @Assert annotations.
cool feature , I can propose something also ^^
Is someone working on this yet?
Could be on 2.1.0 maybe @soyuka.
I don't think someone has been working on it, if someone want begin it, that could be neat ! @sroze @jordscream
I'll have time this week end for this. Though it may be better to wait for #1099.
Most helpful comment
I like the idea, may I suggest an alternative syntax?
I would also allow to add the
@ApiFilteron the class to enable the filter for all properties.