-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStackHydratorBuilder.php
More file actions
109 lines (83 loc) · 2.88 KB
/
StackHydratorBuilder.php
File metadata and controls
109 lines (83 loc) · 2.88 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
<?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 = 0): 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
{
krsort($this->guessers);
krsort($this->metadataEnrichers);
krsort($this->middlewares);
$metadataFactory = new EnrichingMetadataFactory(
new AttributeMetadataFactory(
guesser: new ChainGuesser(array_merge(...$this->guessers)),
),
array_merge(...$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,
array_merge(...$this->middlewares),
$this->defaultLazy,
);
}
}