Passed
Push — master ( b97787...c0f601 )
by Jean-Christophe
16:10
created

OrmUtilsFieldsTrait   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Test Coverage

Coverage 57.83%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 34
eloc 70
c 2
b 0
f 1
dl 0
loc 155
ccs 48
cts 83
cp 0.5783
rs 9.68

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldTypes() 0 6 2
A getKeyFields() 0 5 2
A getKeyMembers() 0 8 3
A getFieldType() 0 6 2
A getSerializableFields() 0 4 1
A getSerializableMembers() 0 4 1
A getFieldNames() 0 12 4
A getFormAllFields() 0 15 5
A getAllFields() 0 2 1
A getFirstPropKey() 0 7 2
A getFirstKey() 0 6 2
A getNullableFields() 0 2 1
A getPropKeys() 0 12 3
A getAccessors() 0 12 5
1
<?php
2
3
namespace Ubiquity\orm\traits;
4
5
/**
6
 * Ubiquity\orm\traits$OrmUtilsFieldsTrait
7
 * This class is part of Ubiquity
8
 *
9
 * @author jc
10
 * @version 1.0.2
11
 *
12
 */
13
trait OrmUtilsFieldsTrait {
14
15
	abstract public static function getAnnotationInfo($class, $keyAnnotation);
16
17
	abstract public static function getAnnotationInfoMember($class, $keyAnnotation, $member);
18
	protected static $fieldNames = [ ];
19
	protected static $propFirstKeys = [ ];
20
	protected static $propKeys = [ ];
21
	protected static $accessors = [ ];
22
23 8
	public static function getFieldTypes($className) {
24 8
		$fieldTypes = self::getAnnotationInfo ( $className, '#fieldTypes' );
25 8
		if ($fieldTypes !== false) {
26 8
			return $fieldTypes;
27
		}
28
		return [ ];
29
	}
30
31
	public static function getFieldType($className, $field) {
32
		$types = self::getFieldTypes ( $className );
33
		if (isset ( $types [$field] )) {
34
			return $types [$field];
35
		}
36
		return 'int';
37
	}
38
39
	/**
40
	 * Return primary key fields from instance or model class
41
	 *
42
	 * @param string|object $instance
43
	 * @return array|boolean
44
	 */
45 73
	public static function getKeyFields($instance) {
46 73
		if (! \is_string ( $instance )) {
47
			$instance = \get_class ( $instance );
48
		}
49 73
		return self::getAnnotationInfo ( $instance, '#primaryKeys' );
50
	}
51
52
	/**
53
	 * Return primary key members from instance or model class
54
	 *
55
	 * @param string|object $instance
56
	 * @return array
57
	 */
58 31
	public static function getKeyMembers($instance) {
59 31
		if (! \is_string ( $instance )) {
60
			$instance = \get_class ( $instance );
61
		}
62 31
		if($info=self::getAnnotationInfo ( $instance, '#primaryKeys' )){
63 31
			return \array_keys ( $info );
64
		}
65 2
		return [];
66
		
67
	}
68
69 82
	public static function getFirstKey($class) {
70 82
		$kf = self::getAnnotationInfo ( $class, '#primaryKeys' );
71 82
		if($kf){
72 82
			return \current ( $kf );
73
		}
74
		return '';
75
	}
76
77
	/**
78
	 *
79
	 * @param string $class
80
	 * @return \ReflectionProperty
81
	 */
82 129
	public static function getFirstPropKey($class) {
83 129
		if (isset ( self::$propFirstKeys [$class] )) {
84 126
			return self::$propFirstKeys [$class];
85
		}
86 19
		$prop = new \ReflectionProperty ( $class, \array_key_first ( self::getAnnotationInfo ( $class, '#primaryKeys' ) ) );
87 19
		$prop->setAccessible ( true );
88 19
		return self::$propFirstKeys [$class] = $prop;
89
	}
90
91 140
	public static function getPropKeys($class) {
92 140
		if (isset ( self::$propKeys [$class] )) {
93 127
			return self::$propKeys [$class];
94
		}
95 22
		$result = [ ];
96 22
		$pkMembers = self::getAnnotationInfo ( $class, '#primaryKeys' );
97 22
		foreach ( $pkMembers as $member => $_field ) {
98 22
			$prop = new \ReflectionProperty ( $class, $member );
99 22
			$prop->setAccessible ( true );
100 22
			$result [] = $prop;
101
		}
102 22
		return self::$propKeys [$class] = $result;
103
	}
104
105
	public static function getAccessors($class, $members) {
106
		if (isset ( self::$accessors [$class] )) {
107
			return self::$accessors [$class];
108
		}
109
		$result = [ ];
110
		foreach ( $members as $member => $field ) {
111
			$accesseur = 'set' . \ucfirst ( $member );
112
			if (! isset ( $result [$field] ) && method_exists ( $class, $accesseur )) {
113
				$result [$field] = $accesseur;
114
			}
115
		}
116
		return self::$accessors [$class] = $result;
117
	}
118
119
	public static function getAllFields($class) {
120
		return \array_keys ( self::getAnnotationInfo ( $class, '#fieldNames' ) );
121
	}
122
123
	public static function getFieldNames($model) {
124
		if (isset ( self::$fieldNames [$model] )) {
125
			return self::$fieldNames [$model];
126
		}
127
		$fields = self::getAnnotationInfo ( $model, '#fieldNames' );
128
		$result = [ ];
129
		$serializables = self::getSerializableFields ( $model );
130
		foreach ( $fields as $member => $field ) {
131
			if (\array_search ( $member, $serializables ) !== false)
132
				$result [$field] = $member;
133
		}
134
		return self::$fieldNames [$model] = $result;
135
	}
136
137 5
	public static function getSerializableFields($class) {
138 5
		$notSerializable = self::getAnnotationInfo ( $class, '#notSerializable' );
139 5
		$fieldNames = \array_values ( self::getAnnotationInfo ( $class, '#fieldNames' ) );
140 5
		return \array_diff ( $fieldNames, $notSerializable );
141
	}
142
143
	public static function getNullableFields($class) {
144
		return self::getAnnotationInfo ( $class, '#nullable' );
145
	}
146
147 8
	public static function getSerializableMembers($class) {
148 8
		$notSerializable = self::getAnnotationInfo ( $class, '#notSerializable' );
149 8
		$memberNames = \array_keys ( self::getAnnotationInfo ( $class, '#fieldNames' ) );
150 8
		return \array_diff ( $memberNames, $notSerializable );
151
	}
152
153 3
	public static function getFormAllFields($class) {
154 3
		$result = self::getSerializableMembers ( $class );
155 3
		if ($manyToOne = self::getAnnotationInfo ( $class, '#manyToOne' )) {
156 1
			foreach ( $manyToOne as $member ) {
157 1
				$joinColumn = self::getAnnotationInfoMember ( $class, '#joinColumn', $member );
158 1
				$result [] = $joinColumn ['name'];
159
			}
160
		}
161 3
		if ($manyToMany = self::getAnnotationInfo ( $class, '#manyToMany' )) {
162
			$manyToMany = \array_keys ( $manyToMany );
163
			foreach ( $manyToMany as $member ) {
164
				$result [] = $member . 'Ids';
165
			}
166
		}
167 3
		return $result;
168
	}
169
}
170
171