Passed
Push — master ( c53668...8969dd )
by Jean-Christophe
08:33
created

Reflexion::getPropertiesAndValues()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 24
ccs 19
cts 19
cp 1
rs 8.8333
c 0
b 0
f 0
cc 7
nc 10
nop 2
crap 7
1
<?php
2
3
namespace Ubiquity\orm\parser;
4
5
use Ubiquity\orm\OrmUtils;
6
use Ubiquity\cache\CacheManager;
7
use Ubiquity\annotations\AnnotationsEngineInterface;
8
9
/**
10
 * Reflection utilities
11
 * in dev environment only
12
 *
13
 * @author jcheron <[email protected]>
14
 * @version 1.0.4
15
 *
16
 */
17
class Reflexion {
18
	use ReflexionFieldsTrait;
19
	protected static $classProperties = [ ];
20
21 1
	public static function getMethods($instance, $filter = null) {
22 1
		$reflect = new \ReflectionClass ( $instance );
23 1
		$methods = $reflect->getMethods ( $filter );
24 1
		return $methods;
25
	}
26
27 31
	public static function getKeyFields($instance) {
28 31
		return self::getMembersNameWithAnnotation ( \get_class ( $instance ), 'id' );
29
	}
30
31 26
	public static function getMemberValue($instance, $member) {
32 26
		$prop = self::getProperty ( $instance, $member );
33 26
		$prop->setAccessible ( true );
34 26
		return $prop->getValue ( $instance );
35
	}
36
37 65
	public static function getPropValue($instance, $prop) {
38 65
		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 19
	public static function getPropertiesAndValues($instance, $props = NULL) {
52 19
		$ret = [];
53 19
		$className = \get_class ( $instance );
54 19
		$modelMetas = OrmUtils::getModelMetadata ( $className );
55 19
		if (isset ( self::$classProperties [$className] )) {
56 17
			foreach ( self::$classProperties [$className] as $name => $prop ) {
57 17
				$ret [$name] = $prop->getValue ( $instance );
58
			}
59 17
			return $ret;
60
		}
61 3
		if (\is_null ( $props ))
62 3
			$props = self::getProperties ( $instance );
63 3
		foreach ( $props as $prop ) {
64 3
			$prop->setAccessible ( true );
65 3
			$v = $prop->getValue ( $instance );
66 3
			if (\array_search ( $prop->getName (), $modelMetas ['#notSerializable'] ) === false) {
67 3
				if (OrmUtils::isNotNullOrNullAccepted ( $v, $className, $prop->getName () )) {
68 3
					$name = $modelMetas ['#fieldNames'] [$prop->getName ()] ?? $prop->getName ();
69 3
					$ret [$name] = $v;
70 3
					self::$classProperties [$className] [$name] = $prop;
71
				}
72
			}
73
		}
74 3
		return $ret;
75
	}
76
77
	/**
78
	 * Returns the annotation engine (php8 attributes or php annotations).
79
	 *
80
	 * @return AnnotationsEngineInterface
81
	 * @since 2.4.0
82
	 */
83 34
	public static function getAnnotsEngine(){
84 34
		return CacheManager::getAnnotationsEngineInstance();
85
	}
86
87 33
	public static function getAnnotationClass($class, $annotation) {
88 33
		$annot = self::getAnnotsEngine()->getAnnotsOfClass( $class, $annotation );
89 33
		return $annot;
90
	}
91
92 31
	public static function getAnnotationMember($class, $member, $annotation) {
93 31
		$annot = self::getAnnotsEngine()->getAnnotsOfProperty( $class, $member, $annotation );
94 31
		return \current ( $annot );
95
	}
96
97
	public static function getAnnotationMethod($class, $method, $annotation) {
98
		$annot = self::getAnnotsEngine()->getAnnotsOfMethod( $class, $method, $annotation );
99
		return \current ( $annot );
100
	}
101
102 32
	public static function getAnnotationsMember($class, $member, $annotation) {
103 32
		return self::getAnnotsEngine()->getAnnotsOfProperty ( $class, $member, $annotation );
104
	}
105
106 1
	public static function getAnnotationsMethod($class, $method, $annotation) {
107 1
		$annotsEngine=self::getAnnotsEngine();
108 1
		if (\is_array ( $annotation )) {
109 1
			$result = [ ];
110 1
			foreach ( $annotation as $annot ) {
111 1
				$annots = $annotsEngine->getAnnotsOfMethod( $class, $method, $annot );
112 1
				if (\count ( $annots ) > 0) {
113 1
					$result = \array_merge ( $result, $annots );
114
				}
115
			}
116 1
			return $result;
117
		}
118 1
		$annots = $annotsEngine->getAnnotsOfMethod ( $class, $method, $annotation );
119 1
		if (\count ( $annots ) > 0){
120 1
			return $annots;
121
		}
122 1
		return false;
123
	}
124
125 31
	public static function getMembersAnnotationWithAnnotation($class, $annotation) {
126 31
		return self::getMembersWithAnnotation_ ( $class, $annotation, function (&$ret, $prop, $annot) {
127
			$ret [$prop->getName ()] = $annot;
128 31
		} );
129
	}
130
131
	public static function getMembersWithAnnotation($class, $annotation) {
132
		return self::getMembersWithAnnotation_ ( $class, $annotation, function (&$ret, $prop) {
133
			$ret [] = $prop;
134
		} );
135
	}
136
137 31
	public static function getMembersNameWithAnnotation($class, $annotation) {
138 31
		return self::getMembersWithAnnotation_ ( $class, $annotation, function (&$ret, $prop) {
139
			$ret [] = $prop->getName ();
140 31
		} );
141
	}
142
143 31
	protected static function getMembersWithAnnotation_($class, $annotation, $callback) {
144 31
		$props = self::getProperties ( $class );
145 31
		$ret = array ();
146 31
		foreach ( $props as $prop ) {
147 31
			$annot = self::getAnnotationMember ( $class, $prop->getName (), $annotation );
148 31
			if ($annot !== false)
149
				$callback ( $ret, $prop, $annot );
150
		}
151 31
		return $ret;
152
	}
153
154 31
	public static function getTableName($class) {
155 31
		$ret = self::getAnnotationClass ( $class, 'table' );
156 31
		if (\count ( $ret ) === 0) {
157 31
			$posSlash = \strrpos ( $class, '\\' );
158 31
			if ($posSlash !== false)
159 31
				$class = \substr ( $class, $posSlash + 1 );
160 31
			$ret = $class;
161
		} else {
162
			$ret = $ret [0]->name;
163
		}
164 31
		return $ret;
165
	}
166
167 1
	public static function getMethodParameters(\ReflectionFunctionAbstract $method) {
168 1
		$result = array ();
169 1
		foreach ( $method->getParameters () as $param ) {
170 1
			$result [] = $param->name;
171
		}
172 1
		return $result;
173
	}
174
175
	public static function getJoinTables($class) {
176
		$result = [ ];
177
		$annots = self::getMembersAnnotationWithAnnotation ( $class, 'joinTable' );
178
		foreach ( $annots as $annot ) {
179
			$result [] = $annot->name;
180
		}
181
		return $result;
182
	}
183
184
	public static function getAllJoinTables($models) {
185
		$result = [ ];
186
		foreach ( $models as $model ) {
187
			$result = array_merge ( $result, self::getJoinTables ( $model ) );
188
		}
189
		return $result;
190
	}
191
}
192