Skip to content

Commit 34a9d06

Browse files
committed
Use an enum for query type
1 parent 14fbb3f commit 34a9d06

File tree

5 files changed

+32
-23
lines changed

5 files changed

+32
-23
lines changed

docs/en/reference/attributes-reference.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,14 @@ multiple document classes, and even other embedded documents!
339339
#[Encrypt]
340340
----------
341341

342-
The `#[Encrypt]` attribute is used to define an encrypted field mapping for a document property. It allows you to configure fields for encryption and queryable encryption in MongoDB.
342+
The ``#[Encrypt]`` attribute is used to define an encrypted field mapping for a document property. It allows you to configure fields for encryption and queryable encryption in MongoDB.
343343

344344
Optional arguments:
345345

346346
- ``queryType`` - Specifies the query type for the field. Possible values:
347347
- ``null`` (default) - Field is not queryable.
348-
- ``QUERY_TYPE_EQUALITY`` - Enables equality queries.
349-
- ``QUERY_TYPE_RANGE`` - Enables range queries.
348+
- ``EncryptQuery::Equality`` - Enables equality queries.
349+
- ``EncryptQuery::Range`` - Enables range queries.
350350
- ``min``, ``max`` - Specify minimum and maximum (inclusive) queryable values fora field when possible, as smaller bounds improve query efficiency. If querying values outside of these bounds, MongoDB returns an error.
351351
- ``sparsity``, ``prevision``, ``trimFactor``, ``contention`` - For advanced users only. The default values for these options are suitable for the majority of use cases, and should only be modified if your use case requires it.
352352

@@ -357,18 +357,20 @@ Example:
357357
<?php
358358
359359
use Doctrine\ODM\MongoDB\Mapping\Annotations\Encrypt;
360+
use Doctrine\ODM\MongoDB\Mapping\Annotations\EncryptQuery;
360361
361362
#[Document]
362363
class Client
363364
{
364365
// ...
365366
366367
#[Field]
367-
#[Encrypt(queryType: Encrypt::QUERY_TYPE_EQUALITY)]
368+
#[Encrypt(queryType: EncryptQuery::Equality)]
368369
public string $name;
369370
}
370371
371-
For more details, refer to the MongoDB documentation on [Queryable Encryption](https://www.mongodb.com/docs/manual/core/queryable-encryption/fundamentals/encrypt-and-query/).
372+
For more details, refer to the MongoDB documentation on
373+
`Queryable Encryption <https://www.mongodb.com/docs/manual/core/queryable-encryption/fundamentals/encrypt-and-query/>`_.
372374

373375
#[Field]
374376
--------

lib/Doctrine/ODM/MongoDB/Mapping/Annotations/Encrypt.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66

77
use Attribute;
88
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
9-
use InvalidArgumentException;
109
use MongoDB\BSON\Type;
11-
use MongoDB\Driver\ClientEncryption;
12-
13-
use function in_array;
1410

1511
/**
1612
* Defines an encrypted field mapping.
@@ -23,27 +19,21 @@
2319
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)]
2420
final class Encrypt implements Annotation
2521
{
26-
public const QUERY_TYPE_EQUALITY = ClientEncryption::QUERY_TYPE_EQUALITY;
27-
public const QUERY_TYPE_RANGE = ClientEncryption::QUERY_TYPE_RANGE;
28-
2922
/**
30-
* @param self::QUERY_TYPE_*|null $queryType Set the query type for the field, null if not queryable.
31-
* @param int<1, 4>|null $sparsity
32-
* @param positive-int|null $prevision
33-
* @param positive-int|null $trimFactor
34-
* @param positive-int|null $contention
23+
* @param EncryptQuery|null $queryType Set the query type for the field, null if not queryable.
24+
* @param int<1, 4>|null $sparsity
25+
* @param positive-int|null $prevision
26+
* @param positive-int|null $trimFactor
27+
* @param positive-int|null $contention
3528
*/
3629
public function __construct(
37-
public ?string $queryType = null,
30+
public ?EncryptQuery $queryType = null,
3831
public string|int|Type|null $min = null,
3932
public string|int|Type|null $max = null,
4033
public ?int $sparsity = null,
4134
public ?int $prevision = null,
4235
public ?int $trimFactor = null,
4336
public ?int $contention = null,
4437
) {
45-
if ($this->queryType && ! in_array($this->queryType, [self::QUERY_TYPE_EQUALITY, self::QUERY_TYPE_RANGE], true)) {
46-
throw new InvalidArgumentException('Invalid query type');
47-
}
4838
}
4939
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\ODM\MongoDB\Mapping\Annotations;
6+
7+
use MongoDB\Driver\ClientEncryption;
8+
9+
enum EncryptQuery: string
10+
{
11+
case Equality = ClientEncryption::QUERY_TYPE_EQUALITY;
12+
case Range = ClientEncryption::QUERY_TYPE_RANGE;
13+
}

lib/Doctrine/ODM/MongoDB/Utility/EncryptionFieldMap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
namespace Doctrine\ODM\MongoDB\Utility;
66

7+
use Doctrine\ODM\MongoDB\Mapping\Annotations\EncryptQuery;
78
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
89
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactoryInterface;
910
use Doctrine\ODM\MongoDB\Mapping\MappingException;
1011
use Generator;
1112

1213
use function array_filter;
14+
use function assert;
1315
use function iterator_to_array;
1416

1517
final class EncryptionFieldMap
@@ -72,6 +74,8 @@ private function createEncryptionFieldMap(ClassMetadata $classMetadata, string $
7274
// When queryType is null, the field is not queryable
7375
if (isset($mapping['encrypt']['queryType'])) {
7476
$field['queries'] = array_filter($mapping['encrypt'], static fn ($v) => $v !== null);
77+
assert($field['queries']['queryType'] instanceof EncryptQuery);
78+
$field['queries']['queryType'] = $field['queries']['queryType']->value;
7579
}
7680

7781
yield $field;

tests/Documents/Encryption/PatientRecord.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class PatientRecord
1414

1515
public function __construct(
1616
#[ODM\Field]
17-
#[ODM\Encrypt(queryType: ODM\Encrypt::QUERY_TYPE_EQUALITY)]
17+
#[ODM\Encrypt(queryType: ODM\EncryptQuery::Equality)]
1818
public string $ssn,
1919
#[ODM\EmbedOne(targetDocument: PatientBilling::class)]
2020
#[ODM\Encrypt]
2121
public PatientBilling $billing,
2222
#[ODM\Field]
23-
#[ODM\Encrypt(queryType: ODM\Encrypt::QUERY_TYPE_RANGE, sparsity: 1, trimFactor: 4, min: 100, max: 2000)]
23+
#[ODM\Encrypt(queryType: ODM\EncryptQuery::Range, sparsity: 1, trimFactor: 4, min: 100, max: 2000)]
2424
public int $billingAmount,
2525
) {
2626
}

0 commit comments

Comments
 (0)