API-Platform: Filtering Custom Data Provider API-Platform: Filtering Custom Data Provider symfony symfony

API-Platform: Filtering Custom Data Provider


So I finally managed to work it out. It was as simple as creating my own version of the SearchFilter, implementing ApiPlatform\Core\Api\FilterInterface.

<?phpnamespace App\Filter;use ApiPlatform\Core\Api\FilterInterface;/** * Class SearchFilter * @package App\Filter */class SearchFilter implements FilterInterface{    /**     * @var string Exact matching     */    const STRATEGY_EXACT = 'exact';    /**     * @var string The value must be contained in the field     */    const STRATEGY_PARTIAL = 'partial';    /**     * @var string Finds fields that are starting with the value     */    const STRATEGY_START = 'start';    /**     * @var string Finds fields that are ending with the value     */    const STRATEGY_END = 'end';    /**     * @var string Finds fields that are starting with the word     */    const STRATEGY_WORD_START = 'word_start';    protected $properties;    /**     * SearchFilter constructor.     * @param array|null $properties     */    public function __construct(array $properties = null)    {        $this->properties = $properties;    }    /**     * {@inheritdoc}     */    public function getDescription(string $resourceClass): array    {        $description = [];        $properties = $this->properties;        foreach ($properties as $property => $strategy) {                $filterParameterNames = [                    $property,                    $property.'[]',                ];                foreach ($filterParameterNames as $filterParameterName) {                    $description[$filterParameterName] = [                        'property' => $property,                        'type' => 'string',                        'required' => false,                        'strategy' => self::STRATEGY_EXACT,                        'is_collection' => '[]' === substr($filterParameterName, -2),                    ];                }            }        return $description;    }}