|
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\Hydration\HydrationContextInterface; |
|
18
|
|
|
use Addiks\RDMBundle\Mapping\NullableMappingInterface; |
|
19
|
|
|
use Doctrine\DBAL\Schema\Column; |
|
20
|
|
|
use Addiks\RDMBundle\Exception\InvalidMappingException; |
|
21
|
|
|
|
|
22
|
|
|
final class NullableValueResolver implements ValueResolverInterface |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ValueResolverInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $rootValueResolver; |
|
29
|
|
|
|
|
30
|
12 |
|
public function __construct(ValueResolverInterface $rootValueResolver) |
|
31
|
|
|
{ |
|
32
|
12 |
|
$this->rootValueResolver = $rootValueResolver; |
|
33
|
12 |
|
} |
|
34
|
|
|
|
|
35
|
3 |
|
public function resolveValue( |
|
36
|
|
|
MappingInterface $fieldMapping, |
|
37
|
|
|
HydrationContextInterface $context, |
|
38
|
|
|
array $dataFromAdditionalColumns |
|
39
|
|
|
) { |
|
40
|
|
|
/** @var mixed|null $value */ |
|
41
|
3 |
|
$value = null; |
|
42
|
|
|
|
|
43
|
3 |
|
if ($fieldMapping instanceof NullableMappingInterface) { |
|
44
|
|
|
/** @var MappingInterface $innerMapping */ |
|
45
|
3 |
|
$innerMapping = $fieldMapping->getInnerMapping(); |
|
46
|
|
|
|
|
47
|
|
|
/** @var string|null $columnName */ |
|
48
|
3 |
|
$columnName = $fieldMapping->getDeterminatorColumnName(); |
|
49
|
|
|
|
|
50
|
3 |
|
if (empty($columnName)) { |
|
51
|
|
|
/** @var array<Column> $columns */ |
|
52
|
3 |
|
$columns = $fieldMapping->collectDBALColumns(); |
|
53
|
|
|
|
|
54
|
3 |
|
if (empty($columns)) { |
|
55
|
1 |
|
throw new InvalidMappingException(sprintf( |
|
56
|
1 |
|
"Nullable mapping needs at least one column (or subcolumn) in %s!", |
|
57
|
1 |
|
$fieldMapping->describeOrigin() |
|
58
|
|
|
)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
$columnName = array_values($columns)[0]->getName(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
if (array_key_exists($columnName, $dataFromAdditionalColumns) |
|
65
|
2 |
|
&& $dataFromAdditionalColumns[$columnName]) { |
|
66
|
1 |
|
$value = $this->rootValueResolver->resolveValue( |
|
67
|
1 |
|
$innerMapping, |
|
68
|
1 |
|
$context, |
|
69
|
1 |
|
$dataFromAdditionalColumns |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
return $value; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
4 |
|
public function revertValue( |
|
78
|
|
|
MappingInterface $fieldMapping, |
|
79
|
|
|
HydrationContextInterface $context, |
|
80
|
|
|
$valueFromEntityField |
|
81
|
|
|
): array { |
|
82
|
|
|
/** @var array<scalar> $data */ |
|
83
|
4 |
|
$data = array(); |
|
84
|
|
|
|
|
85
|
4 |
|
if ($fieldMapping instanceof NullableMappingInterface && !is_null($valueFromEntityField)) { |
|
86
|
|
|
/** @var MappingInterface $innerMapping */ |
|
87
|
3 |
|
$innerMapping = $fieldMapping->getInnerMapping(); |
|
88
|
|
|
|
|
89
|
|
|
/** @var string|null $columnName */ |
|
90
|
3 |
|
$columnName = $fieldMapping->getDeterminatorColumnName(); |
|
91
|
|
|
|
|
92
|
3 |
|
$data = $this->rootValueResolver->revertValue( |
|
93
|
3 |
|
$innerMapping, |
|
94
|
3 |
|
$context, |
|
95
|
3 |
|
$valueFromEntityField |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
3 |
|
if (!empty($columnName) && !array_key_exists($columnName, $data)) { |
|
99
|
1 |
|
$data[$columnName] = true; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
4 |
|
return $data; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
1 |
|
public function assertValue( |
|
107
|
|
|
MappingInterface $fieldMapping, |
|
108
|
|
|
HydrationContextInterface $context, |
|
109
|
|
|
array $dataFromAdditionalColumns, |
|
110
|
|
|
$actualValue |
|
111
|
|
|
): void { |
|
112
|
1 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|