Passed
Push — master ( 5c03cc...d5672a )
by Jean-Christophe
09:14
created

Reflexion::getTableName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

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