Completed
Push — master ( 4e6026...e0b9fa )
by Jean-Christophe
01:30
created

Reflexion::getMembersAnnotationWithAnnotation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
1
<?php
2
3
namespace Ubiquity\orm\parser;
4
5
use mindplay\annotations\Annotation;
6
use mindplay\annotations\Annotations;
7
use Ubiquity\orm\OrmUtils;
8
9
/**
10
 * Utilitaires de Reflexion
11
 * @author jc
12
 * @version 1.0.0.2
13
 */
14
class Reflexion {
15
16
	public static function getProperties($instance) {
17
		if (\is_string($instance)) {
18
			$instance=new $instance();
19
		}
20
		$reflect=new \ReflectionClass($instance);
21
		$props=$reflect->getProperties();
22
		return $props;
23
	}
24
25
	public static function getMethods($instance, $filter=null) {
26
		$reflect=new \ReflectionClass($instance);
27
		$methods=$reflect->getMethods($filter);
28
		return $methods;
29
	}
30
31
	public static function getKeyFields($instance) {
32
		return Reflexion::getMembersNameWithAnnotation(get_class($instance), "@id");
33
	}
34
35
	public static function getMemberValue($instance, $member) {
36
		$prop=self::getProperty($instance, $member);
37
		$prop->setAccessible(true);
38
		return $prop->getValue($instance);
39
	}
40
41
	public static function setMemberValue($instance, $member,$value) {
42
		$prop=self::getProperty($instance, $member);
43
		if($prop){
44
			$prop->setAccessible(true);
45
			$prop->setValue($instance,$value);
46
			return true;
47
		}
48
		return false;
49
	}
50
51
	public static function getProperty($instance, $member) {
52
		$reflect=new \ReflectionClass($instance);
53
		$prop=false;
54
		if($reflect->hasProperty($member))
55
			$prop=$reflect->getProperty($member);
56
		return $prop;
57
	}
58
59
	public static function getPropertiesAndValues($instance, $props=NULL) {
60
		$ret=array ();
61
		$className=get_class($instance);
62
		if (is_null($props))
63
			$props=self::getProperties($instance);
64
		foreach ( $props as $prop ) {
65
			$prop->setAccessible(true);
66
			$v=$prop->getValue($instance);
67
			if (OrmUtils::isSerializable($className, $prop->getName())) {
68 View Code Duplication
				if (OrmUtils::isNotNullOrNullAccepted($v, $className, $prop->getName())) {
1 ignored issue
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...
69
					$name=OrmUtils::getFieldName($className, $prop->getName());
70
					$ret[$name]=$v;
71
				}
72
			}
73
		}
74
		return $ret;
75
	}
76
77
	public static function getAnnotationClass($class, $annotation) {
78
		$annot=Annotations::ofClass($class, $annotation);
79
		return $annot;
80
	}
81
82
	public static function getAnnotationMember($class, $member, $annotation) {
83
		$annot=Annotations::ofProperty($class, $member, $annotation);
84
		if (\sizeof($annot) > 0)
85
			return $annot[0];
86
		return false;
87
	}
88
89
	public static function getAnnotationsMethod($class, $method, $annotation) {
90
		$annots=Annotations::ofMethod($class, $method, $annotation);
91
		if (\sizeof($annots) > 0)
92
			return $annots;
93
		return false;
94
	}
95
96 View Code Duplication
	public static function getMembersAnnotationWithAnnotation($class, $annotation) {
97
		$props=self::getProperties($class);
98
		$ret=array ();
99
		foreach ( $props as $prop ) {
100
			$annot=self::getAnnotationMember($class, $prop->getName(), $annotation);
101
			if ($annot !== false)
102
				$ret[$prop->getName()]=$annot;
103
		}
104
		return $ret;
105
	}
106
107
	public static function getMembersWithAnnotation($class, $annotation) {
108
		$props=self::getProperties($class);
109
		$ret=array ();
110
		foreach ( $props as $prop ) {
111
			$annot=self::getAnnotationMember($class, $prop->getName(), $annotation);
112
			if ($annot !== false)
113
				$ret[]=$prop;
114
		}
115
		return $ret;
116
	}
117
118 View Code Duplication
	public static function getMembersNameWithAnnotation($class, $annotation) {
119
		$props=self::getProperties($class);
120
		$ret=array ();
121
		foreach ( $props as $prop ) {
122
			$annot=self::getAnnotationMember($class, $prop->getName(), $annotation);
123
			if ($annot !== false)
124
				$ret[]=$prop->getName();
125
		}
126
		return $ret;
127
	}
128
129
	public static function isNullable($class, $member) {
130
		$ret=self::getAnnotationMember($class, $member, "@column");
131
		if (!$ret)
132
			return false;
133
		else
134
			return $ret->nullable;
135
	}
136
137
	public static function getDbType($class, $member) {
138
		$ret=self::getAnnotationMember($class, $member, "@column");
139
		if (!$ret)
140
			return false;
141
		else
142
			return $ret->dbType;
143
	}
144
145
	public static function isSerializable($class, $member) {
146
		if (self::getAnnotationMember($class, $member, "@transient") !== false || self::getAnnotationMember($class, $member, "@manyToOne") !== false || self::getAnnotationMember($class, $member, "@manyToMany") !== false || self::getAnnotationMember($class, $member, "@oneToMany") !== false)
147
			return false;
148
		else
149
			return true;
150
	}
151
152
	public static function getFieldName($class, $member) {
153
		$ret=self::getAnnotationMember($class, $member, "@column");
154
		if ($ret === false || !isset($ret->name))
155
			$ret=$member;
156
		else
157
			$ret=$ret->name;
158
		return $ret;
159
	}
160
161
	public static function getTableName($class) {
162
		$ret=Reflexion::getAnnotationClass($class, "@table");
163
		if (\sizeof($ret) === 0) {
164
			$posSlash=strrpos($class, '\\');
165
			if ($posSlash !== false)
166
				$class=substr($class, $posSlash + 1);
167
			$ret=$class;
168
		} else {
169
			$ret=$ret[0]->name;
170
		}
171
		return $ret;
172
	}
173
174
	public static function getMethodParameters(\ReflectionMethod $method) {
175
		$result=array ();
176
		foreach ( $method->getParameters() as $param ) {
177
			$result[]=$param->name;
178
		}
179
		return $result;
180
	}
181
}
182