|
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\ValueResolver; |
|
14
|
|
|
|
|
15
|
|
|
use Addiks\RDMBundle\ValueResolver\ValueResolverInterface; |
|
16
|
|
|
use Addiks\RDMBundle\Mapping\MappingInterface; |
|
17
|
|
|
use Addiks\RDMBundle\Mapping\ArrayMappingInterface; |
|
18
|
|
|
use Addiks\RDMBundle\Exception\FailedRDMAssertionException; |
|
19
|
|
|
|
|
20
|
|
|
final class ArrayValueResolver implements ValueResolverInterface |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var ValueResolverInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $entryValueResolver; |
|
27
|
|
|
|
|
28
|
8 |
|
public function __construct(ValueResolverInterface $entryValueResolver) |
|
29
|
|
|
{ |
|
30
|
8 |
|
$this->entryValueResolver = $entryValueResolver; |
|
31
|
8 |
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
public function resolveValue( |
|
34
|
|
|
MappingInterface $arrayMapping, |
|
35
|
|
|
$entity, |
|
36
|
|
|
array $dataFromAdditionalColumns |
|
37
|
|
|
) { |
|
38
|
|
|
/** @var null|array<mixed> $value */ |
|
39
|
2 |
|
$value = null; |
|
40
|
|
|
|
|
41
|
2 |
|
if ($arrayMapping instanceof ArrayMappingInterface) { |
|
42
|
2 |
|
$value = array(); |
|
43
|
|
|
|
|
44
|
2 |
|
foreach ($arrayMapping->getEntryMappings() as $key => $entryMapping) { |
|
45
|
|
|
/** @var MappingInterface $entryMapping */ |
|
46
|
|
|
|
|
47
|
2 |
|
$value[$key] = $this->entryValueResolver->resolveValue( |
|
48
|
2 |
|
$entryMapping, |
|
49
|
2 |
|
$entity, |
|
50
|
2 |
|
$dataFromAdditionalColumns |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
return $value; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
3 |
|
public function revertValue( |
|
59
|
|
|
MappingInterface $arrayMapping, |
|
60
|
|
|
$entity, |
|
61
|
|
|
$valueFromEntityField |
|
62
|
|
|
): array { |
|
63
|
|
|
/** @var array<string, string> $data */ |
|
64
|
3 |
|
$data = array(); |
|
65
|
|
|
|
|
66
|
3 |
|
if ($arrayMapping instanceof ArrayMappingInterface && is_array($valueFromEntityField)) { |
|
67
|
1 |
|
foreach ($arrayMapping->getEntryMappings() as $key => $entryMapping) { |
|
68
|
|
|
/** @var MappingInterface $entryMapping */ |
|
69
|
|
|
|
|
70
|
|
|
/** @var mixed $valueFromEntry */ |
|
71
|
1 |
|
$valueFromEntry = null; |
|
72
|
|
|
|
|
73
|
1 |
|
if (isset($valueFromEntityField[$key])) { |
|
74
|
1 |
|
$valueFromEntry = $valueFromEntityField[$key]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
$data = array_merge( |
|
78
|
1 |
|
$data, |
|
79
|
1 |
|
$this->entryValueResolver->revertValue( |
|
80
|
1 |
|
$entryMapping, |
|
81
|
1 |
|
$entity, |
|
82
|
1 |
|
$valueFromEntry |
|
83
|
|
|
) |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
3 |
|
return $data; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
public function assertValue( |
|
92
|
|
|
MappingInterface $arrayMapping, |
|
93
|
|
|
$entity, |
|
94
|
|
|
array $dataFromAdditionalColumns, |
|
95
|
|
|
$actualValue |
|
96
|
|
|
): void { |
|
97
|
2 |
|
if ($arrayMapping instanceof ArrayMappingInterface) { |
|
98
|
2 |
|
if (!is_array($actualValue) && !is_null($actualValue)) { |
|
99
|
1 |
|
throw FailedRDMAssertionException::expectedArray( |
|
100
|
1 |
|
$actualValue, |
|
101
|
1 |
|
$arrayMapping->describeOrigin() |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|