Passed
Push — master ( 8220c0...484027 )
by Jean-Christophe
09:22
created

Reflexion::getKeyFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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