Completed
Push — master ( 1f90e4...0f8170 )
by Jean-Christophe
02:14
created

OrmUtils::getFirstKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace micro\orm;
3
4
/**
5
 * Utilitaires de mappage Objet/relationnel
6
 * @author jc
7
 * @version 1.0.0.4
8
 * @package orm
9
 */
10
class OrmUtils{
11
	public static $ormCache;
12
	private static $modelsMetadatas;
13
14
	public static function createOrmModelCache($className){
15
		$key=\str_replace("\\", DIRECTORY_SEPARATOR, $className);
16
		if(!self::$ormCache->exists($key)){
17
			$p=new ModelParser();
18
			$p->parse($className);
19
			self::$ormCache->store($key, $p->__toString());
20
		}
21
		self::$modelsMetadatas[$className]=self::$ormCache->fetch($key);
22
		return self::$modelsMetadatas[$className];
23
	}
24
25
	public static function getModelMetadata($className){
26
		if(!isset(self::$modelsMetadatas[$className])){
27
			self::createOrmModelCache($className);
28
		}
29
		return self::$modelsMetadatas[$className];
30
	}
31
32 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...
33
		$ret=self::getAnnotationInfo($class,"#notSerializable");
34
		if ($ret!==false)
35
			return \array_search($member, $ret)===false;
36
		else
37
			return true;
38
	}
39
40 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...
41
		$ret=self::getAnnotationInfo($class,"#nullable");
42
		if ($ret!==false)
43
			return \array_search($member, $ret)!==false;
44
		else
45
			return false;
46
	}
47
48 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...
49
		$ret=self::getAnnotationInfo($class, "#fieldNames");
50
		if($ret===false)
51
			$ret=$member;
52
		else
53
			$ret=$ret[$member];
54
		return $ret;
55
	}
56
57
	public static function getTableName($class){
58
		return self::getModelMetadata($class)["#tableName"];
59
	}
60
61
	public static function getKeyFieldsAndValues($instance){
62
		$kf=self::getAnnotationInfo(get_class($instance), "#primaryKeys");
63
		return self::getMembersAndValues($instance,$kf);
64
	}
65
66
	public static function getKeyFields($instance){
67
		return self::getAnnotationInfo(get_class($instance), "#primaryKeys");
68
	}
69
70
	public function getMembers($className){
71
		$fieldNames=self::getAnnotationInfo($className, "#fieldNames");
72
		if($fieldNames!==false)
73
			return \array_keys($fieldNames);
74
		return [];
75
	}
76
77
	public static function getMembersAndValues($instance,$members=NULL){
78
		$ret=array();
79
		$className=get_class($instance);
80
		if(is_null($members))
81
			$members=self::getMembers($className);
82
		foreach ($members as $member){
83
			if(OrmUtils::isSerializable($className,$member)){
84
				$v=Reflexion::getMemberValue($instance, $member);
85 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...
86
					$name=self::getFieldName($className, $member);
87
					$ret[$name]=$v;
88
				}
89
			}
90
		}
91
		return $ret;
92
	}
93
94
	public static function getFirstKey($class){
95
		$kf=self::getAnnotationInfo($class, "#primaryKeys");
96
		return \reset($kf);
97
	}
98
99
	public static function getFirstKeyValue($instance){
100
		$fkv=self::getKeyFieldsAndValues($instance);
101
		return \reset($fkv);
102
	}
103
104
	/**
105
	 * @param object $instance
106
	 * @return mixed[]
107
	 */
108
	public static function getManyToOneMembersAndValues($instance){
109
		$ret=array();
110
		$class=get_class($instance);
111
		$members=self::getAnnotationInfo($class, "#manyToOne");
112
		if($members!==false){
113
			foreach ($members as $member){
114
				$memberAccessor="get".ucfirst($member);
115
				if(method_exists($instance,$memberAccessor)){
116
					$memberInstance=$instance->$memberAccessor();
117
					if(isset($memberInstance)){
118
						$keyValues=self::getKeyFieldsAndValues($memberInstance);
119
						if(sizeof($keyValues)>0){
120
							$fkName=self::getJoinColumnName($class, $member);
121
							$ret[$fkName]=reset($keyValues);
122
						}
123
					}
124
				}
125
			}
126
		}
127
		return $ret;
128
	}
129
130
	public static function getMembersWithAnnotation($class,$annotation){
131
		if(isset(self::getModelMetadata($class)[$annotation]))
132
			return self::getModelMetadata($class)[$annotation];
133
		return [];
134
	}
135
136
137
138
	/**
139
	 * @param object $instance
140
	 * @param string $memberKey
141
	 * @param array $array
142
	 * @return boolean
143
	 */
144
	public static function exists($instance,$memberKey,$array){
145
		$accessor="get".ucfirst($memberKey);
146
		if(method_exists($instance, $accessor)){
147
			if($array!==null){
148
				foreach ($array as $value){
149
					if($value->$accessor()==$instance->$accessor())
150
						return true;
151
				}
152
			}
153
		}
154
		return false;
155
	}
156
157
	public static function getJoinColumnName($class,$member){
158
		$annot=self::getAnnotationInfoMember($class, "#joinColumn",$member);
159
		if($annot!==false){
160
			$fkName=$annot["name"];
161
		}else{
162
			$fkName="id".ucfirst(self::getTableName(ucfirst($member)));
163
		}
164
		return $fkName;
165
	}
166
167
	public static function getAnnotationInfo($class,$keyAnnotation){
168
		if(isset(self::getModelMetadata($class)[$keyAnnotation]))
169
			return self::getModelMetadata($class)[$keyAnnotation];
170
		return false;
171
	}
172
173
	public static function getAnnotationInfoMember($class,$keyAnnotation,$member){
174
		$info=self::getAnnotationInfo($class, $keyAnnotation);
175
		if($info!==false){
176
			if(isset($info[$member])){
177
				return $info[$member];
178
			}
179
		}
180
		return false;
181
	}
182
183
	public static function getSerializableFields($class){
184
		$notSerializable=self::getAnnotationInfo($class, "#notSerializable");
185
		$fieldNames=\array_keys(self::getAnnotationInfo($class, "#fieldNames"));
186
		return \array_diff($fieldNames, $notSerializable);
187
	}
188
189
	public static function getFieldsInRelations($class){
190
		$result=[];
191
		if($manyToOne=self::getAnnotationInfo($class, "#manyToOne")){
192
			$result=\array_merge($result,$manyToOne);
193
		}
194
		if($oneToMany=self::getAnnotationInfo($class, "#oneToMany")){
195
			$result=\array_merge($result,\array_keys($oneToMany));
196
		}
197
		return $result;
198
	}
199
}
200