Skip to content

Commit fe1131f

Browse files
committed
feat: add support for uuid and ulid in SearchFilter
1 parent 6bcc1be commit fe1131f

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

src/Doctrine/Odm/Filter/SearchFilter.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
use Symfony\Component\PropertyAccess\PropertyAccess;
3232
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
3333
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
34+
use Symfony\Component\Uid\Ulid;
35+
use Symfony\Component\Uid\Uuid;
3436

3537
/**
3638
* The search filter allows to filter a collection by given properties.
@@ -221,7 +223,16 @@ protected function filterProperty(string $property, $value, Builder $aggregation
221223
return;
222224
}
223225

224-
$values = array_map($this->getIdFromValue(...), $values);
226+
227+
// $values = array_map($this->getIdFromValue(...), $values);
228+
$values = array_map(function ($value) {
229+
$id = $this->getIdFromValue($value);
230+
if ($id instanceof Uuid || $id instanceof Ulid) {
231+
return $id->toBinary();
232+
}
233+
234+
return $id;
235+
}, $values);
225236

226237
$associationResourceClass = $metadata->getAssociationTargetClass($field);
227238
$associationFieldIdentifier = $metadata->getIdentifierFieldNames()[0];

src/Doctrine/Orm/Filter/SearchFilter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
use Symfony\Component\PropertyAccess\PropertyAccess;
3232
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
3333
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
34+
use Symfony\Component\Uid\Ulid;
35+
use Symfony\Component\Uid\Uuid;
3436

3537
/**
3638
* The search filter allows to filter a collection by given properties.
@@ -196,7 +198,6 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
196198
}
197199

198200
$metadata = $this->getNestedMetadata($resourceClass, $associations);
199-
200201
if ($metadata->hasField($field)) {
201202
if ('id' === $field) {
202203
$values = array_map($this->getIdFromValue(...), $values);
@@ -255,6 +256,7 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
255256

256257
$expected = \count($values);
257258
$values = array_filter($values, static fn ($value) => null !== $value);
259+
$values = array_map(static fn ($value) => $value instanceof Uuid || $value instanceof Ulid ? $value->toBinary() : $value, $values);
258260
if ($expected > \count($values)) {
259261
/*
260262
* Shouldn't this actually fail harder?
@@ -272,7 +274,6 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
272274
$associationAlias = QueryBuilderHelper::addJoinOnce($queryBuilder, $queryNameGenerator, $alias, $associationField);
273275
$associationField = $associationFieldIdentifier;
274276
}
275-
276277
$this->addWhereByStrategy($strategy, $queryBuilder, $queryNameGenerator, $associationAlias, $associationField, $values, $caseSensitive);
277278
}
278279

tests/Fixtures/TestBundle/Document/JsonSchemaContextDummy.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class JsonSchemaContextDummy
3030
* @var int The id
3131
*
3232
* @ApiProperty(identifier=true)
33-
*
3433
* @ODM\Id(strategy="INCREMENT", type="int")
3534
*/
3635
private $id;

0 commit comments

Comments
 (0)