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 Addiks\RDMBundle\Hydration\HydrationContextInterface; |
17
|
|
|
use Addiks\RDMBundle\Exception\InvalidMappingException; |
18
|
|
|
use Doctrine\DBAL\Schema\Column; |
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
20
|
|
|
|
21
|
|
|
final class NullableMapping implements MappingInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var MappingInterface |
26
|
|
|
*/ |
27
|
|
|
private $innerMapping; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Column|null |
31
|
|
|
*/ |
32
|
|
|
private $dbalColumn; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $origin; |
38
|
|
|
|
39
|
|
|
/** @var bool */ |
40
|
|
|
private $strict; |
41
|
|
|
|
42
|
16 |
|
public function __construct( |
43
|
|
|
MappingInterface $innerMapping, |
44
|
|
|
Column $dbalColumn = null, |
45
|
|
|
string $origin = "undefined", |
46
|
|
|
bool $strict = false |
47
|
|
|
) { |
48
|
16 |
|
$this->innerMapping = $innerMapping; |
49
|
16 |
|
$this->dbalColumn = $dbalColumn; |
50
|
16 |
|
$this->origin = $origin; |
51
|
16 |
|
$this->strict = $strict; |
52
|
16 |
|
} |
53
|
|
|
|
54
|
1 |
|
public function getDBALColumn(): ?Column |
55
|
|
|
{ |
56
|
1 |
|
return $this->dbalColumn; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function getInnerMapping(): MappingInterface |
60
|
|
|
{ |
61
|
1 |
|
return $this->innerMapping; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function describeOrigin(): string |
65
|
|
|
{ |
66
|
1 |
|
return $this->origin; |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
public function collectDBALColumns(): array |
70
|
|
|
{ |
71
|
|
|
/** @var array<Column> $dbalColumns */ |
72
|
3 |
|
$dbalColumns = array(); |
73
|
|
|
|
74
|
3 |
|
foreach ($this->innerMapping->collectDBALColumns() as $dbalColumn) { |
75
|
|
|
/** @var Column $dbalColumn */ |
76
|
|
|
|
77
|
2 |
|
$dbalColumn = clone $dbalColumn; |
78
|
2 |
|
$dbalColumn->setNotnull(false); |
79
|
|
|
|
80
|
2 |
|
$dbalColumns[] = $dbalColumn; |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
if ($this->dbalColumn instanceof Column) { |
|
|
|
|
84
|
1 |
|
$dbalColumns[] = $this->dbalColumn; |
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
return $dbalColumns; |
88
|
|
|
} |
89
|
|
|
|
90
|
9 |
|
public function getDeterminatorColumnName(): ?string |
91
|
|
|
{ |
92
|
|
|
/** @var string|null $columnName */ |
93
|
9 |
|
$columnName = null; |
94
|
|
|
|
95
|
9 |
|
if ($this->dbalColumn instanceof Column) { |
|
|
|
|
96
|
7 |
|
$columnName = $this->dbalColumn->getName(); |
97
|
|
|
} |
98
|
|
|
|
99
|
9 |
|
return $columnName; |
100
|
|
|
} |
101
|
|
|
|
102
|
4 |
|
public function resolveValue( |
103
|
|
|
HydrationContextInterface $context, |
104
|
|
|
array $dataFromAdditionalColumns |
105
|
|
|
) { |
106
|
|
|
/** @var mixed|null $value */ |
107
|
4 |
|
$value = null; |
108
|
|
|
|
109
|
|
|
/** @var string|null $columnName */ |
110
|
4 |
|
$columnName = $this->getDeterminatorColumnName(); |
111
|
|
|
|
112
|
4 |
|
if (empty($columnName)) { |
113
|
|
|
/** @var array<Column> $columns */ |
114
|
2 |
|
$columns = $this->collectDBALColumns(); |
115
|
|
|
|
116
|
2 |
|
if (empty($columns)) { |
117
|
1 |
|
throw new InvalidMappingException(sprintf( |
118
|
1 |
|
"Nullable mapping needs at least one column (or subcolumn) in %s!", |
119
|
1 |
|
$this->origin |
120
|
|
|
)); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
$columnName = array_values($columns)[0]->getName(); |
124
|
|
|
} |
125
|
|
|
|
126
|
3 |
|
if (array_key_exists($columnName, $dataFromAdditionalColumns) |
127
|
3 |
|
&& ($this->strict ? !is_null($dataFromAdditionalColumns[$columnName]) : $dataFromAdditionalColumns[$columnName])) { |
128
|
2 |
|
$value = $this->innerMapping->resolveValue( |
129
|
2 |
|
$context, |
130
|
2 |
|
$dataFromAdditionalColumns |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
3 |
|
return $value; |
135
|
|
|
} |
136
|
|
|
|
137
|
4 |
|
public function revertValue( |
138
|
|
|
HydrationContextInterface $context, |
139
|
|
|
$valueFromEntityField |
140
|
|
|
): array { |
141
|
|
|
/** @var array<scalar> $data */ |
142
|
4 |
|
$data = array(); |
143
|
|
|
|
144
|
|
|
/** @var string|null $columnName */ |
145
|
4 |
|
$columnName = $this->getDeterminatorColumnName(); |
146
|
|
|
|
147
|
4 |
|
if (!is_null($valueFromEntityField)) { |
148
|
3 |
|
$data = $this->innerMapping->revertValue( |
149
|
3 |
|
$context, |
150
|
3 |
|
$valueFromEntityField |
151
|
|
|
); |
152
|
|
|
|
153
|
3 |
|
if (!empty($columnName) && !array_key_exists($columnName, $data)) { |
154
|
3 |
|
$data[$columnName] = "1"; |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
} elseif (!empty($columnName)) { |
158
|
1 |
|
$data[$columnName] = "0"; |
159
|
|
|
} |
160
|
|
|
|
161
|
4 |
|
return $data; |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
public function assertValue( |
165
|
|
|
HydrationContextInterface $context, |
166
|
|
|
array $dataFromAdditionalColumns, |
167
|
|
|
$actualValue |
168
|
|
|
): void { |
169
|
1 |
|
} |
170
|
|
|
|
171
|
1 |
|
public function wakeUpMapping(ContainerInterface $container): void |
172
|
|
|
{ |
173
|
1 |
|
$this->innerMapping->wakeUpMapping($container); |
174
|
1 |
|
} |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.