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

Reflexion::getTableName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
1
<?php
2
namespace micro\orm;
3
4
use mindplay\annotations\Annotation;
5
use mindplay\annotations\Annotations;
6
7
/**
8
 * Utilitaires de Reflexion
9
 * @author jc
10
 * @version 1.0.0.1
11
 * @package orm
12
 */
13
class Reflexion{
14
	public static function getProperties($instance){
15
		if(\is_string($instance)){
16
			$instance=new $instance();
17
		}
18
		$reflect = new \ReflectionClass($instance);
19
		$props = $reflect->getProperties();
20
		return $props;
21
	}
22
23
	public static function getKeyFields($instance){
24
		return Reflexion::getMembersNameWithAnnotation(get_class($instance), "@id");
25
	}
26
27
	public static function getMemberValue($instance,$member){
28
		$prop=self::getProperty($instance, $member);
29
		$prop->setAccessible(true);
30
		return $prop->getValue($instance);
31
	}
32
33
	public static function getProperty($instance,$member){
34
		$reflect = new \ReflectionClass($instance);
35
		$prop = $reflect->getProperty($member);
36
		return $prop;
37
	}
38
39
	public static function getPropertiesAndValues($instance,$props=NULL){
40
		$ret=array();
41
		$className=get_class($instance);
42
		if(is_null($props))
43
			$props=self::getProperties($instance);
44
		foreach ($props as $prop){
45
			$prop->setAccessible(true);
46
			$v=$prop->getValue($instance);
47
			if(OrmUtils::isSerializable($className,$prop->getName())){
48 View Code Duplication
				if(($v!==null && $v!=="") || (($v===null || $v==="") && OrmUtils::isNullable($className, $prop->getName()))){
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...
49
					$name=OrmUtils::getFieldName($className, $prop->getName());
50
					$ret[$name]=$v;
51
				}
52
			}
53
		}
54
		return $ret;
55
	}
56
57
	public static function getAnnotationClass($class,$annotation){
58
		$annot=Annotations::ofClass($class,$annotation);
59
		return $annot;
60
	}
61
62
	public static function getAnnotationMember($class,$member,$annotation){
63
		$annot=Annotations::ofProperty($class,$member,$annotation);
64
		if(\sizeof($annot)>0)
65
			return $annot[0];
66
		return false;
67
	}
68
69
	public static function getMembersAnnotationWithAnnotation($class,$annotation){
70
		$props=self::getProperties($class);
71
		$ret=array();
72
		foreach ($props as $prop){
73
			$annot=self::getAnnotationMember($class, $prop->getName(), $annotation);
74
			if($annot!==false)
75
				$ret[$prop->getName()]=$annot;
76
		}
77
		return $ret;
78
	}
79
80 View Code Duplication
	public static function getMembersWithAnnotation($class,$annotation){
81
		$props=self::getProperties($class);
82
		$ret=array();
83
		foreach ($props as $prop){
84
			$annot=self::getAnnotationMember($class, $prop->getName(), $annotation);
85
			if($annot!==false)
86
				$ret[]=$prop;
87
		}
88
		return $ret;
89
	}
90
91 View Code Duplication
	public static function getMembersNameWithAnnotation($class,$annotation){
92
		$props=self::getProperties($class);
93
		$ret=array();
94
		foreach ($props as $prop){
95
			$annot=self::getAnnotationMember($class, $prop->getName(), $annotation);
96
			if($annot!==false)
97
				$ret[]=$prop->getName();
98
		}
99
		return $ret;
100
	}
101
102
	public static function isNullable($class,$member){
103
		$ret=self::getAnnotationMember($class,$member,"@column");
104
		if (!$ret)
105
			return false;
106
		else
107
			return $ret->nullable;
108
	}
109
110
	public static function isSerializable($class,$member){
111
		if (self::getAnnotationMember($class,$member,"@transient")!==false || self::getAnnotationMember($class,$member,"@manyToOne")!==false ||
112
				self::getAnnotationMember($class,$member,"@manyToMany")!==false || self::getAnnotationMember($class,$member,"@oneToMany")!==false)
113
			return false;
114
		else
115
			return true;
116
	}
117
118 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...
119
		$ret=self::getAnnotationMember($class, $member, "@column");
120
		if($ret===false)
121
			$ret=$member;
122
		else
123
			$ret=$ret->name;
124
		return $ret;
125
	}
126
127
	public static function getTableName($class){
128
		$ret=Reflexion::getAnnotationClass($class, "@table");
129
		if(\sizeof($ret)===0){
130
			$posSlash=strrpos($class, '\\');
131
			if($posSlash!==false)
132
				$class=substr($class,  $posSlash+ 1);
133
			$ret=$class;
134
		}
135
		else{
136
			$ret=$ret[0]->name;
137
		}
138
		return $ret;
139
	}
140
}
141