1
|
|
|
<?php |
2
|
|
|
namespace micro\orm; |
3
|
|
|
require_once ROOT.DS.'micro/addendum/annotations.php'; |
4
|
|
|
|
5
|
|
|
require_once ROOT.DS.'micro/annotations/BaseAnnotation.php'; |
6
|
|
|
require_once ROOT.DS.'micro/annotations/Column.php'; |
7
|
|
|
require_once ROOT.DS.'micro/annotations/Transient.php'; |
8
|
|
|
require_once ROOT.DS.'micro/annotations/Table.php'; |
9
|
|
|
require_once ROOT.DS.'micro/annotations/Id.php'; |
10
|
|
|
require_once ROOT.DS.'micro/annotations/ManyToOne.php'; |
11
|
|
|
require_once ROOT.DS.'micro/annotations/JoinColumn.php'; |
12
|
|
|
require_once ROOT.DS.'micro/annotations/OneToMany.php'; |
13
|
|
|
require_once ROOT.DS.'micro/annotations/ManyToMany.php'; |
14
|
|
|
require_once ROOT.DS.'micro/annotations/JoinTable.php'; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Utilitaires de Reflexion |
19
|
|
|
* @author jc |
20
|
|
|
* @version 1.0.0.1 |
21
|
|
|
* @package orm |
22
|
|
|
*/ |
23
|
|
|
class Reflexion{ |
24
|
|
|
public static function getProperties($instance){ |
25
|
|
|
$reflect = new \ReflectionClass($instance); |
26
|
|
|
$props = $reflect->getProperties(); |
27
|
|
|
return $props; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function getProperty($instance,$property){ |
31
|
|
|
$reflect = new \ReflectionClass($instance); |
32
|
|
|
$prop = $reflect->getProperty($property); |
33
|
|
|
return $prop; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function getPropertiesAndValues($instance,$props=NULL){ |
37
|
|
|
$ret=array(); |
38
|
|
|
$className=get_class($instance); |
39
|
|
|
if(is_null($props)) |
40
|
|
|
$props=Reflexion::getProperties($instance); |
41
|
|
|
foreach ($props as $prop){ |
42
|
|
|
$prop->setAccessible(true); |
43
|
|
|
$v=$prop->getValue($instance); |
44
|
|
|
if(OrmUtils::isSerializable($className,$prop->getName())){ |
45
|
|
|
if(($v!==null && $v!=="") || (($v===null || $v==="") && OrmUtils::isNullable($className, $prop->getName()))){ |
46
|
|
|
$name=OrmUtils::getFieldName($className, $prop->getName()); |
47
|
|
|
$ret[$name]=$v; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
return $ret; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function getAnnotationClass($class,$annotation){ |
55
|
|
|
$rac=new \ReflectionAnnotatedClass($class); |
56
|
|
|
$annot=$rac->getAnnotation($annotation); |
57
|
|
|
return $annot; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function getAnnotationMember($class,$member,$annotation){ |
61
|
|
|
$annot=false; |
62
|
|
|
$rap=new \ReflectionAnnotatedProperty($class, $member); |
63
|
|
|
if($rap!==null) |
64
|
|
|
$annot=$rap->getAnnotation($annotation); |
65
|
|
|
return $annot; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function getMembersWithAnnotation($class,$annotation){ |
69
|
|
|
$props=Reflexion::getProperties(new $class()); |
70
|
|
|
$ret=array(); |
71
|
|
|
foreach ($props as $prop){ |
72
|
|
|
$annot=Reflexion::getAnnotationMember($class, $prop->getName(), $annotation); |
73
|
|
|
if($annot!==FALSE) |
74
|
|
|
$ret[]=$prop; |
75
|
|
|
} |
76
|
|
|
return $ret; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|