|
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 Doctrine\DBAL\Schema\Column; |
|
16
|
|
|
use Addiks\RDMBundle\Hydration\HydrationContextInterface; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
18
|
|
|
use Doctrine\DBAL\Types\Type; |
|
19
|
|
|
use Doctrine\DBAL\Connection; |
|
20
|
|
|
use Addiks\RDMBundle\Mapping\MappingInterface; |
|
21
|
|
|
use Webmozart\Assert\Assert; |
|
22
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
23
|
|
|
|
|
24
|
|
|
final class FieldMapping implements MappingInterface |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Column |
|
29
|
|
|
*/ |
|
30
|
|
|
private $dbalColumn; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $origin; |
|
36
|
|
|
|
|
37
|
10 |
|
public function __construct( |
|
38
|
|
|
Column $dbalColumn, |
|
39
|
|
|
string $origin = "unknown" |
|
40
|
|
|
) { |
|
41
|
10 |
|
$this->dbalColumn = $dbalColumn; |
|
42
|
10 |
|
$this->origin = $origin; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function getDBALColumn(): Column |
|
46
|
|
|
{ |
|
47
|
1 |
|
return $this->dbalColumn; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function describeOrigin(): string |
|
51
|
|
|
{ |
|
52
|
1 |
|
return $this->origin; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
public function collectDBALColumns(): array |
|
56
|
|
|
{ |
|
57
|
2 |
|
return [$this->dbalColumn]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
public function resolveValue( |
|
61
|
|
|
HydrationContextInterface $context, |
|
62
|
|
|
array $dataFromAdditionalColumns |
|
63
|
|
|
) { |
|
64
|
|
|
/** @var mixed $value */ |
|
65
|
2 |
|
$value = null; |
|
66
|
|
|
|
|
67
|
|
|
/** @var Type $type */ |
|
68
|
2 |
|
$type = $this->dbalColumn->getType(); |
|
69
|
|
|
|
|
70
|
|
|
/** @var Connection $connection */ |
|
71
|
2 |
|
$connection = $context->getEntityManager()->getConnection(); |
|
72
|
|
|
|
|
73
|
|
|
/** @var AbstractPlatform $platform */ |
|
74
|
2 |
|
$platform = $connection->getDatabasePlatform(); |
|
75
|
|
|
|
|
76
|
|
|
/** @var string $columnName */ |
|
77
|
2 |
|
$columnName = $this->dbalColumn->getName(); |
|
78
|
|
|
|
|
79
|
2 |
|
if (isset($dataFromAdditionalColumns[$columnName])) { |
|
80
|
2 |
|
$value = $dataFromAdditionalColumns[$columnName]; |
|
81
|
2 |
|
$value = $type->convertToPHPValue($value, $platform); |
|
82
|
|
|
|
|
83
|
|
|
} elseif (isset($dataFromAdditionalColumns[''])) { |
|
84
|
|
|
if (isset($dataFromAdditionalColumns[''][$columnName])) { |
|
85
|
|
|
$value = $dataFromAdditionalColumns[''][$columnName]; |
|
86
|
|
|
$value = $type->convertToPHPValue($value, $platform); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
return $value; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
public function revertValue( |
|
94
|
|
|
HydrationContextInterface $context, |
|
95
|
|
|
$valueFromEntityField |
|
96
|
|
|
): array { |
|
97
|
|
|
/** @var array<string, mixed> $data */ |
|
98
|
2 |
|
$data = array(); |
|
99
|
|
|
|
|
100
|
|
|
/** @var Type $type */ |
|
101
|
2 |
|
$type = $this->dbalColumn->getType(); |
|
102
|
|
|
|
|
103
|
|
|
/** @var Connection $connection */ |
|
104
|
2 |
|
$connection = $context->getEntityManager()->getConnection(); |
|
105
|
|
|
|
|
106
|
|
|
/** @var scalar|null $databaseValue */ |
|
107
|
2 |
|
$databaseValue = $type->convertToDatabaseValue( |
|
108
|
|
|
$valueFromEntityField, |
|
109
|
2 |
|
$connection->getDatabasePlatform() |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
2 |
|
if (!is_null($databaseValue)) { |
|
113
|
1 |
|
Assert::scalar($databaseValue); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
2 |
|
$data[$this->dbalColumn->getName()] = $databaseValue; |
|
117
|
|
|
|
|
118
|
2 |
|
return $data; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function assertValue( |
|
122
|
|
|
HydrationContextInterface $context, |
|
123
|
|
|
array $dataFromAdditionalColumns, |
|
124
|
|
|
$actualValue |
|
125
|
|
|
): void { |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function wakeUpMapping(ContainerInterface $container): void |
|
129
|
|
|
{ |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|