Passed
Push — master ( 055b0b...f9591e )
by Jean-Christophe
08:37
created

OrmUtils::getMembers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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