Passed
Push — master ( ed813f...432706 )
by Jean-Christophe
02:43
created

Reflexion::getAnnotationMember()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ubiquity\orm\parser;
4
5
use mindplay\annotations\Annotations;
6
use Ubiquity\orm\OrmUtils;
7
8
9
/**
10
 * Reflection utilities
11
 * in dev environment only
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.1
14
 */
15
class Reflexion {
16
	use ReflexionFieldsTrait;
17
18
	public static function getMethods($instance, $filter=null) {
19
		$reflect=new \ReflectionClass($instance);
20
		$methods=$reflect->getMethods($filter);
21
		return $methods;
22
	}
23
24
	public static function getKeyFields($instance) {
25
		return self::getMembersNameWithAnnotation(get_class($instance), "@id");
26
	}
27
28 1
	public static function getMemberValue($instance, $member) {
29 1
		$prop=self::getProperty($instance, $member);
30 1
		$prop->setAccessible(true);
31 1
		return $prop->getValue($instance);
32
	}
33
	
34 7
	public static function getPropValue($instance, $prop) {
35 7
		return $prop->getValue($instance);
36
	}
37
38
	public static function setMemberValue($instance, $member,$value) {
39
		$prop=self::getProperty($instance, $member);
40
		if($prop){
41
			$prop->setAccessible(true);
42
			$prop->setValue($instance,$value);
43
			return true;
44
		}
45
		return false;
46
	}
47
48
	public static function getPropertiesAndValues($instance, $props=NULL) {
49
		$ret=array ();
50
		$className=get_class($instance);
51
		if (is_null($props))
52
			$props=self::getProperties($instance);
53
		foreach ( $props as $prop ) {
54
			$prop->setAccessible(true);
55
			$v=$prop->getValue($instance);
56
			if (OrmUtils::isSerializable($className, $prop->getName())) {
57
				if (OrmUtils::isNotNullOrNullAccepted($v, $className, $prop->getName())) {
58
					$name=OrmUtils::getFieldName($className, $prop->getName());
59
					$ret[$name]=$v;
60
				}
61
			}
62
		}
63
		return $ret;
64
	}
65
66
	public static function getAnnotationClass($class, $annotation) {
67
		$annot=Annotations::ofClass($class, $annotation);
68
		return $annot;
69
	}
70
71
	public static function getAnnotationMember($class, $member, $annotation) {
72
		$annot=Annotations::ofProperty($class, $member, $annotation);
73
		return current($annot);
74
	}
75
	
76
	public static function getAnnotationsMember($class, $member, $annotation) {
77
		return Annotations::ofProperty($class, $member, $annotation);
78
	}
79
80
	public static function getAnnotationsMethod($class, $method, $annotation) {
81
		if(is_array($annotation)){
82
			$result=[];
83
			foreach($annotation as $annot){
84
				$annots=Annotations::ofMethod($class, $method, $annot);
85
				if(sizeof($annots)>0){
86
					$result=array_merge($result,$annots);
87
				}
88
			}
89
			return $result;
90
		}
91
		$annots=Annotations::ofMethod($class, $method, $annotation);
92
		if (\sizeof($annots) > 0)
93
			return $annots;
94
		return false;
95
	}
96
97
	public static function getMembersAnnotationWithAnnotation($class, $annotation) {
98
		return self::getMembersWithAnnotation_($class, $annotation, function(&$ret,$prop,$annot){$ret[$prop->getName()]=$annot;});
99
	}
100
101
	public static function getMembersWithAnnotation($class, $annotation) {
102
		return self::getMembersWithAnnotation_($class, $annotation, function(&$ret,$prop){$ret[]=$prop;});
103
	}
104
105
	public static function getMembersNameWithAnnotation($class, $annotation) {
106
		return self::getMembersWithAnnotation_($class, $annotation, function(&$ret,$prop){ $ret[]=$prop->getName();});
107
	}
108
	
109
	protected static function getMembersWithAnnotation_($class, $annotation,$callback) {
110
		$props=self::getProperties($class);
111
		$ret=array ();
112
		foreach ( $props as $prop ) {
113
			$annot=self::getAnnotationMember($class, $prop->getName(), $annotation);
114
			if ($annot !== false)
115
				$callback($ret,$prop,$annot);
116
		}
117
		return $ret;
118
	}
119
120
	public static function getTableName($class) {
121
		$ret=Reflexion::getAnnotationClass($class, "@table");
122
		if (\sizeof($ret) === 0) {
123
			$posSlash=strrpos($class, '\\');
124
			if ($posSlash !== false)
125
				$class=substr($class, $posSlash + 1);
126
			$ret=$class;
127
		} else {
128
			$ret=$ret[0]->name;
129
		}
130
		return $ret;
131
	}
132
133
	public static function getMethodParameters(\ReflectionMethod $method) {
134
		$result=array ();
135
		foreach ( $method->getParameters() as $param ) {
136
			$result[]=$param->name;
137
		}
138
		return $result;
139
	}
140
	
141
	public static function getJoinTables($class){
142
		$result=[];
143
		$annots=self::getMembersAnnotationWithAnnotation($class, "@joinTable");
144
		foreach ($annots as $annot){
145
			$result[]=$annot->name;
146
		}
147
		return $result;
148
	}
149
	
150
	public static function getAllJoinTables($models){
151
		$result=[];
152
		foreach ($models as $model){
153
			$result=array_merge($result,self::getJoinTables($model));
154
		}
155
		return $result;
156
	}
157
}
158