|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MetadataMapping\Metadata; |
|
6
|
|
|
|
|
7
|
|
|
use ReflectionClass; |
|
8
|
|
|
use Exception\ApplicationException; |
|
9
|
|
|
|
|
10
|
|
|
class DataMap |
|
11
|
|
|
{ |
|
12
|
|
|
private ReflectionClass $domainClass; |
|
13
|
|
|
|
|
14
|
|
|
private string $tableName; |
|
15
|
|
|
|
|
16
|
|
|
private array $columnMaps = []; |
|
17
|
|
|
|
|
18
|
5 |
|
public function __construct(string $domainClass, string $tableName) |
|
19
|
|
|
{ |
|
20
|
5 |
|
$this->domainClass = new ReflectionClass($domainClass); |
|
21
|
5 |
|
$this->tableName = $tableName; |
|
22
|
5 |
|
} |
|
23
|
|
|
|
|
24
|
5 |
|
public function getDomainClass(): ReflectionClass |
|
25
|
|
|
{ |
|
26
|
5 |
|
return $this->domainClass; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
1 |
|
public function getTableName(): string |
|
30
|
|
|
{ |
|
31
|
1 |
|
return $this->tableName; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
5 |
|
public function getColumnMaps(): array |
|
35
|
|
|
{ |
|
36
|
5 |
|
return $this->columnMaps; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
5 |
|
public function addColumn( |
|
40
|
|
|
string $columnName, |
|
41
|
|
|
string $type, |
|
42
|
|
|
string $fieldName |
|
43
|
|
|
): void { |
|
44
|
5 |
|
$this->columnMaps[] = new ColumnMap( |
|
45
|
5 |
|
$columnName, |
|
46
|
5 |
|
$fieldName, |
|
47
|
5 |
|
$type, |
|
48
|
5 |
|
$this |
|
49
|
|
|
); |
|
50
|
5 |
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
public function columnList(): string |
|
53
|
|
|
{ |
|
54
|
|
|
$columnNames = array_map(function (ColumnMap $columnMap) { |
|
55
|
1 |
|
return $columnMap->getColumnName(); |
|
56
|
1 |
|
}, $this->columnMaps); |
|
57
|
|
|
|
|
58
|
1 |
|
return " id," . implode(",", $columnNames); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
public function getColumnForField(string $fieldName): string |
|
62
|
|
|
{ |
|
63
|
2 |
|
foreach ($this->getColumnMaps() as $columnMap) { |
|
64
|
2 |
|
if ($columnMap->getFieldName() == $fieldName) { |
|
65
|
2 |
|
return $columnMap->getColumnName(); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
throw new ApplicationException( |
|
70
|
|
|
sprintf("Unable to find column for `%s`", $fieldName) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|