Completed
Push — master ( 31895c...135d28 )
by Jean-Christophe
02:15
created

OrmUtils::getMembersAndValues()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 16
Code Lines 12

Duplication

Lines 4
Ratio 25 %

Importance

Changes 0
Metric Value
dl 4
loc 16
rs 7.756
c 0
b 0
f 0
cc 9
eloc 12
nc 8
nop 2
1
<?php
2
namespace micro\orm;
3
4
use mindplay\annotations\Annotations;
5
use mindplay\annotations\AnnotationCache;
6
use mindplay\annotations\AnnotationManager;
7
use micro\orm\parser\Reflexion;
8
use micro\cache\CacheManager;
9
10
/**
11
 * Utilitaires de mappage Objet/relationnel
12
 * @author jc
13
 * @version 1.0.0.5
14
 * @package orm
15
 */
16
class OrmUtils{
17
	private static $modelsMetadatas;
18
19
	public static function getModelMetadata($className){
20
		if(!isset(self::$modelsMetadatas[$className])){
21
			self::$modelsMetadatas[$className]=CacheManager::createOrmModelCache($className);
22
		}
23
		return self::$modelsMetadatas[$className];
24
	}
25
26 View Code Duplication
	public static function isSerializable($class,$member){
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
		$ret=self::getAnnotationInfo($class,"#notSerializable");
28
		if ($ret!==false)
29
			return \array_search($member, $ret)===false;
30
		else
31
			return true;
32
	}
33
34 View Code Duplication
	public static function isNullable($class,$member){
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
		$ret=self::getAnnotationInfo($class,"#nullable");
36
		if ($ret!==false)
37
			return \array_search($member, $ret)!==false;
38
		else
39
			return false;
40
	}
41
42 View Code Duplication
	public static function getFieldName($class,$member){
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
		$ret=self::getAnnotationInfo($class, "#fieldNames");
44
		if($ret===false)
45
			$ret=$member;
46
		else
47
			$ret=$ret[$member];
48
		return $ret;
49
	}
50
51
	public static function getTableName($class){
52
		return self::getModelMetadata($class)["#tableName"];
53
	}
54
55
	public static function getKeyFieldsAndValues($instance){
56
		$kf=self::getAnnotationInfo(get_class($instance), "#primaryKeys");
57
		return self::getMembersAndValues($instance,$kf);
58
	}
59
60
	public static function getKeyFields($instance){
61
		return self::getAnnotationInfo(get_class($instance), "#primaryKeys");
62
	}
63
64
	public function getMembers($className){
65
		$fieldNames=self::getAnnotationInfo($className, "#fieldNames");
66
		if($fieldNames!==false)
67
			return \array_keys($fieldNames);
68
		return [];
69
	}
70
71
	public static function getMembersAndValues($instance,$members=NULL){
72
		$ret=array();
73
		$className=get_class($instance);
74
		if(is_null($members))
75
			$members=self::getMembers($className);
76
		foreach ($members as $member){
77
			if(OrmUtils::isSerializable($className,$member)){
78
				$v=Reflexion::getMemberValue($instance, $member);
79 View Code Duplication
				if(($v!==null && $v!=="") || (($v===null || $v==="") && OrmUtils::isNullable($className, $member))){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
					$name=self::getFieldName($className, $member);
81
					$ret[$name]=$v;
82
				}
83
			}
84
		}
85
		return $ret;
86
	}
87
88
	public static function getFirstKey($class){
89
		$kf=self::getAnnotationInfo($class, "#primaryKeys");
90
		return \reset($kf);
91
	}
92
93
	public static function getFirstKeyValue($instance){
94
		$fkv=self::getKeyFieldsAndValues($instance);
95
		return \reset($fkv);
96
	}
97
98
	/**
99
	 * @param object $instance
100
	 * @return mixed[]
101
	 */
102
	public static function getManyToOneMembersAndValues($instance){
103
		$ret=array();
104
		$class=get_class($instance);
105
		$members=self::getAnnotationInfo($class, "#manyToOne");
106
		if($members!==false){
107
			foreach ($members as $member){
108
				$memberAccessor="get".ucfirst($member);
109
				if(method_exists($instance,$memberAccessor)){
110
					$memberInstance=$instance->$memberAccessor();
111
					if(isset($memberInstance)){
112
						$keyValues=self::getKeyFieldsAndValues($memberInstance);
113
						if(sizeof($keyValues)>0){
114
							$fkName=self::getJoinColumnName($class, $member);
115
							$ret[$fkName]=reset($keyValues);
116
						}
117
					}
118
				}
119
			}
120
		}
121
		return $ret;
122
	}
123
124
	public static function getMembersWithAnnotation($class,$annotation){
125
		if(isset(self::getModelMetadata($class)[$annotation]))
126
			return self::getModelMetadata($class)[$annotation];
127
		return [];
128
	}
129
130
	/**
131
	 * @param object $instance
132
	 * @param string $memberKey
133
	 * @param array $array
134
	 * @return boolean
135
	 */
136
	public static function exists($instance,$memberKey,$array){
137
		$accessor="get".ucfirst($memberKey);
138
		if(method_exists($instance, $accessor)){
139
			if($array!==null){
140
				foreach ($array as $value){
141
					if($value->$accessor()==$instance->$accessor())
142
						return true;
143
				}
144
			}
145
		}
146
		return false;
147
	}
148
149
	public static function getJoinColumnName($class,$member){
150
		$annot=self::getAnnotationInfoMember($class, "#joinColumn",$member);
151
		if($annot!==false){
152
			$fkName=$annot["name"];
153
		}else{
154
			$fkName="id".ucfirst(self::getTableName(ucfirst($member)));
155
		}
156
		return $fkName;
157
	}
158
159
	public static function getAnnotationInfo($class,$keyAnnotation){
160
		if(isset(self::getModelMetadata($class)[$keyAnnotation]))
161
			return self::getModelMetadata($class)[$keyAnnotation];
162
		return false;
163
	}
164
165
	public static function getAnnotationInfoMember($class,$keyAnnotation,$member){
166
		$info=self::getAnnotationInfo($class, $keyAnnotation);
167
		if($info!==false){
168
			if(isset($info[$member])){
169
				return $info[$member];
170
			}
171
		}
172
		return false;
173
	}
174
175
	public static function getSerializableFields($class){
176
		$notSerializable=self::getAnnotationInfo($class, "#notSerializable");
177
		$fieldNames=\array_keys(self::getAnnotationInfo($class, "#fieldNames"));
178
		return \array_diff($fieldNames, $notSerializable);
179
	}
180
181
	public static function getFieldsInRelations($class){
182
		$result=[];
183
		if($manyToOne=self::getAnnotationInfo($class, "#manyToOne")){
184
			$result=\array_merge($result,$manyToOne);
185
		}
186
		if($oneToMany=self::getAnnotationInfo($class, "#oneToMany")){
187
			$result=\array_merge($result,\array_keys($oneToMany));
188
		}
189
		return $result;
190
	}
191
192
	public static function getDefaultFk($classname){
193
		return "id".\ucfirst(self::getTableName($classname));
194
	}
195
}
196