Api Platform conference
Register now
References RangeFilter
API Platform Conference
September 19-20, 2024 | Lille & online

The international conference on the API Platform Framework

API Platform Conference 2024: meet the best PHP, JavaScript and API experts

Learn more about the event, register for the conference, and get ready for two days of inspiration, ideas, and knowledge-sharing with our incredible lineup of renowned specialists and advocates.

Register now
Reference
C
Class

ApiPlatform\Doctrine\Orm\Filter\RangeFilter

The range filter allows you to filter by a value lower than, greater than, lower than or equal, greater than or equal and between two values.

Syntax: ?property[<lt|gt|lte|gte|between>]=value.

<?php
// api/src/Entity/Book.php
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Doctrine\Orm\Filter\RangeFilter;

#[ApiResource]
#[ApiFilter(RangeFilter::class, properties: ['price'])]
class Book
{
    // ...
}
# config/services.yaml
services:
    book.range_filter:
        parent: 'api_platform.doctrine.orm.range_filter'
        arguments: [ { price: ~ } ]
        tags:  [ 'api_platform.filter' ]
        # The following are mandatory only if a _defaults section is defined with inverted values.
        # You may want to isolate filters in a dedicated file to avoid adding the following lines (by adding them in the defaults section)
        autowire: false
        autoconfigure: false
        public: false

# api/config/api_platform/resources.yaml
resources:
    App\Entity\Book:
        - operations:
              ApiPlatform\Metadata\GetCollection:
                  filters: ['book.range_filter']
<?xml version="1.0" encoding="UTF-8" ?>
<!-- api/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container
        xmlns="http://symfony.com/schema/dic/services"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd">
    <services>
        <service id="book.range_filter" parent="api_platform.doctrine.orm.range_filter">
            <argument type="collection">
                <argument key="price"/>
            </argument>
            <tag name="api_platform.filter"/>
        </service>
    </services>
</container>
<!-- api/config/api_platform/resources.xml -->
<resources
        xmlns="https://api-platform.com/schema/metadata/resources-3.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
        https://api-platform.com/schema/metadata/resources-3.0.xsd">
    <resource class="App\Entity\Book">
        <operations>
            <operation class="ApiPlatform\Metadata\GetCollection">
                <filters>
                    <filter>book.range_filter</filter>
                </filters>
            </operation>
        </operations>
    </resource>
</resources>

Given that the collection endpoint is /books, you can filter books with the following query: /books?price[between]=12.99..15.99.

class ApiPlatform\Doctrine\Orm\Filter\RangeFilter extends implements `<a href="/docs/v3.1/references/Doctrine/Orm/Filter/FilterInterface">ApiPlatform\Doctrine\Orm\Filter\FilterInterface</a>`, `<a href="/docs/v3.1/references/Metadata/FilterInterface">ApiPlatform\Metadata\FilterInterface</a>`, `<a href="/docs/v3.1/references/Api/FilterInterface">ApiPlatform\Api\FilterInterface</a>`, `<a href="/docs/v3.1/references/Doctrine/Common/Filter/RangeFilterInterface">ApiPlatform\Doctrine\Common\Filter\RangeFilterInterface
{
    protected filterProperty(string $property, $values, Doctrine\ORM\QueryBuilder $queryBuilder, $queryNameGenerator, string $resourceClass, null|`<a href="/docs/v3.1/references/Metadata/Operation">ApiPlatform\Metadata\Operation $operation, array $context): null
    protected addWhere(Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, string $operator, string $value): null
    public __construct(Doctrine\Persistence\ManagerRegistry $managerRegistry, null|Psr\Log\LoggerInterface $logger, null|array $properties, null|Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter)
    public apply(Doctrine\ORM\QueryBuilder $queryBuilder, $queryNameGenerator, string $resourceClass, null|`<a href="/docs/v3.1/references/Metadata/Operation">ApiPlatform\Metadata\Operation $operation, array $context): null
    protected getManagerRegistry(): Doctrine\Persistence\ManagerRegistry
    protected getProperties(): array
    protected getLogger(): Psr\Log\LoggerInterface
    protected isPropertyEnabled(string $property, string $resourceClass): bool
    protected denormalizePropertyName(string|int $property): string
    protected normalizePropertyName(string $property): string
    protected splitPropertyParts(string $property, string $resourceClass): array
    protected addJoinsForNestedProperty(string $property, string $rootAlias, Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $joinType): array
    protected isPropertyMapped(string $property, string $resourceClass, bool $allowAssociation): bool
    protected isPropertyNested(string $property, string $resourceClass): bool
    protected isPropertyEmbedded(string $property, string $resourceClass): bool
    protected getDoctrineFieldType(string $property, string $resourceClass): string
    protected getNestedMetadata(string $resourceClass, array<int, string> $associations): Doctrine\Persistence\Mapping\ClassMetadata
    protected getClassMetadata(string $resourceClass): Doctrine\Persistence\Mapping\ClassMetadata
    public getDescription(string $resourceClass): array
    protected getFilterDescription(string $fieldName, string $operator): array
}

Properties

logger

Psr\Log\LoggerInterface $logger

managerRegistry

Doctrine\Persistence\ManagerRegistry $managerRegistry

properties

array $properties

nameConverter

Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter

Methods

filterProperty

Passes a property through the filter.

protected filterProperty(string $property, $values, Doctrine\ORM\QueryBuilder $queryBuilder, $queryNameGenerator, string $resourceClass, null|`<a href="/docs/v3.1/references/Metadata/Operation">ApiPlatform\Metadata\Operation $operation, array $context): null

Parameters

propertystring
values
queryBuilderDoctrine\ORM\QueryBuilder
queryNameGenerator`ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface`
resourceClassstring
operation`ApiPlatform\Metadata\Operation`
contextarray

Returns

null

addWhere

Adds the where clause according to the operator.

protected addWhere(Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, string $operator, string $value): null

Parameters

queryBuilderDoctrine\ORM\QueryBuilder
queryNameGenerator`ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface`
aliasstring
fieldstring
operatorstring
valuestring

Returns

null

splitPropertyParts

Splits the given property into parts.Returns an array with the following keys:

  • associations: array of associations according to nesting order
  • field: string holding the actual field (leaf node)
protected splitPropertyParts(string $property, string $resourceClass): array

Parameters

propertystring
resourceClassstring

Returns

array

isPropertyMapped

Determines whether the given property is mapped.

protected isPropertyMapped(string $property, string $resourceClass, bool $allowAssociation): bool

Parameters

propertystring
resourceClassstring
allowAssociationbool

Returns

bool

isPropertyNested

Determines whether the given property is nested.

protected isPropertyNested(string $property, string $resourceClass): bool

Parameters

propertystring
resourceClassstring

Returns

bool

isPropertyEmbedded

Determines whether the given property is embedded.

protected isPropertyEmbedded(string $property, string $resourceClass): bool

Parameters

propertystring
resourceClassstring

Returns

bool

getDoctrineFieldType

Gets the Doctrine Type of a given property/resourceClass.

protected getDoctrineFieldType(string $property, string $resourceClass): string

Parameters

propertystring
resourceClassstring

Returns

string

getNestedMetadata

Gets nested class metadata for the given resource.

protected getNestedMetadata(string $resourceClass, array<int, string> $associations): Doctrine\Persistence\Mapping\ClassMetadata

Parameters

resourceClassstring
associationsarray<int, string>

Returns

Doctrine\Persistence\Mapping\ClassMetadata

getClassMetadata

Gets class metadata for the given resource.

protected getClassMetadata(string $resourceClass): Doctrine\Persistence\Mapping\ClassMetadata

Parameters

resourceClassstring

Returns

Doctrine\Persistence\Mapping\ClassMetadata

getDescription

Gets the description of this filter for the given resource.Returns an array with the filter parameter names as keys and array with the following data as values:

  • property: the property where the filter is applied
  • type: the type of the filter
  • required: if this filter is required
  • strategy (optional): the used strategy
  • is_collection (optional): if this filter is for collection
  • swagger (optional): additional parameters for the path operation, e.g. ‘swagger’ => [ ‘description’ => ‘My Description’, ’name’ => ‘My Name’, ’type’ => ‘integer’, ]
  • openapi (optional): additional parameters for the path operation in the version 3 spec, e.g. ‘openapi’ => [ ‘description’ => ‘My Description’, ’name’ => ‘My Name’, ‘schema’ => [ ’type’ => ‘integer’, ] ]
  • schema (optional): schema definition, e.g. ‘schema’ => [ ’type’ => ‘string’, ’enum’ => [‘value_1’, ‘value_2’], ] The description can contain additional data specific to a filter.Gets the description of this filter for the given resource.Returns an array with the filter parameter names as keys and array with the following data as values:
  • property: the property where the filter is applied
  • type: the type of the filter
  • required: if this filter is required
  • strategy (optional): the used strategy
  • is_collection (optional): if this filter is for collection
  • swagger (optional): additional parameters for the path operation, e.g. ‘swagger’ => [ ‘description’ => ‘My Description’, ’name’ => ‘My Name’, ’type’ => ‘integer’, ]
  • openapi (optional): additional parameters for the path operation in the version 3 spec, e.g. ‘openapi’ => [ ‘description’ => ‘My Description’, ’name’ => ‘My Name’, ‘schema’ => [ ’type’ => ‘integer’, ] ]
  • schema (optional): schema definition, e.g. ‘schema’ => [ ’type’ => ‘string’, ’enum’ => [‘value_1’, ‘value_2’], ] The description can contain additional data specific to a filter.Gets the description of this filter for the given resource.Returns an array with the filter parameter names as keys and array with the following data as values:
  • property: the property where the filter is applied
  • type: the type of the filter
  • required: if this filter is required
  • strategy (optional): the used strategy
  • is_collection (optional): if this filter is for collection
  • swagger (optional): additional parameters for the path operation, e.g. ‘swagger’ => [ ‘description’ => ‘My Description’, ’name’ => ‘My Name’, ’type’ => ‘integer’, ]
  • openapi (optional): additional parameters for the path operation in the version 3 spec, e.g. ‘openapi’ => [ ‘description’ => ‘My Description’, ’name’ => ‘My Name’, ‘schema’ => [ ’type’ => ‘integer’, ] ]
  • schema (optional): schema definition, e.g. ‘schema’ => [ ’type’ => ‘string’, ’enum’ => [‘value_1’, ‘value_2’], ] The description can contain additional data specific to a filter.Gets the description of this filter for the given resource.Returns an array with the filter parameter names as keys and array with the following data as values:
  • property: the property where the filter is applied
  • type: the type of the filter
  • required: if this filter is required
  • strategy (optional): the used strategy
  • is_collection (optional): if this filter is for collection
  • swagger (optional): additional parameters for the path operation, e.g. ‘swagger’ => [ ‘description’ => ‘My Description’, ’name’ => ‘My Name’, ’type’ => ‘integer’, ]
  • openapi (optional): additional parameters for the path operation in the version 3 spec, e.g. ‘openapi’ => [ ‘description’ => ‘My Description’, ’name’ => ‘My Name’, ‘schema’ => [ ’type’ => ‘integer’, ] ]
  • schema (optional): schema definition, e.g. ‘schema’ => [ ’type’ => ‘string’, ’enum’ => [‘value_1’, ‘value_2’], ] The description can contain additional data specific to a filter.
public getDescription(string $resourceClass): array

Parameters

resourceClassstring

Returns

array

getFilterDescription

Gets filter description.

protected getFilterDescription(string $fieldName, string $operator): array

Parameters

fieldNamestring
operatorstring

Returns

array

Made with love by

Les-Tilleuls.coop can help you design and develop your APIs and web projects, and train your teams in API Platform, Symfony, Next.js, Kubernetes and a wide range of other technologies.

Learn more

Copyright © 2023 Kévin Dunglas

Sponsored by Les-Tilleuls.coop