-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLifecycleMetadataEnricher.php
More file actions
73 lines (57 loc) · 2.76 KB
/
Copy pathLifecycleMetadataEnricher.php
File metadata and controls
73 lines (57 loc) · 2.76 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
<?php
declare(strict_types=1);
namespace Patchlevel\Hydrator\Extension\Lifecycle;
use LogicException;
use Patchlevel\Hydrator\Extension\Lifecycle\Attribute\PostExtract;
use Patchlevel\Hydrator\Extension\Lifecycle\Attribute\PostHydrate;
use Patchlevel\Hydrator\Extension\Lifecycle\Attribute\PreExtract;
use Patchlevel\Hydrator\Extension\Lifecycle\Attribute\PreHydrate;
use Patchlevel\Hydrator\Metadata\ClassMetadata;
use Patchlevel\Hydrator\Metadata\MetadataEnricher;
use function sprintf;
final class LifecycleMetadataEnricher implements MetadataEnricher
{
public function enrich(ClassMetadata $classMetadata): void
{
$preHydrate = null;
$postHydrate = null;
$preExtract = null;
$postExtract = null;
foreach ($classMetadata->reflection->getMethods() as $reflectionMethod) {
if ($reflectionMethod->getAttributes(PreHydrate::class)) {
if ($reflectionMethod->isStatic() === false) {
throw new LogicException(sprintf('Method "%s::%s" must be static when using the PreHydrate attribute.', $classMetadata->className, $reflectionMethod->getName()));
}
$preHydrate = $reflectionMethod->getName();
}
if ($reflectionMethod->getAttributes(PostHydrate::class)) {
if ($reflectionMethod->isStatic() === false) {
throw new LogicException(sprintf('Method "%s::%s" must be static when using the PostHydrate attribute.', $classMetadata->className, $reflectionMethod->getName()));
}
$postHydrate = $reflectionMethod->getName();
}
if ($reflectionMethod->getAttributes(PreExtract::class)) {
if ($reflectionMethod->isStatic() === false) {
throw new LogicException(sprintf('Method "%s::%s" must be static when using the PreExtract attribute.', $classMetadata->className, $reflectionMethod->getName()));
}
$preExtract = $reflectionMethod->getName();
}
if (!$reflectionMethod->getAttributes(PostExtract::class)) {
continue;
}
if ($reflectionMethod->isStatic() === false) {
throw new LogicException(sprintf('Method "%s::%s" must be static when using the PostExtract attribute.', $classMetadata->className, $reflectionMethod->getName()));
}
$postExtract = $reflectionMethod->getName();
}
if ($preHydrate === null && $postHydrate === null && $preExtract === null && $postExtract === null) {
return;
}
$classMetadata->extras[Lifecycle::class] = new Lifecycle(
$preHydrate,
$postHydrate,
$preExtract,
$postExtract,
);
}
}