Skip to content

Commit 6626010

Browse files
Add missing function return type in filters.md (#1791)
* Update function return type in filters.md * docs: various fixes in filters.md * Update filters.md --------- Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
1 parent 05b0eb7 commit 6626010

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

core/filters.md

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Filters
22

33
API Platform provides a generic system to apply filters and sort criteria on collections.
4-
Useful filters for Doctrine ORM, MongoDB ODM and ElasticSearch are provided with the library.
4+
Useful filters for Doctrine ORM, MongoDB ODM, and ElasticSearch are provided with the library.
55

66
You can also create custom filters that fit your specific needs.
77
You can also add filtering support to your custom [state providers](state-providers.md) by implementing interfaces provided
@@ -23,7 +23,7 @@ to a Resource in two ways:
2323

2424
1. Through the resource declaration, as the `filters` attribute.
2525

26-
For example having a filter service declaration in `services.yaml`:
26+
For example, having a filter service declaration in `services.yaml`:
2727

2828
```yaml
2929
# api/config/services.yaml
@@ -254,15 +254,15 @@ The above URLs will return all offers for the product having the following IRI a
254254

255255
### Date Filter
256256

257-
The date filter allows to filter a collection by date intervals.
257+
The date filter allows filtering a collection by date intervals.
258258

259259
Syntax: `?property[<after|before|strictly_after|strictly_before>]=value`
260260

261261
The value can take any date format supported by the [`\DateTime` constructor](https://www.php.net/manual/en/datetime.construct.php).
262262

263263
The `after` and `before` filters will filter including the value whereas `strictly_after` and `strictly_before` will filter excluding the value.
264264

265-
Like others filters, the date filter must be explicitly enabled:
265+
Like other filters, the date filter must be explicitly enabled:
266266

267267
[codeSelector]
268268

@@ -533,7 +533,7 @@ You can filter offers by joining two values, for example: `/offers?price[gt]=12.
533533

534534
### Exists Filter
535535

536-
The exists filter allows you to select items based on a nullable field value.
536+
The "exists" filter allows you to select items based on a nullable field value.
537537
It will also check the emptiness of a collection association.
538538

539539
Syntax: `?exists[property]=<true|false|1|0>`
@@ -582,7 +582,7 @@ App\Entity\Offer:
582582

583583
[/codeSelector]
584584

585-
Given that the collection endpoint is `/offers`, you can filter offers on nullable field with the following query: `/offers?exists[transportFees]=true`.
585+
Given that the collection endpoint is `/offers`, you can filter offers on the nullable field with the following query: `/offers?exists[transportFees]=true`.
586586

587587
It will return all offers where `transportFees` is not `null`.
588588

@@ -600,7 +600,7 @@ api_platform:
600600

601601
### Order Filter (Sorting)
602602

603-
The order filter allows to sort a collection against the given properties.
603+
The order filter allows sorting a collection against the given properties.
604604

605605
Syntax: `?order[property]=<asc|desc>`
606606

@@ -990,8 +990,8 @@ api_platform:
990990

991991
### Match Filter
992992

993-
The match filter allows to find resources that [match](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html)
994-
the specified text on full text fields.
993+
The match filter allows us to find resources that [match](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html)
994+
the specified text on full-text fields.
995995

996996
Syntax: `?property[]=value`
997997

@@ -1021,7 +1021,7 @@ Given that the collection endpoint is `/tweets`, you can filter tweets by messag
10211021

10221022
### Term Filter
10231023

1024-
The term filter allows to find resources that contain the exact specified
1024+
The term filter allows us to find resources that contain the exact specified
10251025
[terms](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html).
10261026

10271027
Syntax: `?property[]=value`
@@ -1194,7 +1194,7 @@ final class RegexpFilter extends AbstractFilter
11941194
{
11951195
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, Operation $operation = null, array $context = []): void
11961196
{
1197-
// otherwise filter is applied to order and page as well
1197+
// Otherwise filter is applied to order and page as well
11981198
if (
11991199
!$this->isPropertyEnabled($property, $resourceClass) ||
12001200
!$this->isPropertyMapped($property, $resourceClass)
@@ -1260,7 +1260,7 @@ class Offer
12601260
You can now use this filter in the URL like `http://example.com/offers?regexp_email=^[FOO]`. This new filter will also
12611261
appear in OpenAPI and Hydra documentations.
12621262

1263-
In the previous example, the filter can be applied on any property. You can also apply this filter on a specific property:
1263+
In the previous example, the filter can be applied to any property. You can also apply this filter on a specific property:
12641264

12651265
```php
12661266
<?php
@@ -1303,10 +1303,11 @@ class Offer
13031303
```
13041304
These properties can then be accessed in the custom filter like this:
13051305
```php
1306-
//App/Filter/CustomAndFilter.php
1307-
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, Operation $operation = null, array $context = []) {
1306+
// api/src/Filter/CustomAndFilter.php
1307+
1308+
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, Operation $operation = null, array $context = []): void {
13081309
$rootAlias = $queryBuilder->getRootAliases()[0];
1309-
foreach (array_keys($this->getProperties()) as $prop) { //NOTE: we use array_keys because getProperties() returns a map of property => strategy
1310+
foreach(array_keys($this->getProperties()) as $prop) { // we use array_keys() because getProperties() returns a map of property => strategy
13101311
if (!$this->isPropertyEnabled($prop, $resourceClass) || !$this->isPropertyMapped($prop, $resourceClass)) {
13111312
return;
13121313
}
@@ -1335,7 +1336,7 @@ services:
13351336
tags: [ 'api_platform.filter' ]
13361337
```
13371338

1338-
In the previous example, the filter can be applied on any property. However, thanks to the `AbstractFilter` class,
1339+
In the previous example, the filter can be applied to any property. However, thanks to the `AbstractFilter` class,
13391340
it can also be enabled for some properties:
13401341

13411342
```yaml
@@ -1416,7 +1417,7 @@ class AndOperatorFilterExtension implements RequestBodySearchCollectionExtension
14161417
### Using Doctrine ORM Filters
14171418

14181419
Doctrine ORM features [a filter system](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/filters.html) that allows the developer to add SQL to the conditional clauses of queries, regardless of the place where the SQL is generated (e.g. from a DQL query, or by loading associated entities).
1419-
These are applied on collections and items and therefore are incredibly useful.
1420+
These are applied to collections and items and therefore are incredibly useful.
14201421

14211422
The following information, specific to Doctrine filters in Symfony, is based upon [a great article posted on Michaël Perrin's blog](http://blog.michaelperrin.fr/2014/12/05/doctrine-filters/).
14221423

@@ -1595,7 +1596,7 @@ On the first property, `name`, it's straightforward. The first attribute argumen
15951596
#[ApiFilter(SearchFilter::class, strategy: 'partial')]
15961597
```
15971598

1598-
In the second attribute, we specify `properties` on which the filter should apply. It's necessary here because we don't want to filter `colors` but the `prop` property of the `colors` association.
1599+
In the second attribute, we specify `properties` to which the filter should apply. It's necessary here because we don't want to filter `colors` but the `prop` property of the `colors` association.
15991600
Note that for each given property we specify the strategy:
16001601

16011602
```php
@@ -1633,7 +1634,7 @@ class DummyCar
16331634
16341635
```
16351636

1636-
The `BooleanFilter` is applied to every `Boolean` property of the class. Indeed, in each core filter we check the Doctrine type. It's written only by using the filter class:
1637+
The `BooleanFilter` is applied to every `Boolean` property of the class. Indeed, in each core filter, we check the Doctrine type. It's written only by using the filter class:
16371638

16381639
```php
16391640
#[ApiFilter(BooleanFilter::class)]

0 commit comments

Comments
 (0)