Passed
Push — master ( 61e57b...69ef8d )
by Jean-Christophe
04:35
created

OrmUtilsFieldsTrait::getPropKeys()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 12
ccs 10
cts 10
cp 1
crap 3
rs 9.9666
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 4
	public static function getFieldTypes($className) {
14 4
		$fieldTypes=self::getAnnotationInfo($className, "#fieldTypes");
15 4
		if ($fieldTypes !== false){
16 4
			return $fieldTypes;
17
		}
18
		return [ ];
19
	}
20
	
21 1
	public static function getFieldType($className,$field){
22 1
		$types= self::getFieldTypes($className);
23 1
		if(isset($types[$field])){
24 1
			return $types[$field];
25
		}
26
		return "int";
27
	}
28
	
29 4
	public static function getKeyFields($instance) {
30 4
		if(!\is_string($instance)){
31 3
			$instance=\get_class($instance);
32
		}
33 4
		return self::getAnnotationInfo($instance, "#primaryKeys");
34
	}
35
	
36 11
	public static function getFirstKey($class) {
37 11
		$kf=self::getAnnotationInfo($class, "#primaryKeys");
38 11
		return \current($kf);
39
	}
40
	
41 9
	public static function getFirstPropKey($class) {
42 9
		if(isset(self::$propFirstKeys[$class])){
43 9
			return self::$propFirstKeys[$class];
44
		}
45 6
		$prop=new \ReflectionProperty($class, current(self::getAnnotationInfo($class, "#primaryKeys")));
46 6
		$prop->setAccessible(true);
47 6
		return self::$propFirstKeys[$class]=$prop;
48
	}
49
	
50 14
	public static function getPropKeys($class){
51 14
		if(isset(self::$propKeys[$class])){
52 11
			return self::$propKeys[$class];
53
		}
54 8
		$result=[];
55 8
		$pkMembers=self::getAnnotationInfo($class, "#primaryKeys");
56 8
		foreach ($pkMembers as $member){
57 8
			$prop=new \ReflectionProperty($class, $member);
58 8
			$prop->setAccessible(true);
59 8
			$result[]=$prop;
60
		}
61 8
		return self::$propKeys[$class]=$result;
62
	}
63
	
64 14
	public static function getAccessors($class,$members){
65 14
		if(isset(self::$accessors[$class])){
66 11
			return self::$accessors[$class];
67
		}
68 8
		$result=[];
69 8
		foreach ($members as $member=>$fieldNotUsed){
70 8
			$accesseur="set" . ucfirst($member);
71 8
			if (method_exists($class, $accesseur)) {
72 8
				$result[$member]=$accesseur;
73
			}
74
		}
75 8
		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 3
	public static function getSerializableFields($class) {
98 3
		$notSerializable=self::getAnnotationInfo($class, "#notSerializable");
99 3
		$fieldNames=\array_keys(self::getAnnotationInfo($class, "#fieldNames"));
100 3
		return \array_diff($fieldNames, $notSerializable);
101
	}
102
	
103 3
	public static function getFormAllFields($class){
104 3
		$result=self::getSerializableFields($class);
105 3
		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 3
		if ($manyToMany=self::getAnnotationInfo($class, "#manyToMany")) {
112
			$manyToMany=array_keys($manyToMany);
113
			foreach ($manyToMany as $member){
114
				$result[]=$member . "Ids";
115
			}
116
		}
117 3
		return $result;
118
	}
119
}
120
121