|
1
|
|
|
<?php |
|
2
|
|
|
namespace micro\orm; |
|
3
|
|
|
|
|
4
|
|
|
use mindplay\annotations\Annotation; |
|
5
|
|
|
use mindplay\annotations\Annotations; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Utilitaires de Reflexion |
|
9
|
|
|
* @author jc |
|
10
|
|
|
* @version 1.0.0.1 |
|
11
|
|
|
* @package orm |
|
12
|
|
|
*/ |
|
13
|
|
|
class Reflexion{ |
|
14
|
|
|
public static function getProperties($instance){ |
|
15
|
|
|
if(\is_string($instance)){ |
|
16
|
|
|
$instance=new $instance(); |
|
17
|
|
|
} |
|
18
|
|
|
$reflect = new \ReflectionClass($instance); |
|
19
|
|
|
$props = $reflect->getProperties(); |
|
20
|
|
|
return $props; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public static function getProperty($instance,$property){ |
|
24
|
|
|
$reflect = new \ReflectionClass($instance); |
|
25
|
|
|
$prop = $reflect->getProperty($property); |
|
26
|
|
|
return $prop; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public static function getPropertiesAndValues($instance,$props=NULL){ |
|
30
|
|
|
$ret=array(); |
|
31
|
|
|
$className=get_class($instance); |
|
32
|
|
|
if(is_null($props)) |
|
33
|
|
|
$props=self::getProperties($instance); |
|
34
|
|
|
foreach ($props as $prop){ |
|
35
|
|
|
$prop->setAccessible(true); |
|
36
|
|
|
$v=$prop->getValue($instance); |
|
37
|
|
|
if(OrmUtils::isSerializable($className,$prop->getName())){ |
|
38
|
|
|
if(($v!==null && $v!=="") || (($v===null || $v==="") && OrmUtils::isNullable($className, $prop->getName()))){ |
|
39
|
|
|
$name=OrmUtils::getFieldName($className, $prop->getName()); |
|
40
|
|
|
$ret[$name]=$v; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
return $ret; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public static function getAnnotationClass($class,$annotation){ |
|
48
|
|
|
$annot=Annotations::ofClass($class,$annotation); |
|
49
|
|
|
return $annot; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public static function getAnnotationMember($class,$member,$annotation){ |
|
53
|
|
|
$annot=Annotations::ofProperty($class,$member,$annotation); |
|
54
|
|
|
if(\sizeof($annot)>0) |
|
55
|
|
|
return $annot[0]; |
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
View Code Duplication |
public static function getMembersWithAnnotation($class,$annotation){ |
|
|
|
|
|
|
60
|
|
|
$props=self::getProperties($class); |
|
61
|
|
|
$ret=array(); |
|
62
|
|
|
foreach ($props as $prop){ |
|
63
|
|
|
$annot=self::getAnnotationMember($class, $prop->getName(), $annotation); |
|
64
|
|
|
if($annot!==false) |
|
65
|
|
|
$ret[]=$prop; |
|
66
|
|
|
} |
|
67
|
|
|
return $ret; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
View Code Duplication |
public static function getMembersNameWithAnnotation($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(); |
|
77
|
|
|
} |
|
78
|
|
|
return $ret; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
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.