Skip to content

Commit d224b3c

Browse files
committed
udp
1 parent d8e7088 commit d224b3c

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

docs/guides/create-a-custom-doctrine-filter.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
//
1414
// A Doctrine ORM filter has access to the `QueryBuilder` and the `QueryParameter` context.
1515
//
16-
// In this example, we create a `RegexpFilter` that applies a MySQL `REGEXP` condition. We map this filter to specific API parameters using the `#[QueryParameter]` attribute on our resource.
16+
// In this example, we create a `MinLengthFilter` that filters resources where the length of a property is greater than or equal to a specific value. We map this filter to specific API parameters using the `#[QueryParameter]` attribute on our resource.
1717

1818
namespace App\Filter {
1919
use ApiPlatform\Doctrine\Orm\Filter\FilterInterface;
2020
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
2121
use ApiPlatform\Metadata\Operation;
2222
use Doctrine\ORM\QueryBuilder;
2323

24-
final class RegexpFilter implements FilterInterface
24+
final class MinLengthFilter implements FilterInterface
2525
{
2626
//The `apply` method is where the filtering logic happens.
2727
//We retrieve the parameter definition and its value from the context.
@@ -47,7 +47,7 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q
4747
$alias = $queryBuilder->getRootAliases()[0];
4848

4949
$queryBuilder
50-
->andWhere(sprintf('REGEXP(%s.%s, :%s) = 1', $alias, $property, $parameterName))
50+
->andWhere(sprintf('LENGTH(%s.%s) >= :%s', $alias, $property, $parameterName))
5151
->setParameter($parameterName, $value);
5252
}
5353

@@ -64,17 +64,17 @@ public function getDescription(string $resourceClass): array
6464
use ApiPlatform\Metadata\ApiResource;
6565
use ApiPlatform\Metadata\GetCollection;
6666
use ApiPlatform\Metadata\QueryParameter;
67-
use App\Filter\RegexpFilter;
67+
use App\Filter\MinLengthFilter;
6868
use Doctrine\ORM\Mapping as ORM;
6969

7070
#[ORM\Entity]
7171
#[ApiResource(
7272
operations: [
7373
new GetCollection(
7474
parameters: [
75-
// We define a parameter 'regexp' that filters on the `title` and the `author` property using our custom logic.
76-
'regexp[:property]' => new QueryParameter(
77-
filter: RegexpFilter::class,
75+
// We define a parameter 'min_length' that filters on the `title` and the `author` property using our custom logic.
76+
'min_length[:property]' => new QueryParameter(
77+
filter: MinLengthFilter::class,
7878
properties: ['title', 'author'],
7979
),
8080
]
@@ -101,7 +101,7 @@ class Book
101101

102102
function request(): Request
103103
{
104-
return Request::create('/books.jsonld?regexp[title]=^[Found]', 'GET');
104+
return Request::create('/books.jsonld?min_length[title]=10', 'GET');
105105
}
106106
}
107107

@@ -129,25 +129,25 @@ final class BookTest extends ApiTestCase
129129

130130
public function testAsAnonymousICanAccessTheDocumentation(): void
131131
{
132-
static::createClient()->request('GET', '/books.jsonld?regexp[title]=^[Found]');
132+
static::createClient()->request('GET', '/books.jsonld?min_length[title]=10');
133133

134134
$this->assertResponseIsSuccessful();
135135
$this->assertMatchesResourceCollectionJsonSchema(Book::class, '_api_/books{._format}_get_collection');
136136
$this->assertJsonContains([
137137
'search' => [
138138
'@type' => 'IriTemplate',
139-
'template' => '/books.jsonld{?regexp[title],regexp[author]}',
139+
'template' => '/books.jsonld{?min_length[title],min_length[author]}',
140140
'variableRepresentation' => 'BasicRepresentation',
141141
'mapping' => [
142142
[
143143
'@type' => 'IriTemplateMapping',
144-
'variable' => 'regexp[title]',
144+
'variable' => 'min_length[title]',
145145
'property' => 'title',
146146
'required' => false,
147147
],
148148
[
149149
'@type' => 'IriTemplateMapping',
150-
'variable' => 'regexp[author]',
150+
'variable' => 'min_length[author]',
151151
'property' => 'author',
152152
'required' => false,
153153
],

0 commit comments

Comments
 (0)