Passed
Push — master ( 80b376...d6ee6e )
by Jean-Christophe
09:45
created

OrmUtils::getTransformers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Ubiquity\orm;
4
5
use Ubiquity\orm\parser\Reflexion;
6
use Ubiquity\cache\CacheManager;
7
use Ubiquity\utils\base\UString;
8
use Ubiquity\utils\base\UArray;
9
use Ubiquity\controllers\rest\ResponseFormatter;
10
use Ubiquity\orm\traits\OrmUtilsRelationsTrait;
11
use Ubiquity\orm\traits\OrmUtilsFieldsTrait;
12
13
/**
14
 * Object/relational mapping utilities
15
 *
16
 * @author jc
17
 * @version 1.0.3
18
 */
19
class OrmUtils {
20
21
	use OrmUtilsFieldsTrait,OrmUtilsRelationsTrait;
22
	private static $modelsMetadatas;
23
24 76
	public static function getModelMetadata($className) {
25 76
		if (! isset ( self::$modelsMetadatas [$className] )) {
26 27
			self::$modelsMetadatas [$className] = CacheManager::getOrmModelCache ( $className );
27
		}
28 76
		return self::$modelsMetadatas [$className];
29
	}
30
31 13
	public static function isSerializable($class, $member) {
32 13
		return ! self::_is ( $class, $member, "#notSerializable" );
33
	}
34
35 11
	public static function isNullable($class, $member) {
36 11
		return self::_is ( $class, $member, "#nullable" );
37
	}
38
39 13
	protected static function _is($class, $member, $like) {
40 13
		$ret = self::getAnnotationInfo ( $class, $like );
41 13
		if ($ret !== false) {
42 13
			return \array_search ( $member, $ret ) !== false;
43
		}
44
		return false;
45
	}
46
47 13
	public static function getFieldName($class, $member) {
48 13
		$ret = self::getAnnotationInfo ( $class, "#fieldNames" );
49 13
		if ($ret === false || ! isset ( $ret [$member] )) {
50
			return $member;
51
		}
52 13
		return $ret [$member];
53
	}
54
55 51
	public static function getTableName($class) {
56 51
		if (isset ( self::getModelMetadata ( $class ) ["#tableName"] ))
57 51
			return self::getModelMetadata ( $class ) ["#tableName"];
58
	}
59
60 11
	public static function getKeyFieldsAndValues($instance) {
61 11
		$class = get_class ( $instance );
62 11
		$kf = self::getAnnotationInfo ( $class, "#primaryKeys" );
63 11
		return self::getFieldsAndValues_ ( $instance, $kf );
64
	}
65
66 12
	public static function getFieldsAndValues_($instance, $members) {
67 12
		$ret = [ ];
68 12
		foreach ( $members as $member ) {
69 12
			$v = Reflexion::getMemberValue ( $instance, $member );
70 12
			$ret [$member] = $v;
71
		}
72 12
		return $ret;
73
	}
74
75 1
	public static function getKeyPropsAndValues_($instance, $props) {
76 1
		$ret = [ ];
77 1
		foreach ( $props as $prop ) {
78 1
			$v = Reflexion::getPropValue ( $instance, $prop );
79 1
			$ret [$prop->getName ()] = $v;
80
		}
81 1
		return $ret;
82
	}
83
84 3
	public static function getMembers($className) {
85 3
		$fieldNames = self::getAnnotationInfo ( $className, "#fieldNames" );
86 3
		if ($fieldNames !== false)
87 3
			return \array_keys ( $fieldNames );
88
		return [ ];
89
	}
90
91 1
	public static function getMembersAndValues($instance, $members = NULL) {
92 1
		$ret = array ();
93 1
		$className = get_class ( $instance );
94 1
		if (is_null ( $members ))
95 1
			$members = self::getMembers ( $className );
96 1
		foreach ( $members as $member ) {
97 1
			if (self::isSerializable ( $className, $member )) {
98 1
				$v = Reflexion::getMemberValue ( $instance, $member );
99 1
				if (self::isNotNullOrNullAccepted ( $v, $className, $member )) {
100 1
					$name = self::getFieldName ( $className, $member );
101 1
					$ret [$name] = $v;
102
				}
103
			}
104
		}
105 1
		return $ret;
106
	}
107
108 13
	public static function isNotNullOrNullAccepted($v, $className, $member) {
109 13
		$notNull = UString::isNotNull ( $v );
110 13
		return ($notNull) || (! $notNull && self::isNullable ( $className, $member ));
111
	}
112
113 42
	public static function getFirstKeyValue($instance) {
114 42
		$prop = OrmUtils::getFirstPropKey ( get_class ( $instance ) );
115 42
		return Reflexion::getPropValue ( $instance, $prop );
116
	}
117
118 1
	public static function getFirstKeyValue_($instance, $members) {
119 1
		$fkv = self::getFieldsAndValues_ ( $instance, $members );
120 1
		return \current ( $fkv );
121
	}
122
123
	public static function getKeyValues($instance) {
124
		$fkv = self::getKeyFieldsAndValues ( $instance );
125
		return implode ( "_", $fkv );
126
	}
127
128 56
	public static function getPropKeyValues($instance, $props) {
129 56
		$values = [ ];
130 56
		foreach ( $props as $prop ) {
131 56
			$values [] = Reflexion::getPropValue ( $instance, $prop );
132
		}
133 56
		return implode ( "_", $values );
134
	}
135
136
	public static function getMembersWithAnnotation($class, $annotation) {
137
		if (isset ( self::getModelMetadata ( $class ) [$annotation] ))
138
			return self::getModelMetadata ( $class ) [$annotation];
139
		return [ ];
140
	}
141
142
	/**
143
	 *
144
	 * @param object $instance
145
	 * @param string $memberKey
146
	 * @param array $array
147
	 * @return boolean
148
	 */
149 1
	public static function exists($instance, $memberKey, $array) {
150 1
		$accessor = "get" . ucfirst ( $memberKey );
151 1
		if (method_exists ( $instance, $accessor )) {
152 1
			foreach ( $array as $value ) {
153 1
				if ($value->$accessor () == $instance->$accessor ())
154 1
					return true;
155
			}
156
		}
157
		return false;
158
	}
159
160 63
	public static function getAnnotationInfo($class, $keyAnnotation) {
161 63
		$metas = self::getModelMetadata ( $class );
162 63
		if (isset ( $metas [$keyAnnotation] ))
163 63
			return $metas [$keyAnnotation];
164 12
		return false;
165
	}
166
167 46
	public static function getAnnotationInfoMember($class, $keyAnnotation, $member) {
168 46
		$info = self::getAnnotationInfo ( $class, $keyAnnotation );
169 46
		if ($info !== false) {
170 46
			if (UArray::isAssociative ( $info )) {
171 46
				if (isset ( $info [$member] )) {
172 46
					return $info [$member];
173
				}
174
			} else {
175 1
				if (\array_search ( $member, $info ) !== false) {
176 1
					return $member;
177
				}
178
			}
179
		}
180
		return false;
181
	}
182
183 4
	public static function setFieldToMemberNames(&$fields, $relFields) {
184 4
		foreach ( $fields as $index => $field ) {
185 4
			if (isset ( $relFields [$field] )) {
186 1
				$fields [$index] = $relFields [$field];
187
			}
188
		}
189 4
	}
190
191 1
	public static function objectAsJSON($instance) {
192 1
		$formatter = new ResponseFormatter ();
193 1
		$datas = $formatter->cleanRestObject ( $instance );
194 1
		return $formatter->format ( [ "pk" => self::getFirstKeyValue ( $instance ),"object" => $datas ] );
195
	}
196
197 1
	public static function getTransformers($class) {
198 1
		if (isset ( self::getModelMetadata ( $class ) ["#transformers"] ))
199 1
			return self::getModelMetadata ( $class ) ["#transformers"];
200
	}
201
202 1
	public static function getAccessors($class) {
203 1
		if (isset ( self::getModelMetadata ( $class ) ["#accessors"] ))
204 1
			return self::getModelMetadata ( $class ) ["#accessors"];
205
	}
206
207 4
	public static function clearMetaDatas() {
208 4
		self::$modelsMetadatas = [ ];
209 4
	}
210
}
211