1
|
|
|
<?php |
2
|
|
|
/******************************************************************************** |
3
|
|
|
* Apache License, Version 2.0 * |
4
|
|
|
* * |
5
|
|
|
* Copyright [2020] [Nurlan Mukhanov <[email protected]>] * |
6
|
|
|
* * |
7
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); * |
8
|
|
|
* you may not use this file except in compliance with the License. * |
9
|
|
|
* You may obtain a copy of the License at * |
10
|
|
|
* * |
11
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 * |
12
|
|
|
* * |
13
|
|
|
* Unless required by applicable law or agreed to in writing, software * |
14
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, * |
15
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * |
16
|
|
|
* See the License for the specific language governing permissions and * |
17
|
|
|
* limitations under the License. * |
18
|
|
|
* * |
19
|
|
|
********************************************************************************/ |
20
|
|
|
|
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace DBD\Entity; |
24
|
|
|
|
25
|
|
|
use DBD\Entity\Common\EntityException; |
26
|
|
|
use ReflectionException; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Trait MapperTrait |
30
|
|
|
* |
31
|
|
|
* @package DBD\Entity |
32
|
|
|
*/ |
33
|
|
|
trait MapperTrait |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @return Complex[] |
37
|
|
|
*/ |
38
|
|
|
public function getComplex(): array |
39
|
|
|
{ |
40
|
|
|
return MapperCache::me()->complex[$this->name()] ?? []; |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return Column[] |
45
|
|
|
*/ |
46
|
|
|
public function getColumns(): array |
47
|
|
|
{ |
48
|
|
|
return MapperCache::me()->columns[$this->name()] ?? []; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return Constraint[] |
53
|
|
|
*/ |
54
|
|
|
public function getConstraints(): array |
55
|
|
|
{ |
56
|
|
|
return MapperCache::me()->constraints[$this->name()]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return Embedded[] |
61
|
|
|
*/ |
62
|
|
|
public function getEmbedded(): array |
63
|
|
|
{ |
64
|
|
|
return MapperCache::me()->embedded[$this->name()] ?? []; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $originName |
69
|
|
|
* |
70
|
|
|
* @return Column |
71
|
|
|
* @throws EntityException |
72
|
|
|
*/ |
73
|
|
|
public function findColumnByOriginName(string $originName): Column |
74
|
|
|
{ |
75
|
|
|
foreach ($this->getColumns() as $column) { |
76
|
|
|
if ($column->name == $originName) { |
77
|
|
|
return $column; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
throw new EntityException(sprintf("Can't find origin column '%s' in %s", $originName, get_class($this))); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Column $column |
86
|
|
|
* @return int|string |
87
|
|
|
* @throws EntityException |
88
|
|
|
*/ |
89
|
|
|
public function getVarNameByColumn(Column $column): int|string |
90
|
|
|
{ |
91
|
|
|
foreach ($this->getOriginFieldNames() as $varName => $originFieldName) { |
92
|
|
|
if ($originFieldName == $column->name) { |
93
|
|
|
return $varName; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
throw new EntityException(sprintf("Seems column '%s' does not belong to this mapper", $column->name)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return Column[] that is associative array where key is property name |
102
|
|
|
*/ |
103
|
|
|
public function getPrimaryKey(): array { |
104
|
|
|
$keys = []; |
105
|
|
|
|
106
|
|
|
foreach ($this->getColumns() as $columnName => $column) { |
107
|
|
|
if (isset($column->{Column::KEY}) and $column->{Column::KEY} === true) { |
108
|
|
|
$keys[$columnName] = $column; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $keys; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return array<string, string> |
117
|
|
|
*/ |
118
|
|
|
public function getOriginFieldNames(): array |
119
|
|
|
{ |
120
|
|
|
$thisName = $this->name(); |
121
|
|
|
|
122
|
|
|
if (!isset(MapperCache::me()->originFieldNames[$thisName])) { |
123
|
|
|
MapperCache::me()->originFieldNames[$thisName] = []; |
124
|
|
|
|
125
|
|
|
foreach ($this->getColumns() as $columnName => $column) { |
126
|
|
|
MapperCache::me()->originFieldNames[$thisName][$columnName] = $column->name; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return MapperCache::me()->originFieldNames[$thisName]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return Table |
135
|
|
|
* @throws ReflectionException |
136
|
|
|
* @throws EntityException |
137
|
|
|
*/ |
138
|
|
|
public function getTable(): Table |
139
|
|
|
{ |
140
|
|
|
$thisName = $this->name(); |
141
|
|
|
|
142
|
|
|
if (!isset(MapperCache::me()->tables[$thisName])) { |
|
|
|
|
143
|
|
|
$table = new Table(); |
144
|
|
|
|
145
|
|
|
$table->name = $this->getTableName(); |
|
|
|
|
146
|
|
|
$table->scheme = $this->getScheme(); |
|
|
|
|
147
|
|
|
$table->columns = $this->getColumns(); |
148
|
|
|
$table->constraints = $this->getConstraints(); |
149
|
|
|
$table->keys = $this->getPrimaryKey(); |
150
|
|
|
$table->annotation = $this->getAnnotation(); |
|
|
|
|
151
|
|
|
|
152
|
|
|
MapperCache::me()->table[$thisName] = $table; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return MapperCache::me()->table[$thisName]; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|