Passed
Push — master ( 213516...f477f5 )
by Jean-Christophe
04:14
created

Reflexion::getMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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