-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStackHydratorBuilder.php
More file actions
134 lines (102 loc) · 3.38 KB
/
Copy pathStackHydratorBuilder.php
File metadata and controls
134 lines (102 loc) · 3.38 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
declare(strict_types=1);
namespace Patchlevel\Hydrator;
use Patchlevel\Hydrator\Guesser\ChainGuesser;
use Patchlevel\Hydrator\Guesser\Guesser;
use Patchlevel\Hydrator\Metadata\AttributeMetadataFactory;
use Patchlevel\Hydrator\Metadata\EnrichingMetadataFactory;
use Patchlevel\Hydrator\Metadata\MetadataEnricher;
use Patchlevel\Hydrator\Metadata\Psr16MetadataFactory;
use Patchlevel\Hydrator\Metadata\Psr6MetadataFactory;
use Patchlevel\Hydrator\Middleware\Middleware;
use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;
use function array_merge;
use function krsort;
/** @experimental */
final class StackHydratorBuilder
{
private bool $defaultLazy = false;
/** @var array<int, list<Middleware>> */
private array $middlewares = [];
/** @var array<int, list<MetadataEnricher>> */
private array $metadataEnrichers = [];
/** @var array<int, list<Guesser>> */
private array $guessers = [];
private CacheItemPoolInterface|CacheInterface|null $cache = null;
/** @return $this */
public function addMiddleware(Middleware $middleware, int $priority = Extension::PRIORITY_BEFORE_TRANSFORM): static
{
$this->middlewares[$priority][] = $middleware;
return $this;
}
/** @return $this */
public function addMetadataEnricher(MetadataEnricher $enricher, int $priority = 0): static
{
$this->metadataEnrichers[$priority][] = $enricher;
return $this;
}
/** @return $this */
public function addGuesser(Guesser $guesser, int $priority = 0): static
{
$this->guessers[$priority][] = $guesser;
return $this;
}
public function enableDefaultLazy(bool $lazy = true): static
{
$this->defaultLazy = $lazy;
return $this;
}
public function useExtension(Extension $extension): static
{
$extension->configure($this);
return $this;
}
public function setCache(CacheItemPoolInterface|CacheInterface|null $cache): static
{
$this->cache = $cache;
return $this;
}
public function build(): StackHydrator
{
$metadataFactory = new EnrichingMetadataFactory(
new AttributeMetadataFactory(
guesser: new ChainGuesser($this->guessers()),
),
$this->metadataEnrichers(),
);
if ($this->cache instanceof CacheItemPoolInterface) {
$metadataFactory = new Psr6MetadataFactory($metadataFactory, $this->cache);
}
if ($this->cache instanceof CacheInterface) {
$metadataFactory = new Psr16MetadataFactory($metadataFactory, $this->cache);
}
return new StackHydrator(
$metadataFactory,
$this->middlewares(),
$this->defaultLazy,
);
}
public function defaultLazy(): bool
{
return $this->defaultLazy;
}
/** @return list<Middleware> */
public function middlewares(): array
{
krsort($this->middlewares);
return array_merge(...$this->middlewares);
}
/** @return list<Guesser> */
public function guessers(): array
{
krsort($this->guessers);
return array_merge(...$this->guessers);
}
/** @return list<MetadataEnricher> */
public function metadataEnrichers(): array
{
krsort($this->metadataEnrichers);
return array_merge(...$this->metadataEnrichers);
}
}