Skip to content

Commit 8c9542a

Browse files
vinceAmstoutzdunglas
authored andcommitted
Removal of the duplicate parameters section
1 parent f137399 commit 8c9542a

File tree

1 file changed

+10
-250
lines changed

1 file changed

+10
-250
lines changed

core/filters.md

Lines changed: 10 additions & 250 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ It is also automatically documented as a `search` property for JSON-LD responses
1616

1717
## Parameters
1818

19-
You can declare parameters on a Resource or an Operation through the `parameters` property.
19+
You can declare parameters on a Resource or an Operation through the `parameters` property.
2020

2121
```php
2222
namespace App\ApiResource;
@@ -39,7 +39,7 @@ A Parameter can be linked to a filter, there are two types of filters:
3939

4040
### Alter the Operation via a parameter
4141

42-
A parameter can alter the current Operation context, to do so use a `ApiPlatform\State\ParameterProviderInterface`:
42+
A parameter can alter the current Operation context, to do so use a `ApiPlatform\State\ParameterProviderInterface`:
4343

4444
```php
4545
class GroupsParameterProvider implements ParameterProviderInterface {
@@ -64,7 +64,7 @@ class Book {
6464
}
6565
```
6666

67-
If you don't have autoconfiguration enabled, declare the parameter as a tagged service:
67+
If you don't have autoconfiguration enabled, declare the parameter as a tagged service:
6868

6969
```yaml
7070
services:
@@ -76,7 +76,7 @@ services:
7676
7777
### Call a filter
7878
79-
A Parameter can also call a filter and works on filters that impact the data persistence layer (Doctrine ORM, ODM and Eloquent filters are supported). Let's assume, that we have an Order filter declared:
79+
A Parameter can also call a filter and works on filters that impact the data persistence layer (Doctrine ORM, ODM and Eloquent filters are supported). Let's assume, that we have an Order filter declared:
8080
8181
```yaml
8282
# config/services.yaml
@@ -89,7 +89,7 @@ services:
8989
tags: [ 'api_platform.filter' ]
9090
```
9191
92-
We can use this filter specifying we want a query parameter with the `:property` placeholder:
92+
We can use this filter specifying we want a query parameter with the `:property` placeholder:
9393

9494
```php
9595
namespace App\ApiResource;
@@ -127,7 +127,7 @@ When you declare a parameter on top of a class, you need to specify it's key.
127127

128128
### The :property placeholder
129129

130-
When used on a Parameter, the `:property` placeholder allows to map automatically a parameter to the readable properties of your resource.
130+
When used on a Parameter, the `:property` placeholder allows to map automatically a parameter to the readable properties of your resource.
131131

132132
```php
133133
namespace App\ApiResource;
@@ -143,7 +143,7 @@ class Book {
143143
}
144144
```
145145

146-
This will declare a query parameter for each property (id, title and author) calling the SearchFilter.
146+
This will declare a query parameter for each property (ID, title and author) calling the SearchFilter.
147147

148148
This is especially useful for sort filters where you'd like to use `?sort[name]=asc`:
149149

@@ -161,7 +161,7 @@ class Book {
161161
}
162162
```
163163

164-
### Documentation
164+
### Documentation
165165

166166
A parameter is quite close to its documentation and you can specify the JSON Schema and/or the OpenAPI documentation:
167167

@@ -226,7 +226,7 @@ use ApiPlatform\Metadata\QueryParameter;
226226
],
227227
)]
228228
class ValidateParameter {}
229-
```
229+
```
230230

231231
You can also use your own constraint by setting the `constraints` option on a Parameter. In that case we won't setup the automatic validation for you and it'll replace our defaults.
232232

@@ -299,7 +299,7 @@ final class SearchTextAndDateFilter implements FilterInterface
299299
}
300300
```
301301

302-
This can be used with parameters using attributes:
302+
This can be used with parameters using attributes:
303303

304304
```php
305305
namespace App\Entity;
@@ -2006,243 +2006,3 @@ The next filters are not related to how the data is fetched but rather to how th
20062006
#[ApiFilter(GroupFilter::class, arguments: ['parameterName' => 'foobargroups'])]
20072007
```
20082008

2009-
## Parameters
2010-
2011-
An option exists to declare parameters on a Resource or an Operation through the `parameters` property.
2012-
2013-
```php
2014-
namespace App\ApiResource;
2015-
2016-
use ApiPlatform\Metadata\GetCollection;
2017-
use ApiPlatform\Metadata\QueryParameter;
2018-
2019-
// This parameter "page" works only on /books
2020-
#[GetCollection(uriTemplate: '/books', parameters: ['page' => new QueryParameter])]
2021-
// This parameter is available on every operations, key is mandatory
2022-
#[QueryParameter(key: 'q', property: 'freetextQuery')]
2023-
class Book {}
2024-
```
2025-
2026-
Note that `property` is used to document the Hydra view. You can also specify an [OpenAPI Parameter](https://api-platform.com/docs/references/OpenApi/Model/Parameter/) if needed.
2027-
2028-
### Alter the Operation via a parameter
2029-
2030-
A parameter can alter the current Operation context, to do so use a `ApiPlatform\State\ParameterProviderInterface`:
2031-
2032-
```php
2033-
class GroupsParameterProvider implements ParameterProviderInterface {
2034-
public function provider(Parameter $parameter, array $uriVariables = [], array $context = []): HttpOperation
2035-
{
2036-
$request = $context['request'];
2037-
return $context['operation']->withNormalizationContext(['groups' => $request->query->all('groups')]);
2038-
}
2039-
}
2040-
```
2041-
2042-
Then plug this provider on your parameter:
2043-
2044-
```php
2045-
namespace App\ApiResource;
2046-
2047-
use ApiPlatform\Metadata\HeaderParameter;
2048-
2049-
#[Get(parameters: ['groups' => new HeaderParameter(provider: GroupsParameterProvider::class)])]
2050-
class Book {
2051-
public string $id;
2052-
}
2053-
```
2054-
2055-
If you don't have autoconfiguration enabled, declare the parameter as a tagged service:
2056-
2057-
```yaml
2058-
services:
2059-
ApiPlatform\Tests\Fixtures\TestBundle\Parameter\CustomGroupParameterProvider:
2060-
tags:
2061-
- name: 'api_platform.parameter_provider'
2062-
key: 'ApiPlatform\Tests\Fixtures\TestBundle\Parameter\CustomGroupParameterProvider'
2063-
```
2064-
2065-
### Call a filter
2066-
2067-
A Parameter can also call a filter and works on filters that impact the data persistence layer (Doctrine ORM and ODM filters are supported). Let's assume, that we have an Order filter declared:
2068-
2069-
```yaml
2070-
# config/services.yaml
2071-
services:
2072-
offer.order_filter:
2073-
parent: 'api_platform.doctrine.orm.order_filter'
2074-
arguments:
2075-
$properties: { id: ~, name: ~ }
2076-
$orderParameterName: order
2077-
tags: [ 'api_platform.filter' ]
2078-
```
2079-
2080-
We can use this filter specifying we want a query parameter with the `:property` placeholder:
2081-
2082-
```php
2083-
namespace App\ApiResource;
2084-
2085-
#[GetCollection(
2086-
uriTemplate: 'orders',
2087-
parameters: [
2088-
'order[:property]' => new QueryParameter(filter: 'offer.order_filter'),
2089-
]
2090-
)
2091-
class Offer {
2092-
public string $id;
2093-
public string $name;
2094-
}
2095-
```
2096-
2097-
### Decorate a Doctrine filter
2098-
2099-
A filter that implements the `ApiPlatform\Doctrine\Common\Filter\PropertyAwareFilterInterface` interface can be decorated:
2100-
2101-
```php
2102-
namespace App\Doctrine\Filter;
2103-
2104-
use ApiPlatform\Doctrine\Common\Filter\PropertyAwareFilterInterface;
2105-
use ApiPlatform\Doctrine\Orm\Filter\FilterInterface;
2106-
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
2107-
use ApiPlatform\Metadata\Operation;
2108-
use Doctrine\ORM\QueryBuilder;
2109-
use Symfony\Component\DependencyInjection\Attribute\Autowire;
2110-
2111-
final class SearchTextAndDateFilter implements FilterInterface
2112-
{
2113-
public function __construct(#[Autowire('@api_platform.doctrine.orm.search_filter.instance')] readonly FilterInterface $searchFilter, #[Autowire('@api_platform.doctrine.orm.date_filter.instance')] readonly FilterInterface $dateFilter, protected ?array $properties = null, private array $dateFilterProperties = [], private array $searchFilterProperties = [])
2114-
{
2115-
}
2116-
2117-
// This function is only used to hook in documentation generators (supported by Swagger and Hydra)
2118-
public function getDescription(string $resourceClass): array
2119-
{
2120-
if ($this->searchFilter instanceof PropertyAwareFilterInterface) {
2121-
$this->searchFilter->setProperties($this->searchFilterProperties);
2122-
}
2123-
if ($this->dateFilter instanceof PropertyAwareFilterInterface) {
2124-
$this->dateFilter->setProperties($this->dateFilterProperties);
2125-
}
2126-
2127-
return array_merge($this->searchFilter->getDescription($resourceClass), $this->dateFilter->getDescription($resourceClass));
2128-
}
2129-
2130-
public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
2131-
{
2132-
if ($this->searchFilter instanceof PropertyAwareFilterInterface) {
2133-
$this->searchFilter->setProperties($this->searchFilterProperties);
2134-
}
2135-
if ($this->dateFilter instanceof PropertyAwareFilterInterface) {
2136-
$this->dateFilter->setProperties($this->dateFilterProperties);
2137-
}
2138-
2139-
$this->searchFilter->apply($queryBuilder, $queryNameGenerator, $resourceClass, $operation, ['filters' => $context['filters']['searchOnTextAndDate']] + $context);
2140-
$this->dateFilter->apply($queryBuilder, $queryNameGenerator, $resourceClass, $operation, ['filters' => $context['filters']['searchOnTextAndDate']] + $context);
2141-
}
2142-
}
2143-
```
2144-
2145-
This can be used with parameters using attributes:
2146-
2147-
```php
2148-
namespace App\Entity;
2149-
2150-
#[GetCollection(
2151-
uriTemplate: 'search_filter_parameter{._format}',
2152-
parameters: [
2153-
'searchOnTextAndDate[:property]' => new QueryParameter(filter: 'app_filter_date_and_search'),
2154-
]
2155-
)]
2156-
// Note that we link the parameter filter and this filter using the "alias" option:
2157-
#[ApiFilter(SearchTextAndDateFilter::class, alias: 'app_filter_date_and_search', properties: ['foo', 'createdAt'], arguments: ['dateFilterProperties' => ['createdAt' => 'exclude_null'], 'searchFilterProperties' => ['foo' => 'exact']])]
2158-
#[ORM\Entity]
2159-
class SearchFilterParameter
2160-
{
2161-
/**
2162-
* @var int The id
2163-
*/
2164-
#[ORM\Column(type: 'integer')]
2165-
#[ORM\Id]
2166-
#[ORM\GeneratedValue(strategy: 'AUTO')]
2167-
private ?int $id = null;
2168-
#[ORM\Column(type: 'string')]
2169-
private string $foo = '';
2170-
2171-
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
2172-
private ?\DateTimeImmutable $createdAt = null;
2173-
2174-
public function getId(): ?int
2175-
{
2176-
return $this->id;
2177-
}
2178-
2179-
public function getFoo(): string
2180-
{
2181-
return $this->foo;
2182-
}
2183-
2184-
public function setFoo(string $foo): void
2185-
{
2186-
$this->foo = $foo;
2187-
}
2188-
2189-
public function getCreatedAt(): ?\DateTimeImmutable
2190-
{
2191-
return $this->createdAt;
2192-
}
2193-
2194-
public function setCreatedAt(\DateTimeImmutable $createdAt): void
2195-
{
2196-
$this->createdAt = $createdAt;
2197-
}
2198-
}
2199-
```
2200-
2201-
### Parameter validation
2202-
2203-
Parameter validation is automatic based on the configuration for example:
2204-
2205-
```php
2206-
<?php
2207-
use ApiPlatform\Metadata\GetCollection;
2208-
use ApiPlatform\Metadata\QueryParameter;
2209-
2210-
#[GetCollection(
2211-
uriTemplate: 'validate_parameters{._format}',
2212-
parameters: [
2213-
'enum' => new QueryParameter(schema: ['enum' => ['a', 'b'], 'uniqueItems' => true]),
2214-
'num' => new QueryParameter(schema: ['minimum' => 1, 'maximum' => 3]),
2215-
'exclusiveNum' => new QueryParameter(schema: ['exclusiveMinimum' => 1, 'exclusiveMaximum' => 3]),
2216-
'blank' => new QueryParameter(openApi: new OpenApiParameter(name: 'blank', in: 'query', allowEmptyValue: false)),
2217-
'length' => new QueryParameter(schema: ['maxLength' => 1, 'minLength' => 3]),
2218-
'array' => new QueryParameter(schema: ['minItems' => 2, 'maxItems' => 3]),
2219-
'multipleOf' => new QueryParameter(schema: ['multipleOf' => 2]),
2220-
'pattern' => new QueryParameter(schema: ['pattern' => '/\d/']),
2221-
'required' => new QueryParameter(required: true),
2222-
],
2223-
)]
2224-
class ValidateParameter {}
2225-
```
2226-
2227-
You can also use your own constraint by setting the `constraints` option on a Parameter. In that case we won't setup the automatic validation for you and it'll replace our defaults.
2228-
2229-
2230-
### Parameter security
2231-
2232-
Parameters may have security checks:
2233-
2234-
``` php
2235-
<?php
2236-
use ApiPlatform\Metadata\GetCollection;
2237-
use ApiPlatform\Metadata\HeaderParameter;
2238-
use ApiPlatform\Metadata\QueryParameter;
2239-
2240-
#[GetCollection(
2241-
uriTemplate: 'security_parameters{._format}',
2242-
parameters: [
2243-
'sensitive' => new QueryParameter(security: 'is_granted("ROLE_ADMIN")'),
2244-
'auth' => new HeaderParameter(security: '"secretKey" == auth[0]'),
2245-
],
2246-
)]
2247-
class SecurityParameter {}
2248-
```

0 commit comments

Comments
 (0)