Completed
Push — master ( ac3b2a...67ef21 )
by Jean-Christophe
02:07
created

Reflexion::getMemberValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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