|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\traits; |
|
4
|
|
|
|
|
5
|
|
|
trait OrmUtilsFieldsTrait { |
|
6
|
|
|
abstract public static function getAnnotationInfo($class, $keyAnnotation); |
|
7
|
|
|
abstract public static function getAnnotationInfoMember($class, $keyAnnotation, $member); |
|
8
|
|
|
protected static $fieldNames=[]; |
|
9
|
|
|
protected static $propFirstKeys=[]; |
|
10
|
|
|
protected static $propKeys=[]; |
|
11
|
|
|
protected static $accessors=[]; |
|
12
|
|
|
|
|
13
|
|
|
public static function getFieldTypes($className) { |
|
14
|
|
|
$fieldTypes=self::getAnnotationInfo($className, "#fieldTypes"); |
|
15
|
|
|
if ($fieldTypes !== false){ |
|
16
|
|
|
return $fieldTypes; |
|
17
|
|
|
} |
|
18
|
|
|
return [ ]; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public static function getFieldType($className,$field){ |
|
22
|
|
|
$types= self::getFieldTypes($className); |
|
23
|
|
|
if(isset($types[$field])){ |
|
24
|
|
|
return $types[$field]; |
|
25
|
|
|
} |
|
26
|
|
|
return "int"; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
1 |
|
public static function getKeyFields($instance) { |
|
30
|
1 |
|
if(!\is_string($instance)){ |
|
31
|
1 |
|
$instance=\get_class($instance); |
|
32
|
|
|
} |
|
33
|
1 |
|
return self::getAnnotationInfo($instance, "#primaryKeys"); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function getFirstKey($class) { |
|
37
|
|
|
$kf=self::getAnnotationInfo($class, "#primaryKeys"); |
|
38
|
|
|
return \current($kf); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public static function getFirstPropKey($class) { |
|
42
|
|
|
if(isset(self::$propFirstKeys[$class])){ |
|
43
|
|
|
return self::$propFirstKeys[$class]; |
|
44
|
|
|
} |
|
45
|
|
|
$prop=new \ReflectionProperty($class, current(self::getAnnotationInfo($class, "#primaryKeys"))); |
|
46
|
|
|
$prop->setAccessible(true); |
|
47
|
|
|
return self::$propFirstKeys[$class]=$prop; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public static function getPropKeys($class){ |
|
51
|
1 |
|
if(isset(self::$propKeys[$class])){ |
|
52
|
|
|
return self::$propKeys[$class]; |
|
53
|
|
|
} |
|
54
|
1 |
|
$result=[]; |
|
55
|
1 |
|
$pkMembers=self::getAnnotationInfo($class, "#primaryKeys"); |
|
56
|
1 |
|
foreach ($pkMembers as $member){ |
|
57
|
1 |
|
$prop=new \ReflectionProperty($class, $member); |
|
58
|
1 |
|
$prop->setAccessible(true); |
|
59
|
1 |
|
$result[]=$prop; |
|
60
|
|
|
} |
|
61
|
1 |
|
return self::$propKeys[$class]=$result; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
public static function getAccessors($class,$members){ |
|
65
|
1 |
|
if(isset(self::$accessors[$class])){ |
|
66
|
|
|
return self::$accessors[$class]; |
|
67
|
|
|
} |
|
68
|
1 |
|
$result=[]; |
|
69
|
1 |
|
foreach ($members as $member=>$fieldNotUsed){ |
|
70
|
1 |
|
$accesseur="set" . ucfirst($member); |
|
71
|
1 |
|
if (method_exists($class, $accesseur)) { |
|
72
|
1 |
|
$result[$member]=$accesseur; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
1 |
|
return self::$accessors[$class]=$result; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public static function getAllFields($class){ |
|
79
|
|
|
return \array_keys(self::getAnnotationInfo($class, "#fieldNames")); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
public static function getFieldNames($model){ |
|
84
|
|
|
if(isset(self::$fieldNames[$model])){ |
|
85
|
|
|
return self::$fieldNames[$model]; |
|
86
|
|
|
} |
|
87
|
|
|
$fields=self::getAnnotationInfo($model, "#fieldNames"); |
|
88
|
|
|
$result=[]; |
|
89
|
|
|
$serializables=self::getSerializableFields($model); |
|
90
|
|
|
foreach ($fields as $member=>$field){ |
|
91
|
|
|
if(\array_search($member, $serializables)!==false) |
|
92
|
|
|
$result[$field]=$member; |
|
93
|
|
|
} |
|
94
|
|
|
return self::$fieldNames[$model]=$result; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
public static function getSerializableFields($class) { |
|
98
|
1 |
|
$notSerializable=self::getAnnotationInfo($class, "#notSerializable"); |
|
99
|
1 |
|
$fieldNames=\array_keys(self::getAnnotationInfo($class, "#fieldNames")); |
|
100
|
1 |
|
return \array_diff($fieldNames, $notSerializable); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public static function getFormAllFields($class){ |
|
104
|
|
|
$result=self::getSerializableFields($class); |
|
105
|
|
|
if ($manyToOne=self::getAnnotationInfo($class, "#manyToOne")) { |
|
106
|
|
|
foreach ($manyToOne as $member){ |
|
107
|
|
|
$joinColumn = self::getAnnotationInfoMember ( $class, "#joinColumn", $member ); |
|
108
|
|
|
$result[]=$joinColumn["name"]; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
if ($manyToMany=self::getAnnotationInfo($class, "#manyToMany")) { |
|
112
|
|
|
$manyToMany=array_keys($manyToMany); |
|
113
|
|
|
foreach ($manyToMany as $member){ |
|
114
|
|
|
$result[]=$member . "Ids"; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
return $result; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
|