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