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