|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\traits; |
|
4
|
|
|
|
|
5
|
|
|
trait OrmUtilsFieldsTrait { |
|
6
|
|
|
abstract public static function getAnnotationInfo($class, $keyAnnotation); |
|
7
|
|
|
|
|
8
|
|
|
public static function getFieldTypes($className) { |
|
9
|
|
|
$fieldTypes=self::getAnnotationInfo($className, "#fieldTypes"); |
|
10
|
|
|
if ($fieldTypes !== false){ |
|
11
|
|
|
return $fieldTypes; |
|
12
|
|
|
} |
|
13
|
|
|
return [ ]; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public static function getFieldType($className,$field){ |
|
17
|
|
|
$types= self::getFieldTypes($className); |
|
18
|
|
|
if(isset($types[$field])){ |
|
19
|
|
|
return $types[$field]; |
|
20
|
|
|
} |
|
21
|
|
|
return "int"; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public static function getKeyFields($instance) { |
|
25
|
|
|
if(!\is_string($instance)){ |
|
26
|
|
|
$instance=\get_class($instance); |
|
27
|
|
|
} |
|
28
|
|
|
return self::getAnnotationInfo($instance, "#primaryKeys"); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public static function getFirstKey($class) { |
|
32
|
|
|
$kf=self::getAnnotationInfo($class, "#primaryKeys"); |
|
33
|
|
|
return \reset($kf); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function getAllFields($class){ |
|
37
|
|
|
return \array_keys(self::getAnnotationInfo($class, "#fieldNames")); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
public static function getFieldNames($model){ |
|
42
|
|
|
$fields=self::getAnnotationInfo($model, "#fieldNames"); |
|
43
|
|
|
$result=[]; |
|
44
|
|
|
$serializables=self::getSerializableFields($model); |
|
45
|
|
|
foreach ($fields as $member=>$field){ |
|
46
|
|
|
if(\array_search($member, $serializables)!==false) |
|
47
|
|
|
$result[$field]=$member; |
|
48
|
|
|
} |
|
49
|
|
|
return $result; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public static function getSerializableFields($class) { |
|
53
|
|
|
$notSerializable=self::getAnnotationInfo($class, "#notSerializable"); |
|
54
|
|
|
$fieldNames=\array_keys(self::getAnnotationInfo($class, "#fieldNames")); |
|
55
|
|
|
return \array_diff($fieldNames, $notSerializable); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function getFormAllFields($class){ |
|
59
|
|
|
$result=self::getSerializableFields($class); |
|
60
|
|
View Code Duplication |
if ($manyToOne=self::getAnnotationInfo($class, "#manyToOne")) { |
|
|
|
|
|
|
61
|
|
|
foreach ($manyToOne as $member){ |
|
62
|
|
|
$joinColumn = self::getAnnotationInfoMember ( $class, "#joinColumn", $member ); |
|
|
|
|
|
|
63
|
|
|
$result[]=$joinColumn["name"]; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
View Code Duplication |
if ($manyToMany=self::getAnnotationInfo($class, "#manyToMany")) { |
|
|
|
|
|
|
67
|
|
|
$manyToMany=array_keys($manyToMany); |
|
68
|
|
|
foreach ($manyToMany as $member){ |
|
69
|
|
|
$result[]=$member . "Ids"; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
return $result; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.