1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace ReadModel\Walker; |
5
|
|
|
|
6
|
|
|
use ReadModel\InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
final class WalkerBuilder |
9
|
|
|
{ |
10
|
|
|
/** @var BinaryUuidTransformer */ |
11
|
|
|
private $transformer; |
12
|
|
|
|
13
|
|
|
/** @var ResultWalker[] */ |
14
|
|
|
private $walkers = []; |
15
|
|
|
|
16
|
|
|
/** @var callable[] */ |
17
|
|
|
private $callables = []; |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
private $prefixes = []; |
21
|
|
|
|
22
|
|
|
/** @var array */ |
23
|
|
|
private $binaryKeys = []; |
24
|
|
|
|
25
|
|
|
/** @var array */ |
26
|
|
|
private $scalarMapping = []; |
27
|
|
|
|
28
|
|
|
/** @var bool */ |
29
|
|
|
private $camelCase = false; |
30
|
|
|
|
31
|
2 |
|
public function __construct(BinaryUuidTransformer $transformer = null) |
32
|
|
|
{ |
33
|
2 |
|
$this->transformer = $transformer; |
34
|
2 |
|
} |
35
|
|
|
|
36
|
2 |
|
public function addWalker(ResultWalker $walker): self |
37
|
|
|
{ |
38
|
2 |
|
$this->walkers[] = $walker; |
39
|
2 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Embed walker is added as the first walker. |
44
|
|
|
*/ |
45
|
2 |
|
public function withEmbedded(...$prefixes): self |
46
|
|
|
{ |
47
|
2 |
|
$this->prefixes = array_merge($this->prefixes, $prefixes); |
48
|
2 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Callable walkers are added after embed walker. |
54
|
|
|
*/ |
55
|
2 |
|
public function with(callable $callable): self |
56
|
|
|
{ |
57
|
2 |
|
$this->callables[] = $callable; |
58
|
2 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Binary casting is added after callable walkers. |
63
|
|
|
*/ |
64
|
2 |
|
public function withBinaryUuidCasting(...$keys): self |
65
|
|
|
{ |
66
|
2 |
|
if ($this->transformer === null) { |
67
|
|
|
throw new InvalidArgumentException( |
68
|
|
|
'You have to provide BinaryUuidTransformer to use BinaryUuidTransformerWalker' |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
$this->binaryKeys = array_merge($this->binaryKeys, $keys); |
73
|
2 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Scalar casting is added after binary casting walker. |
78
|
|
|
*/ |
79
|
2 |
|
public function withScalarCasting(array $mapping): self |
80
|
|
|
{ |
81
|
2 |
|
$this->scalarMapping = array_merge($this->scalarMapping, $mapping); |
82
|
2 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Camel cased walker is added as the last walker. |
87
|
|
|
*/ |
88
|
2 |
|
public function withCamelCasedFieldNames(): self |
89
|
|
|
{ |
90
|
2 |
|
$this->camelCase = true; |
91
|
2 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
public function build(): ResultWalker |
95
|
|
|
{ |
96
|
2 |
|
if (!empty($this->prefixes)) { |
97
|
2 |
|
$this->addWalker(new EmbedWalker(...$this->prefixes)); |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
foreach ($this->callables as $callable) { |
101
|
2 |
|
$this->addWalker(new CallableWalker($callable)); |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
if (!empty($this->prefixes)) { |
105
|
2 |
|
$this->addWalker(new BinaryUuidTransformerWalker($this->transformer, ...$this->binaryKeys)); |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
if (!empty($this->prefixes)) { |
109
|
2 |
|
$this->addWalker(new ScalarTransformerWalker($this->scalarMapping)); |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
if ($this->camelCase) { |
113
|
2 |
|
$this->addWalker(new KeysToCamelCaseWalker()); |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
$walker = new ChainWalker(...$this->walkers); |
117
|
2 |
|
$this->clearBuilder(); |
118
|
|
|
|
119
|
2 |
|
return $walker; |
120
|
|
|
} |
121
|
|
|
|
122
|
2 |
|
private function clearBuilder(): void |
123
|
|
|
{ |
124
|
2 |
|
$this->walkers = []; |
125
|
2 |
|
$this->callables = []; |
126
|
2 |
|
$this->prefixes = []; |
127
|
2 |
|
$this->binaryKeys = []; |
128
|
2 |
|
$this->scalarMapping = []; |
129
|
2 |
|
$this->camelCase = false; |
130
|
2 |
|
} |
131
|
|
|
} |
132
|
|
|
|