Passed
Push — master ( be71ba...d26f6c )
by Jean-Christophe
03:55
created

OrmUtilsFieldsTrait::getFirstKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 1
	public static function getFieldTypes($className) {
14 1
		$fieldTypes=self::getAnnotationInfo($className, "#fieldTypes");
15 1
		if ($fieldTypes !== false){
16 1
			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 2
	public static function getKeyFields($instance) {
30 2
		if(!\is_string($instance)){
31 1
			$instance=\get_class($instance);
32
		}
33 2
		return self::getAnnotationInfo($instance, "#primaryKeys");
34
	}
35
	
36 8
	public static function getFirstKey($class) {
37 8
		$kf=self::getAnnotationInfo($class, "#primaryKeys");
38 8
		return \current($kf);
39
	}
40
	
41 5
	public static function getFirstPropKey($class) {
42 5
		if(isset(self::$propFirstKeys[$class])){
43 5
			return self::$propFirstKeys[$class];
44
		}
45 3
		$prop=new \ReflectionProperty($class, current(self::getAnnotationInfo($class, "#primaryKeys")));
46 3
		$prop->setAccessible(true);
47 3
		return self::$propFirstKeys[$class]=$prop;
48
	}
49
	
50 8
	public static function getPropKeys($class){
51 8
		if(isset(self::$propKeys[$class])){
52 6
			return self::$propKeys[$class];
53
		}
54 4
		$result=[];
55 4
		$pkMembers=self::getAnnotationInfo($class, "#primaryKeys");
56 4
		foreach ($pkMembers as $member){
57 4
			$prop=new \ReflectionProperty($class, $member);
58 4
			$prop->setAccessible(true);
59 4
			$result[]=$prop;
60
		}
61 4
		return self::$propKeys[$class]=$result;
62
	}
63
	
64 8
	public static function getAccessors($class,$members){
65 8
		if(isset(self::$accessors[$class])){
66 6
			return self::$accessors[$class];
67
		}
68 4
		$result=[];
69 4
		foreach ($members as $member=>$fieldNotUsed){
70 4
			$accesseur="set" . ucfirst($member);
71 4
			if (method_exists($class, $accesseur)) {
72 4
				$result[$member]=$accesseur;
73
			}
74
		}
75 4
		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 1
	public static function getFormAllFields($class){
104 1
		$result=self::getSerializableFields($class);
105 1
		if ($manyToOne=self::getAnnotationInfo($class, "#manyToOne")) {
106 1
			foreach ($manyToOne as $member){
107 1
				$joinColumn = self::getAnnotationInfoMember ( $class, "#joinColumn", $member );
108 1
				$result[]=$joinColumn["name"];
109
			}
110
		}
111 1
		if ($manyToMany=self::getAnnotationInfo($class, "#manyToMany")) {
112
			$manyToMany=array_keys($manyToMany);
113
			foreach ($manyToMany as $member){
114
				$result[]=$member . "Ids";
115
			}
116
		}
117 1
		return $result;
118
	}
119
}
120
121