Passed
Push — master ( abeffe...907b02 )
by Jean-Christophe
01:39
created

Reflexion::getAnnotationClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
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;
1 ignored issue
show
introduced by
The trait Ubiquity\orm\parser\ReflexionFieldsTrait requires some properties which are not provided by Ubiquity\orm\parser\Reflexion: $dbType, $name, $nullable
Loading history...
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
	public static function getMemberValue($instance, $member) {
29
		$prop=self::getProperty($instance, $member);
30
		$prop->setAccessible(true);
31
		return $prop->getValue($instance);
32
	}
33
34
	public static function setMemberValue($instance, $member,$value) {
35
		$prop=self::getProperty($instance, $member);
36
		if($prop){
37
			$prop->setAccessible(true);
38
			$prop->setValue($instance,$value);
39
			return true;
40
		}
41
		return false;
42
	}
43
44
	public static function getPropertiesAndValues($instance, $props=NULL) {
45
		$ret=array ();
46
		$className=get_class($instance);
47
		if (is_null($props))
48
			$props=self::getProperties($instance);
49
		foreach ( $props as $prop ) {
50
			$prop->setAccessible(true);
51
			$v=$prop->getValue($instance);
52
			if (OrmUtils::isSerializable($className, $prop->getName())) {
53
				if (OrmUtils::isNotNullOrNullAccepted($v, $className, $prop->getName())) {
54
					$name=OrmUtils::getFieldName($className, $prop->getName());
55
					$ret[$name]=$v;
56
				}
57
			}
58
		}
59
		return $ret;
60
	}
61
62
	public static function getAnnotationClass($class, $annotation) {
63
		$annot=Annotations::ofClass($class, $annotation);
64
		return $annot;
65
	}
66
67
	public static function getAnnotationMember($class, $member, $annotation) {
68
		$annot=Annotations::ofProperty($class, $member, $annotation);
69
		return current($annot);
70
	}
71
	
72
	public static function getAnnotationsMember($class, $member, $annotation) {
73
		return Annotations::ofProperty($class, $member, $annotation);
74
	}
75
76
	public static function getAnnotationsMethod($class, $method, $annotation) {
77
		if(is_array($annotation)){
78
			$result=[];
79
			foreach($annotation as $annot){
80
				$annots=Annotations::ofMethod($class, $method, $annot);
81
				if(sizeof($annots)>0){
82
					$result=array_merge($result,$annots);
83
				}
84
			}
85
			return $result;
86
		}
87
		$annots=Annotations::ofMethod($class, $method, $annotation);
88
		if (\sizeof($annots) > 0)
89
			return $annots;
90
		return false;
91
	}
92
93
	public static function getMembersAnnotationWithAnnotation($class, $annotation) {
94
		return self::getMembersWithAnnotation_($class, $annotation, function(&$ret,$prop,$annot){$ret[$prop->getName()]=$annot;});
95
	}
96
97
	public static function getMembersWithAnnotation($class, $annotation) {
98
		return self::getMembersWithAnnotation_($class, $annotation, function(&$ret,$prop){$ret[]=$prop;});
99
	}
100
101
	public static function getMembersNameWithAnnotation($class, $annotation) {
102
		return self::getMembersWithAnnotation_($class, $annotation, function(&$ret,$prop){ $ret[]=$prop->getName();});
103
	}
104
	
105
	protected static function getMembersWithAnnotation_($class, $annotation,$callback) {
106
		$props=self::getProperties($class);
107
		$ret=array ();
108
		foreach ( $props as $prop ) {
109
			$annot=self::getAnnotationMember($class, $prop->getName(), $annotation);
110
			if ($annot !== false)
111
				$callback($ret,$prop,$annot);
112
		}
113
		return $ret;
114
	}
115
116
	public static function getTableName($class) {
117
		$ret=Reflexion::getAnnotationClass($class, "@table");
118
		if (\sizeof($ret) === 0) {
119
			$posSlash=strrpos($class, '\\');
120
			if ($posSlash !== false)
121
				$class=substr($class, $posSlash + 1);
122
			$ret=$class;
123
		} else {
124
			$ret=$ret[0]->name;
125
		}
126
		return $ret;
127
	}
128
129
	public static function getMethodParameters(\ReflectionMethod $method) {
130
		$result=array ();
131
		foreach ( $method->getParameters() as $param ) {
132
			$result[]=$param->name;
133
		}
134
		return $result;
135
	}
136
	
137
	public static function getJoinTables($class){
138
		$result=[];
139
		$annots=self::getMembersAnnotationWithAnnotation($class, "@joinTable");
140
		foreach ($annots as $annot){
141
			$result[]=$annot->name;
142
		}
143
		return $result;
144
	}
145
	
146
	public static function getAllJoinTables($models){
147
		$result=[];
148
		foreach ($models as $model){
149
			$result=array_merge($result,self::getJoinTables($model));
150
		}
151
		return $result;
152
	}
153
}
154