-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCryptographyMetadataFactory.php
More file actions
75 lines (56 loc) · 2.46 KB
/
Copy pathCryptographyMetadataFactory.php
File metadata and controls
75 lines (56 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace Patchlevel\Hydrator\Cryptography;
use Patchlevel\Hydrator\Attribute\DataSubjectId;
use Patchlevel\Hydrator\Attribute\SensitiveData;
use Patchlevel\Hydrator\Metadata\ClassMetadata;
use Patchlevel\Hydrator\Metadata\MetadataFactory;
use function array_key_exists;
final class CryptographyMetadataFactory implements MetadataFactory
{
public function __construct(
private readonly Cryptographer $cryptographer,
private readonly MetadataFactory $metadataFactory,
) {
}
public function metadata(string $class): ClassMetadata
{
$metadata = $this->metadataFactory->metadata($class);
$subjectIdMapping = [];
foreach ($metadata->properties as $property) {
$isSubjectId = false;
$attributeReflectionList = $property->reflection->getAttributes(DataSubjectId::class);
if ($attributeReflectionList) {
$subjectIdIdentifier = $attributeReflectionList[0]->newInstance()->name;
if (array_key_exists($subjectIdIdentifier, $subjectIdMapping)) {
throw new DuplicateSubjectIdIdentifier(
$metadata->className,
$metadata->propertyForField($subjectIdMapping[$subjectIdIdentifier])->propertyName,
$property->propertyName,
$subjectIdIdentifier,
);
}
$subjectIdMapping[$subjectIdIdentifier] = $property->fieldName;
$isSubjectId = true;
}
$attributeReflectionList = $property->reflection->getAttributes(SensitiveData::class);
if ($attributeReflectionList === []) {
continue;
}
if ($isSubjectId) {
throw new SubjectIdAndSensitiveDataConflict($metadata->className, $property->propertyName);
}
$attribute = $attributeReflectionList[0]->newInstance();
$property->normalizer = new CryptoNormalizer(
$this->cryptographer,
$attribute->subjectIdName,
$attribute->fallbackCallable !== null ? ($attribute->fallbackCallable)(...) : $attribute->fallback,
$property->normalizer,
);
}
if ($subjectIdMapping !== []) {
$metadata->extras[SubjectIdFieldMapping::class] = new SubjectIdFieldMapping($subjectIdMapping);
}
return $metadata;
}
}