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\Exception\FailedRDMAssertionException; |
17
|
|
|
use Addiks\RDMBundle\Mapping\ListMappingInterface; |
18
|
|
|
use Addiks\RDMBundle\Mapping\MappingInterface; |
19
|
|
|
use Addiks\RDMBundle\Hydration\HydrationContextInterface; |
20
|
|
|
|
21
|
|
|
class ListValueResolver implements ValueResolverInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ValueResolverInterface |
26
|
|
|
*/ |
27
|
|
|
private $entryValueResolver; |
28
|
|
|
|
29
|
9 |
|
public function __construct(ValueResolverInterface $entryValueResolver) |
30
|
|
|
{ |
31
|
9 |
|
$this->entryValueResolver = $entryValueResolver; |
32
|
9 |
|
} |
33
|
|
|
|
34
|
1 |
|
public function resolveValue( |
35
|
|
|
MappingInterface $listMapping, |
36
|
|
|
HydrationContextInterface $context, |
37
|
|
|
array $dataFromAdditionalColumns |
38
|
|
|
) { |
39
|
|
|
/** @var null|array<mixed> $value */ |
40
|
1 |
|
$value = null; |
41
|
|
|
|
42
|
1 |
|
if ($listMapping instanceof ListMappingInterface) { |
43
|
|
|
/** @var MappingInterface $entryMapping */ |
44
|
1 |
|
$entryMapping = $listMapping->getEntryMapping(); |
45
|
|
|
|
46
|
|
|
/** @var string $columnName */ |
47
|
1 |
|
$columnName = $listMapping->getDBALColumn()->getName(); |
48
|
|
|
|
49
|
1 |
|
if (isset($dataFromAdditionalColumns[$columnName])) { |
50
|
|
|
/** @var array<string> $rawValues */ |
51
|
1 |
|
$serializedValues = json_decode($dataFromAdditionalColumns[$columnName], true); |
52
|
|
|
|
53
|
1 |
|
if (is_array($serializedValues)) { |
54
|
1 |
|
$value = array(); |
55
|
|
|
|
56
|
1 |
|
foreach ($serializedValues as $key => $entryData) { |
57
|
1 |
|
$value[$key] = $this->entryValueResolver->resolveValue( |
58
|
1 |
|
$entryMapping, |
59
|
1 |
|
$context, |
60
|
|
|
[ |
61
|
1 |
|
'' => $entryData, |
62
|
1 |
|
$columnName => $entryData |
63
|
|
|
] |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return $value; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
public function revertValue( |
74
|
|
|
MappingInterface $listMapping, |
75
|
|
|
HydrationContextInterface $context, |
76
|
|
|
$valueFromEntityField |
77
|
|
|
): array { |
78
|
|
|
/** @var array<string, string> $data */ |
79
|
2 |
|
$data = array(); |
80
|
|
|
|
81
|
2 |
|
if ($listMapping instanceof ListMappingInterface && is_array($valueFromEntityField)) { |
82
|
|
|
/** @var MappingInterface $entryMapping */ |
83
|
1 |
|
$entryMapping = $listMapping->getEntryMapping(); |
84
|
|
|
|
85
|
|
|
/** @var string $columnName */ |
86
|
1 |
|
$columnName = $listMapping->getDBALColumn()->getName(); |
87
|
|
|
|
88
|
|
|
/** @var array<string> $serializedValues */ |
89
|
1 |
|
$serializedValues = array(); |
90
|
|
|
|
91
|
1 |
|
foreach ($valueFromEntityField as $key => $valueFromEntry) { |
92
|
|
|
/** @var mixed $valueFromEntry */ |
93
|
|
|
|
94
|
1 |
|
$entryData = $this->entryValueResolver->revertValue( |
95
|
1 |
|
$entryMapping, |
96
|
1 |
|
$context, |
97
|
1 |
|
$valueFromEntry |
98
|
|
|
); |
99
|
|
|
|
100
|
1 |
|
if (count($entryData) === 1) { |
101
|
1 |
|
$serializedValues[$key] = array_values($entryData)[0]; |
102
|
|
|
|
103
|
|
|
} elseif (isset($entryData[$columnName])) { |
104
|
1 |
|
$serializedValues[$key] = $entryData; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
$data[$columnName] = json_encode($serializedValues); |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
return $data; |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
View Code Duplication |
public function assertValue( |
|
|
|
|
115
|
|
|
MappingInterface $listMapping, |
116
|
|
|
HydrationContextInterface $context, |
117
|
|
|
array $dataFromAdditionalColumns, |
118
|
|
|
$actualValue |
119
|
|
|
): void { |
120
|
2 |
|
if ($listMapping instanceof ListMappingInterface) { |
121
|
2 |
|
if (!is_array($actualValue) && !is_null($actualValue)) { |
122
|
1 |
|
throw FailedRDMAssertionException::expectedArray( |
123
|
1 |
|
$actualValue, |
124
|
1 |
|
$listMapping->describeOrigin() |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
} |
128
|
1 |
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.