1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Accessible\Reader; |
4
|
|
|
|
5
|
|
|
class AutoConstructReader extends Reader |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* The name of the annotation class that define the construct arguments. |
9
|
|
|
* |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
private static $constructAnnotationClass = "Accessible\\Annotation\\Construct"; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The name of the annotation class that define a property's default value. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private static $initializeAnnotationClass = "Accessible\\Annotation\\Initialize"; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The name of the annotation class that define the initial value of an object property. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private static $initializeObjectAnnotationClass = "Accessible\\Annotation\\InitializeObject"; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get the list of needed arguments for given object's constructor. |
30
|
|
|
* |
31
|
|
|
* @param array $objectClasses The classes of the object to read. |
32
|
|
|
* @param \Doctrine\Common\Annotations\Reader $annotationReader The annotation reader to use. |
33
|
|
|
* |
34
|
|
|
* @return array The list of arguments. |
35
|
|
|
*/ |
36
|
7 |
|
public static function getConstructArguments($objectClasses, $annotationReader) |
37
|
|
|
{ |
38
|
7 |
|
$constructArguments = null; |
39
|
7 |
|
array_reverse($objectClasses); |
40
|
|
|
|
41
|
7 |
|
foreach ($objectClasses as $class) { |
42
|
7 |
|
$annotation = $annotationReader->getClassAnnotation($class, self::$constructAnnotationClass); |
43
|
7 |
|
if ($annotation !== null) { |
44
|
2 |
|
$constructArguments = $annotation->getArguments(); |
45
|
2 |
|
break; |
46
|
|
|
} |
47
|
7 |
|
} |
48
|
|
|
|
49
|
7 |
|
return $constructArguments; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the list of properties that have to be initialized automatically |
54
|
|
|
* during the object construction, plus their value. |
55
|
|
|
* |
56
|
|
|
* @param array $properties The properties of the object to read. |
57
|
|
|
* @param \Doctrine\Common\Annotations\Reader $annotationReader The annotation reader to use. |
58
|
|
|
* |
59
|
|
|
* @return array The list of properties and values, |
60
|
|
|
* in the form ["property" => "value"]. |
61
|
|
|
*/ |
62
|
7 |
|
public static function getPropertiesToInitialize($properties, $annotationReader) |
63
|
|
|
{ |
64
|
7 |
|
$propertiesValues = array(); |
65
|
|
|
|
66
|
7 |
|
foreach ($properties as $propertyName => $property) { |
67
|
7 |
|
$initializeAnnotation = $annotationReader->getPropertyAnnotation($property, self::$initializeAnnotationClass); |
68
|
7 |
|
$initializeObjectAnnotation = $annotationReader->getPropertyAnnotation($property, self::$initializeObjectAnnotationClass); |
69
|
|
|
|
70
|
7 |
|
if ($initializeAnnotation !== null && $initializeObjectAnnotation !== null) { |
71
|
|
|
throw new \LogicException("Two initial values are given for property $propertyName."); |
72
|
|
|
} |
73
|
|
|
|
74
|
7 |
|
if ($initializeAnnotation !== null) { |
75
|
6 |
|
$propertiesValues[$propertyName] = array( |
76
|
6 |
|
'type' => 'initialize', |
77
|
6 |
|
'value' => $initializeAnnotation->getValue() |
78
|
6 |
|
); |
79
|
7 |
|
} else if ($initializeObjectAnnotation !== null) { |
80
|
5 |
|
$propertiesValues[$propertyName] = array( |
81
|
5 |
|
'type' => 'initializeObject', |
82
|
5 |
|
'value' => $initializeObjectAnnotation->getClassName() |
83
|
5 |
|
); |
84
|
5 |
|
} |
85
|
7 |
|
} |
86
|
|
|
|
87
|
7 |
|
return $propertiesValues; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|