|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2018 Gerrit Addiks. |
|
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
|
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
|
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
|
7
|
|
|
* |
|
8
|
|
|
* @license GPL-3.0 |
|
9
|
|
|
* |
|
10
|
|
|
* @author Gerrit Addiks <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Addiks\RDMBundle\Mapping; |
|
14
|
|
|
|
|
15
|
|
|
use Addiks\RDMBundle\Mapping\MappingInterface; |
|
16
|
|
|
use Webmozart\Assert\Assert; |
|
17
|
|
|
use Doctrine\DBAL\Schema\Column; |
|
18
|
|
|
use Addiks\RDMBundle\Hydration\HydrationContextInterface; |
|
19
|
|
|
use Addiks\RDMBundle\Exception\FailedRDMAssertionException; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
21
|
|
|
|
|
22
|
|
|
final class ArrayMapping implements MappingInterface |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array<MappingInterface> |
|
27
|
|
|
*/ |
|
28
|
|
|
private $entryMappings = array(); |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $origin; |
|
34
|
|
|
|
|
35
|
11 |
|
public function __construct(array $entryMappings, string $origin = "unknown") |
|
36
|
|
|
{ |
|
37
|
11 |
|
$this->origin = $origin; |
|
38
|
|
|
|
|
39
|
11 |
|
foreach ($entryMappings as $key => $entryMapping) { |
|
40
|
|
|
/** @var MappingInterface $entryMapping */ |
|
41
|
|
|
|
|
42
|
11 |
|
Assert::isInstanceOf($entryMapping, MappingInterface::class); |
|
43
|
|
|
|
|
44
|
11 |
|
$this->entryMappings[$key] = $entryMapping; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
public function getEntryMappings(): array |
|
49
|
|
|
{ |
|
50
|
1 |
|
return $this->entryMappings; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
public function describeOrigin(): string |
|
54
|
|
|
{ |
|
55
|
1 |
|
return $this->origin; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
public function collectDBALColumns(): array |
|
59
|
|
|
{ |
|
60
|
|
|
/** @var array<Column> $dbalColumns */ |
|
61
|
2 |
|
$dbalColumns = array(); |
|
62
|
|
|
|
|
63
|
2 |
|
foreach ($this->entryMappings as $entryMapping) { |
|
64
|
|
|
/** @var MappingInterface $entryMapping */ |
|
65
|
|
|
|
|
66
|
2 |
|
$dbalColumns = array_merge( |
|
67
|
|
|
$dbalColumns, |
|
68
|
2 |
|
$entryMapping->collectDBALColumns() |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
2 |
|
return $dbalColumns; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
public function resolveValue( |
|
76
|
|
|
HydrationContextInterface $context, |
|
77
|
|
|
array $dataFromAdditionalColumns |
|
78
|
|
|
) { |
|
79
|
|
|
/** @var array<mixed> $value */ |
|
80
|
2 |
|
$value = array(); |
|
81
|
|
|
|
|
82
|
2 |
|
foreach ($this->entryMappings as $key => $entryMapping) { |
|
83
|
|
|
/** @var MappingInterface $entryMapping */ |
|
84
|
|
|
|
|
85
|
2 |
|
$value[$key] = $entryMapping->resolveValue( |
|
86
|
|
|
$context, |
|
87
|
|
|
$dataFromAdditionalColumns |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
return $value; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
3 |
|
public function revertValue( |
|
95
|
|
|
HydrationContextInterface $context, |
|
96
|
|
|
$valueFromEntityField |
|
97
|
|
|
): array { |
|
98
|
|
|
/** @var array<string, string> $data */ |
|
99
|
3 |
|
$data = array(); |
|
100
|
|
|
|
|
101
|
3 |
|
if (is_array($valueFromEntityField)) { |
|
102
|
1 |
|
foreach ($this->entryMappings as $key => $entryMapping) { |
|
103
|
|
|
/** @var MappingInterface $entryMapping */ |
|
104
|
|
|
|
|
105
|
|
|
/** @var mixed $valueFromEntry */ |
|
106
|
1 |
|
$valueFromEntry = null; |
|
107
|
|
|
|
|
108
|
1 |
|
if (isset($valueFromEntityField[$key])) { |
|
109
|
1 |
|
$valueFromEntry = $valueFromEntityField[$key]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
$data = array_merge( |
|
113
|
|
|
$data, |
|
114
|
1 |
|
$entryMapping->revertValue( |
|
115
|
|
|
$context, |
|
116
|
|
|
$valueFromEntry |
|
117
|
|
|
) |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
3 |
|
return $data; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
2 |
|
public function assertValue( |
|
126
|
|
|
HydrationContextInterface $context, |
|
127
|
|
|
array $dataFromAdditionalColumns, |
|
128
|
|
|
$actualValue |
|
129
|
|
|
): void { |
|
130
|
2 |
|
if (!is_array($actualValue) && !is_null($actualValue)) { |
|
131
|
1 |
|
throw FailedRDMAssertionException::expectedArray( |
|
132
|
|
|
$actualValue, |
|
133
|
1 |
|
$this->origin |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
1 |
|
public function wakeUpMapping(ContainerInterface $container): void |
|
139
|
|
|
{ |
|
140
|
1 |
|
foreach ($this->entryMappings as $key => $entryMapping) { |
|
141
|
|
|
/** @var MappingInterface $entryMapping */ |
|
142
|
|
|
|
|
143
|
1 |
|
$entryMapping->wakeUpMapping($container); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|