Passed
Push — master ( ba7917...6c027d )
by Jean-Christophe
06:26
created

OrmUtilsFieldsTrait::getFormAllFields()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.5069

Importance

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