Skip to content

Commit 56c3279

Browse files
committed
move encrypted field name logic into metadata
1 parent 58936e9 commit 56c3279

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/Cryptography/PersonalDataPayloadCryptographer.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
final class PersonalDataPayloadCryptographer implements PayloadCryptographer
2121
{
22-
public const ENCRYPTED_PREFIX = '!';
23-
2422
public function __construct(
2523
private readonly CipherKeyStore $cipherKeyStore,
2624
private readonly CipherKeyFactory $cipherKeyFactory,
@@ -54,7 +52,7 @@ public function encrypt(ClassMetadata $metadata, array $data): array
5452
continue;
5553
}
5654

57-
$data[self::ENCRYPTED_PREFIX . $propertyMetadata->fieldName()] = $this->cipher->encrypt(
55+
$data[$propertyMetadata->encryptedFieldName()] = $this->cipher->encrypt(
5856
$cipherKey,
5957
$data[$propertyMetadata->fieldName()],
6058
);
@@ -89,11 +87,9 @@ public function decrypt(ClassMetadata $metadata, array $data): array
8987
continue;
9088
}
9189

92-
$fieldNameWithPrefix = self::ENCRYPTED_PREFIX . $propertyMetadata->fieldName();
93-
94-
if (array_key_exists($fieldNameWithPrefix, $data)) {
95-
$rawData = $data[$fieldNameWithPrefix];
96-
unset($data[$fieldNameWithPrefix]);
90+
if (array_key_exists($propertyMetadata->encryptedFieldName(), $data)) {
91+
$rawData = $data[$propertyMetadata->encryptedFieldName()];
92+
unset($data[$propertyMetadata->encryptedFieldName()]);
9793
} elseif ($this->fallbackWithoutPrefix) {
9894
$rawData = $data[$propertyMetadata->fieldName()];
9995
} else {

src/Metadata/PropertyMetadata.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Patchlevel\Hydrator\Metadata;
66

77
use InvalidArgumentException;
8-
use Patchlevel\Hydrator\Cryptography\PersonalDataPayloadCryptographer;
98
use Patchlevel\Hydrator\Normalizer\Normalizer;
109
use ReflectionProperty;
1110

@@ -23,14 +22,16 @@
2322
*/
2423
final class PropertyMetadata
2524
{
25+
private const ENCRYPTED_PREFIX = '!';
26+
2627
public function __construct(
2728
private readonly ReflectionProperty $reflection,
2829
private readonly string $fieldName,
2930
private readonly Normalizer|null $normalizer = null,
3031
private readonly bool $isPersonalData = false,
3132
private readonly mixed $personalDataFallback = null,
3233
) {
33-
if (str_starts_with($fieldName, PersonalDataPayloadCryptographer::ENCRYPTED_PREFIX)) {
34+
if (str_starts_with($fieldName, self::ENCRYPTED_PREFIX)) {
3435
throw new InvalidArgumentException('fieldName must not start with !');
3536
}
3637
}
@@ -50,6 +51,11 @@ public function fieldName(): string
5051
return $this->fieldName;
5152
}
5253

54+
public function encryptedFieldName(): string
55+
{
56+
return self::ENCRYPTED_PREFIX . $this->fieldName;
57+
}
58+
5359
public function normalizer(): Normalizer|null
5460
{
5561
return $this->normalizer;

0 commit comments

Comments
 (0)