1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\orm; |
4
|
|
|
|
5
|
|
|
use Ubiquity\orm\parser\Reflexion; |
6
|
|
|
use Ubiquity\cache\CacheManager; |
7
|
|
|
use Ubiquity\controllers\rest\ResponseFormatter; |
8
|
|
|
use Ubiquity\orm\traits\OrmUtilsRelationsTrait; |
9
|
|
|
use Ubiquity\orm\traits\OrmUtilsFieldsTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Object/relational mapping utilities |
13
|
|
|
* |
14
|
|
|
* @author jc |
15
|
|
|
* @version 1.0.4 |
16
|
|
|
*/ |
17
|
|
|
class OrmUtils { |
18
|
|
|
|
19
|
|
|
use OrmUtilsFieldsTrait,OrmUtilsRelationsTrait; |
20
|
|
|
private static $modelsMetadatas; |
21
|
|
|
|
22
|
76 |
|
public static function getModelMetadata($className) { |
23
|
76 |
|
return self::$modelsMetadatas [$className] ?? (self::$modelsMetadatas [$className] = CacheManager::getOrmModelCache ( $className )); |
24
|
|
|
} |
25
|
|
|
|
26
|
13 |
|
public static function isSerializable($class, $member) { |
27
|
13 |
|
return ! self::_is ( $class, $member, '#notSerializable' ); |
28
|
|
|
} |
29
|
|
|
|
30
|
11 |
|
public static function isNullable($class, $member) { |
31
|
11 |
|
return self::_is ( $class, $member, '#nullable' ); |
32
|
|
|
} |
33
|
|
|
|
34
|
13 |
|
protected static function _is($class, $member, $like) { |
35
|
13 |
|
$ret = self::getAnnotationInfo ( $class, $like ); |
36
|
13 |
|
if ($ret !== false) { |
37
|
13 |
|
return \array_search ( $member, $ret ) !== false; |
38
|
|
|
} |
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
13 |
|
public static function getFieldName($class, $member) { |
43
|
13 |
|
$ret = self::getAnnotationInfo ( $class, '#fieldNames' ); |
44
|
13 |
|
return $ret [$member] ?? $member; |
45
|
|
|
} |
46
|
|
|
|
47
|
50 |
|
public static function getTableName($class) { |
48
|
50 |
|
return self::getModelMetadata ( $class ) ['#tableName']; |
49
|
|
|
} |
50
|
|
|
|
51
|
11 |
|
public static function getKeyFieldsAndValues($instance) { |
52
|
11 |
|
$class = \get_class ( $instance ); |
53
|
11 |
|
$kf = self::getAnnotationInfo ( $class, '#primaryKeys' ); |
54
|
11 |
|
return self::getFieldsAndValues_ ( $instance, $kf ); |
55
|
|
|
} |
56
|
|
|
|
57
|
12 |
|
public static function getFieldsAndValues_($instance, $members) { |
58
|
12 |
|
$ret = [ ]; |
59
|
12 |
|
foreach ( $members as $member ) { |
60
|
12 |
|
$v = Reflexion::getMemberValue ( $instance, $member ); |
61
|
12 |
|
$ret [$member] = $v; |
62
|
|
|
} |
63
|
12 |
|
return $ret; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
public static function getKeyPropsAndValues_($instance, $props) { |
67
|
1 |
|
$ret = [ ]; |
68
|
1 |
|
foreach ( $props as $prop ) { |
69
|
1 |
|
$v = Reflexion::getPropValue ( $instance, $prop ); |
70
|
1 |
|
$ret [$prop->getName ()] = $v; |
71
|
|
|
} |
72
|
1 |
|
return $ret; |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
public static function getMembers($className) { |
76
|
3 |
|
$fieldNames = self::getAnnotationInfo ( $className, '#fieldNames' ); |
77
|
3 |
|
if ($fieldNames !== false) |
78
|
3 |
|
return \array_keys ( $fieldNames ); |
79
|
|
|
return [ ]; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public static function getMembersAndValues($instance, $members = NULL) { |
83
|
1 |
|
$ret = array (); |
84
|
1 |
|
$className = \get_class ( $instance ); |
85
|
1 |
|
if (\is_null ( $members )) |
86
|
1 |
|
$members = self::getMembers ( $className ); |
87
|
1 |
|
foreach ( $members as $member ) { |
88
|
1 |
|
if (self::isSerializable ( $className, $member )) { |
89
|
1 |
|
$v = Reflexion::getMemberValue ( $instance, $member ); |
90
|
1 |
|
if (self::isNotNullOrNullAccepted ( $v, $className, $member )) { |
91
|
1 |
|
$name = self::getFieldName ( $className, $member ); |
92
|
1 |
|
$ret [$name] = $v; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
1 |
|
return $ret; |
97
|
|
|
} |
98
|
|
|
|
99
|
13 |
|
public static function isNotNullOrNullAccepted($v, $className, $member) { |
100
|
13 |
|
$notNull = (isset ( $v ) && NULL !== $v && '' !== $v); |
101
|
13 |
|
return ($notNull) || (! $notNull && self::isNullable ( $className, $member )); |
102
|
|
|
} |
103
|
|
|
|
104
|
42 |
|
public static function getFirstKeyValue($instance) { |
105
|
42 |
|
$prop = OrmUtils::getFirstPropKey ( \get_class ( $instance ) ); |
106
|
42 |
|
return Reflexion::getPropValue ( $instance, $prop ); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
public static function getFirstKeyValue_($instance, $members) { |
110
|
1 |
|
$fkv = self::getFieldsAndValues_ ( $instance, $members ); |
111
|
1 |
|
return \current ( $fkv ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public static function getKeyValues($instance) { |
115
|
|
|
$fkv = self::getKeyFieldsAndValues ( $instance ); |
116
|
|
|
return \implode ( '_', $fkv ); |
117
|
|
|
} |
118
|
|
|
|
119
|
56 |
|
public static function getPropKeyValues($instance, $props) { |
120
|
56 |
|
$values = [ ]; |
121
|
56 |
|
foreach ( $props as $prop ) { |
122
|
56 |
|
$values [] = Reflexion::getPropValue ( $instance, $prop ); |
123
|
|
|
} |
124
|
56 |
|
return \implode ( '_', $values ); |
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
|
|
|
* |
135
|
|
|
* @param object $instance |
136
|
|
|
* @param string $memberKey |
137
|
|
|
* @param array $array |
138
|
|
|
* @return boolean |
139
|
|
|
*/ |
140
|
1 |
|
public static function exists($instance, $memberKey, $array) { |
141
|
1 |
|
$accessor = 'get' . \ucfirst ( $memberKey ); |
142
|
1 |
|
if (\method_exists ( $instance, $accessor )) { |
143
|
1 |
|
foreach ( $array as $value ) { |
144
|
1 |
|
if ($value->$accessor () == $instance->$accessor ()) |
145
|
1 |
|
return true; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
|
151
|
64 |
|
public static function getAnnotationInfo($class, $keyAnnotation) { |
152
|
64 |
|
return self::getModelMetadata ( $class ) [$keyAnnotation] ?? false; |
153
|
|
|
} |
154
|
|
|
|
155
|
46 |
|
public static function getAnnotationInfoMember($class, $keyAnnotation, $member) { |
156
|
46 |
|
$info = self::getAnnotationInfo ( $class, $keyAnnotation ); |
157
|
46 |
|
if ($info !== false) { |
158
|
46 |
|
if (! isset ( $info [0] )) { // isAssociative |
159
|
46 |
|
if (isset ( $info [$member] )) { |
160
|
46 |
|
return $info [$member]; |
161
|
|
|
} |
162
|
|
|
} else { |
163
|
1 |
|
if (\array_search ( $member, $info ) !== false) { |
164
|
1 |
|
return $member; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
4 |
|
public static function setFieldToMemberNames(&$fields, $relFields) { |
172
|
4 |
|
foreach ( $fields as $index => $field ) { |
173
|
4 |
|
if (isset ( $relFields [$field] )) { |
174
|
1 |
|
$fields [$index] = $relFields [$field]; |
175
|
|
|
} |
176
|
|
|
} |
177
|
4 |
|
} |
178
|
|
|
|
179
|
1 |
|
public static function objectAsJSON($instance) { |
180
|
1 |
|
$formatter = new ResponseFormatter (); |
181
|
1 |
|
$datas = $formatter->cleanRestObject ( $instance ); |
182
|
1 |
|
return $formatter->format ( [ 'pk' => self::getFirstKeyValue ( $instance ),'object' => $datas ] ); |
183
|
|
|
} |
184
|
|
|
|
185
|
1 |
|
public static function getTransformers($class) { |
186
|
1 |
|
if (isset ( self::getModelMetadata ( $class ) ['#transformers'] )) |
187
|
1 |
|
return self::getModelMetadata ( $class ) ['#transformers']; |
188
|
|
|
} |
189
|
|
|
|
190
|
1 |
|
public static function getAccessors($class) { |
191
|
1 |
|
if (isset ( self::getModelMetadata ( $class ) ['#accessors'] )) |
192
|
1 |
|
return self::getModelMetadata ( $class ) ['#accessors']; |
193
|
|
|
} |
194
|
|
|
|
195
|
4 |
|
public static function clearMetaDatas() { |
196
|
4 |
|
self::$modelsMetadatas = [ ]; |
197
|
4 |
|
} |
198
|
|
|
} |
199
|
|
|
|