Passed
Push — master ( 01b681...b7f456 )
by Jean-Christophe
12:19
created

OrmUtils::getManyToManyFieldsDt()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
c 1
b 0
f 1
dl 0
loc 11
ccs 0
cts 8
cp 0
rs 10
cc 4
nc 4
nop 2
crap 20
1
<?php
2
3
namespace Ubiquity\orm;
4
5
use Ubiquity\orm\parser\Reflexion;
6
use Ubiquity\cache\CacheManager;
7
use Ubiquity\orm\traits\OrmUtilsRelationsTrait;
8
use Ubiquity\orm\traits\OrmUtilsFieldsTrait;
9
use Ubiquity\controllers\rest\formatters\ResponseFormatter;
10
11
/**
12
 * Object/relational mapping utilities
13
 *
14
 * @author jc
15
 * @version 1.0.7
16
 */
17
class OrmUtils {
18
19
	use OrmUtilsFieldsTrait,OrmUtilsRelationsTrait;
20
	private static $modelsMetadatas;
21
22 129
	public static function getModelMetadata($className) {
23 129
		return self::$modelsMetadatas [$className] ??= CacheManager::getOrmModelCache ( $className );
24
	}
25
26 1
	public static function isSerializable($class, $member) {
27 1
		return ! self::_is ( $class, $member, '#notSerializable' );
28
	}
29
30 6
	public static function isNullable($class, $member) {
31 6
		return self::_is ( $class, $member, '#nullable' );
32
	}
33
34 7
	protected static function _is($class, $member, $like) {
35 7
		$ret = self::getAnnotationInfo ( $class, $like );
36 7
		if ($ret !== false) {
37 7
			return \array_search ( $member, $ret ) !== false;
38
		}
39
		return false;
40
	}
41
42 1
	public static function getFieldName($class, $member) {
43 1
		return (self::getAnnotationInfo ( $class, '#fieldNames' ) [$member]) ?? $member;
44
	}
45
46 73
	public static function getTableName($class) {
47 73
		return self::getModelMetadata ( $class ) ['#tableName'];
48
	}
49
50 25
	public static function getKeyFieldsAndValues($instance) {
51 25
		$class = \get_class ( $instance );
52 25
		return self::getFieldsAndValues_ ( $instance, self::getKeyMembers ( $class ) );
53
	}
54
55 26
	public static function getFieldsAndValues_($instance, $members) {
56 26
		$ret = [ ];
57 26
		$fieldnames = self::getAnnotationInfo ( \get_class ( $instance ), '#fieldNames' );
58 26
		foreach ( $members as $member ) {
59 26
			$v = Reflexion::getMemberValue ( $instance, $member );
60 26
			$ret [$fieldnames [$member] ?? $member] = $v;
61
		}
62 26
		return $ret;
63
	}
64
65 1
	public static function getKeyPropsAndValues_($instance, $props) {
66 1
		$ret = [ ];
67 1
		foreach ( $props as $prop ) {
68 1
			$v = Reflexion::getPropValue ( $instance, $prop );
69 1
			$ret [$prop->getName ()] = $v;
70
		}
71 1
		return $ret;
72
	}
73
74 65
	public static function getMembers($className) {
75 65
		$fieldNames = self::getAnnotationInfo ( $className, '#fieldNames' );
76 65
		if ($fieldNames !== false) {
77 65
			return \array_keys ( $fieldNames );
78
		}
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 8
	public static function isNotNullOrNullAccepted($v, $className, $member) {
100 8
		$notNull = (isset ( $v ) && NULL !== $v && '' !== $v);
101 8
		return ($notNull) || (! $notNull && self::isNullable ( $className, $member ));
102
	}
103
104 58
	public static function getFirstKeyValue($instance) {
105 58
		$prop = OrmUtils::getFirstPropKey ( \get_class ( $instance ) );
106 58
		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 2
	public static function getKeyValues($instance) {
114 2
		$fkv = self::getKeyFieldsAndValues ( $instance );
115 2
		return \implode ( '_', $fkv );
116
	}
117
118 79
	public static function getPropKeyValues($instance, $props) {
119 79
		$values = [ ];
120 79
		foreach ( $props as $prop ) {
121 79
			$values [] = $prop->getValue ( $instance );
122
		}
123 79
		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 118
	public static function getAnnotationInfo($class, $keyAnnotation) {
149 118
		return self::getModelMetadata ( $class ) [$keyAnnotation] ?? false;
150
	}
151
152 55
	public static function getAnnotationInfoMember($class, $keyAnnotation, $member) {
153 55
		$info = self::getAnnotationInfo ( $class, $keyAnnotation );
154 55
		if ($info !== false) {
155 55
			if (! isset ( $info [0] )) { // isAssociative
156 55
				if (isset ( $info [$member] )) {
157 55
					return $info [$member];
158
				}
159
			} else {
160 1
				if (\array_search ( $member, $info ) !== false) {
161 1
					return $member;
162
				}
163
			}
164
		}
165 1
		return false;
166
	}
167
168 5
	public static function setFieldToMemberNames(&$fields, $relFields) {
169 5
		foreach ( $fields as $index => $field ) {
170 5
			if (isset ( $relFields [$field] )) {
171 1
				$fields [$index] = $relFields [$field];
172
			}
173
		}
174 5
	}
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 62
	public static function hasAllMembersPublic($className) {
197 62
		$members = self::getMembers ( $className );
198 62
		foreach ( $members as $memberName ) {
199 62
			$field = new \ReflectionProperty ( $className, $memberName );
200 62
			if (! $field->isPublic ()) {
201 58
				return false;
202
			}
203
		}
204 4
		return true;
205
	}
206
207
	public static function isManyToMany($class):bool{
208
		$metas=self::getModelMetadata ( $class );
209
		$pks=$metas['#primaryKeys'];
210
		$manyToOnes=$metas['#manyToOne'];
211
		$manysCount=\count($manyToOnes);
212
		$counter=0;
213
		if($manysCount>1) {
214
			foreach ($manyToOnes as $manyToOne) {
215
				$len = \strlen($manyToOne);
216
				foreach ($pks as $k) {
217
					if (\substr($k, -$len) === \ucfirst($manyToOne)) {
218
						$counter++;
219
					}
220
				}
221
			}
222
			return $counter>1;
223
		}
224
		return false;
225
	}
226
227
	public static function getManyToManyFieldsDt($class,$manyClass) {
228
		$fields=self::getSerializableMembers($manyClass);
229
		$joinColumns=self::getModelMetadata($manyClass)['#joinColumn'];
230
		foreach ($joinColumns as $joinColumn){
231
			if($joinColumn['className']===$class){
232
				if($index=\array_search($joinColumn['name'],$fields)!==false){
233
					unset($fields[$index]);
234
				}
235
			}
236
		}
237
		return array_values($fields);
238
	}
239
}