|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace micro\orm; |
|
4
|
|
|
|
|
5
|
|
|
use micro\orm\parser\Reflexion; |
|
6
|
|
|
use micro\cache\CacheManager; |
|
7
|
|
|
use micro\utils\StrUtils; |
|
8
|
|
|
use micro\utils\JArray; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Utilitaires de mappage Objet/relationnel |
|
12
|
|
|
* @author jc |
|
13
|
|
|
* @version 1.0.0.5 |
|
14
|
|
|
*/ |
|
15
|
|
|
class OrmUtils { |
|
16
|
|
|
private static $modelsMetadatas; |
|
17
|
|
|
|
|
18
|
|
|
public static function getModelMetadata($className) { |
|
19
|
|
|
if (!isset(self::$modelsMetadatas[$className])) { |
|
20
|
|
|
self::$modelsMetadatas[$className]=CacheManager::createOrmModelCache($className); |
|
21
|
|
|
} |
|
22
|
|
|
return self::$modelsMetadatas[$className]; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
View Code Duplication |
public static function isSerializable($class, $member) { |
|
26
|
|
|
$ret=self::getAnnotationInfo($class, "#notSerializable"); |
|
27
|
|
|
if ($ret !== false) |
|
28
|
|
|
return \array_search($member, $ret) === false; |
|
29
|
|
|
else |
|
30
|
|
|
return true; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
View Code Duplication |
public static function isNullable($class, $member) { |
|
34
|
|
|
$ret=self::getAnnotationInfo($class, "#nullable"); |
|
35
|
|
|
if ($ret !== false) |
|
36
|
|
|
return \array_search($member, $ret) !== false; |
|
37
|
|
|
else |
|
38
|
|
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
View Code Duplication |
public static function getFieldName($class, $member) { |
|
42
|
|
|
$ret=self::getAnnotationInfo($class, "#fieldNames"); |
|
43
|
|
|
if ($ret === false) |
|
44
|
|
|
$ret=$member; |
|
45
|
|
|
else |
|
46
|
|
|
$ret=$ret[$member]; |
|
47
|
|
|
return $ret; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public static function getTableName($class) { |
|
51
|
|
|
return self::getModelMetadata($class)["#tableName"]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public static function getKeyFieldsAndValues($instance) { |
|
55
|
|
|
$kf=self::getAnnotationInfo(get_class($instance), "#primaryKeys"); |
|
56
|
|
|
return self::getMembersAndValues($instance, $kf); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public static function getKeyFields($instance) { |
|
60
|
|
|
if(!\is_string($instance)){ |
|
61
|
|
|
$instance=\get_class($instance); |
|
62
|
|
|
} |
|
63
|
|
|
return self::getAnnotationInfo($instance, "#primaryKeys"); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public static function getMembers($className) { |
|
67
|
|
|
$fieldNames=self::getAnnotationInfo($className, "#fieldNames"); |
|
68
|
|
|
if ($fieldNames !== false) |
|
69
|
|
|
return \array_keys($fieldNames); |
|
70
|
|
|
return [ ]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public static function getFieldTypes($className) { |
|
74
|
|
|
$fieldTypes=self::getAnnotationInfo($className, "#fieldTypes"); |
|
75
|
|
|
if ($fieldTypes !== false) |
|
76
|
|
|
return $fieldTypes; |
|
77
|
|
|
return [ ]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public static function getMembersAndValues($instance, $members=NULL) { |
|
81
|
|
|
$ret=array (); |
|
82
|
|
|
$className=get_class($instance); |
|
83
|
|
|
if (is_null($members)) |
|
84
|
|
|
$members=self::getMembers($className); |
|
85
|
|
|
foreach ( $members as $member ) { |
|
86
|
|
|
if (OrmUtils::isSerializable($className, $member)) { |
|
87
|
|
|
$v=Reflexion::getMemberValue($instance, $member); |
|
88
|
|
|
if (self::isNotNullOrNullAccepted($v, $className, $member)) { |
|
89
|
|
|
$name=self::getFieldName($className, $member); |
|
90
|
|
|
$ret[$name]=$v; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
return $ret; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public static function isNotNullOrNullAccepted($v, $className, $member) { |
|
98
|
|
|
$notNull=StrUtils::isNotNull($v); |
|
99
|
|
|
return ($notNull) || (!$notNull && OrmUtils::isNullable($className, $member)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public static function getFirstKey($class) { |
|
103
|
|
|
$kf=self::getAnnotationInfo($class, "#primaryKeys"); |
|
104
|
|
|
return \reset($kf); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public static function getFirstKeyValue($instance) { |
|
108
|
|
|
$fkv=self::getKeyFieldsAndValues($instance); |
|
109
|
|
|
return \reset($fkv); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* |
|
114
|
|
|
* @param object $instance |
|
115
|
|
|
* @return mixed[] |
|
116
|
|
|
*/ |
|
117
|
|
|
public static function getManyToOneMembersAndValues($instance) { |
|
118
|
|
|
$ret=array (); |
|
119
|
|
|
$class=get_class($instance); |
|
120
|
|
|
$members=self::getAnnotationInfo($class, "#manyToOne"); |
|
121
|
|
|
if ($members !== false) { |
|
122
|
|
|
foreach ( $members as $member ) { |
|
123
|
|
|
$memberAccessor="get" . ucfirst($member); |
|
124
|
|
|
if (method_exists($instance, $memberAccessor)) { |
|
125
|
|
|
$memberInstance=$instance->$memberAccessor(); |
|
126
|
|
|
if (isset($memberInstance)) { |
|
127
|
|
|
$keyValues=self::getKeyFieldsAndValues($memberInstance); |
|
128
|
|
|
if (sizeof($keyValues) > 0) { |
|
129
|
|
|
$fkName=self::getJoinColumnName($class, $member); |
|
130
|
|
|
$ret[$fkName]=reset($keyValues); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
return $ret; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public static function getMembersWithAnnotation($class, $annotation) { |
|
140
|
|
|
if (isset(self::getModelMetadata($class)[$annotation])) |
|
141
|
|
|
return self::getModelMetadata($class)[$annotation]; |
|
142
|
|
|
return [ ]; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* |
|
147
|
|
|
* @param object $instance |
|
148
|
|
|
* @param string $memberKey |
|
149
|
|
|
* @param array $array |
|
150
|
|
|
* @return boolean |
|
151
|
|
|
*/ |
|
152
|
|
|
public static function exists($instance, $memberKey, $array) { |
|
153
|
|
|
$accessor="get" . ucfirst($memberKey); |
|
154
|
|
|
if (method_exists($instance, $accessor)) { |
|
155
|
|
|
if ($array !== null) { |
|
156
|
|
|
foreach ( $array as $value ) { |
|
157
|
|
|
if ($value->$accessor() == $instance->$accessor()) |
|
158
|
|
|
return true; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
return false; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public static function getJoinColumnName($class, $member) { |
|
166
|
|
|
$annot=self::getAnnotationInfoMember($class, "#joinColumn", $member); |
|
167
|
|
|
if ($annot !== false) { |
|
168
|
|
|
$fkName=$annot["name"]; |
|
169
|
|
|
} else { |
|
170
|
|
|
$fkName="id" . ucfirst(self::getTableName(ucfirst($member))); |
|
171
|
|
|
} |
|
172
|
|
|
return $fkName; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public static function getAnnotationInfo($class, $keyAnnotation) { |
|
176
|
|
|
if (isset(self::getModelMetadata($class)[$keyAnnotation])) |
|
177
|
|
|
return self::getModelMetadata($class)[$keyAnnotation]; |
|
178
|
|
|
return false; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public static function getAnnotationInfoMember($class, $keyAnnotation, $member) { |
|
182
|
|
|
$info=self::getAnnotationInfo($class, $keyAnnotation); |
|
183
|
|
|
if ($info !== false) { |
|
184
|
|
|
if(JArray::isAssociative($info)){ |
|
185
|
|
|
if (isset($info[$member])) { |
|
186
|
|
|
return $info[$member]; |
|
187
|
|
|
} |
|
188
|
|
|
}else{ |
|
189
|
|
|
if(\array_search($member, $info)!==false){ |
|
190
|
|
|
return $member; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
return false; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public static function getSerializableFields($class) { |
|
198
|
|
|
$notSerializable=self::getAnnotationInfo($class, "#notSerializable"); |
|
199
|
|
|
$fieldNames=\array_keys(self::getAnnotationInfo($class, "#fieldNames")); |
|
200
|
|
|
return \array_diff($fieldNames, $notSerializable); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public static function getFieldsInRelations($class) { |
|
204
|
|
|
$result=[ ]; |
|
205
|
|
|
if ($manyToOne=self::getAnnotationInfo($class, "#manyToOne")) { |
|
206
|
|
|
$result=\array_merge($result, $manyToOne); |
|
207
|
|
|
} |
|
208
|
|
|
if ($oneToMany=self::getAnnotationInfo($class, "#oneToMany")) { |
|
209
|
|
|
$result=\array_merge($result, \array_keys($oneToMany)); |
|
210
|
|
|
} |
|
211
|
|
|
if ($manyToMany=self::getAnnotationInfo($class, "#manyToMany")) { |
|
212
|
|
|
$result=\array_merge($result, \array_keys($manyToMany)); |
|
213
|
|
|
} |
|
214
|
|
|
return $result; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
public static function getDefaultFk($classname) { |
|
218
|
|
|
return "id" . \ucfirst(self::getTableName($classname)); |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|