1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Accessible\Reader; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Inflector\Inflector; |
6
|
|
|
|
7
|
|
|
class AssociationReader extends Reader |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The name of the annotation classes that define an association. |
11
|
|
|
* |
12
|
|
|
* @var array<string> |
13
|
|
|
*/ |
14
|
|
|
private static $associationAnnotationClasses = array( |
15
|
|
|
"inverted" => "Accessible\\Annotation\\Referenced", |
16
|
|
|
"mapped" => "Accessible\\Annotation\\InCollection", |
17
|
|
|
); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get a list linking a property with association between the property's class and the current class. |
21
|
|
|
* Ex: ["products" => ["property" => "cart", "association" => "inverted"]] |
22
|
|
|
* |
23
|
|
|
* @param array $properties The properties of the object to read. |
24
|
|
|
* @param \Doctrine\Common\Annotations\Reader $annotationReader The annotation reader to use. |
25
|
|
|
* |
26
|
|
|
* @return array The described list. |
27
|
|
|
*/ |
28
|
7 |
|
public static function getAssociations($properties, $annotationReader) |
29
|
|
|
{ |
30
|
7 |
|
$objectAssociations = array(); |
31
|
|
|
|
32
|
7 |
|
foreach ($properties as $propertyName => $property) { |
33
|
7 |
|
$annotation = null; |
34
|
7 |
|
$associationType = null; |
35
|
7 |
|
foreach (self::$associationAnnotationClasses as $annotationAssociationType => $annotationClass) { |
36
|
7 |
|
$annotation = $annotationReader->getPropertyAnnotation($property, $annotationClass); |
37
|
7 |
|
if ($annotation !== null) { |
38
|
6 |
|
$associationType = $annotationAssociationType; |
39
|
6 |
|
break; |
40
|
|
|
} |
41
|
7 |
|
} |
42
|
|
|
|
43
|
7 |
|
if ($annotation !== null) { |
44
|
|
|
// get the item name |
45
|
6 |
|
$associatedPropertyName = $annotation->getAssociatedProperty(); |
46
|
|
|
|
47
|
6 |
|
$objectAssociations[$propertyName] = array( |
48
|
6 |
|
"property" => $associatedPropertyName, |
49
|
|
|
"association" => $associationType |
50
|
6 |
|
); |
51
|
|
|
|
52
|
|
|
// if this is a mapped association, get the item name of the current class |
53
|
6 |
|
if ($associationType === "mapped") { |
54
|
6 |
|
$associatedClass = new \ReflectionClass($annotation->getClassName()); |
55
|
6 |
|
$associatedPropertyReflection = $associatedClass->getProperty($associatedPropertyName); |
56
|
|
|
|
57
|
6 |
|
$itemName = null; |
58
|
6 |
|
foreach (self::$collectionAnnotationClasses as $annotationBehavior => $annotationClass) { |
59
|
6 |
|
$collectionAnnotation = $annotationReader->getPropertyAnnotation($associatedPropertyReflection, $annotationClass); |
60
|
6 |
|
if ($collectionAnnotation !== null) { |
61
|
6 |
|
$itemName = $collectionAnnotation->getItemName(); |
62
|
6 |
|
break; |
63
|
|
|
} |
64
|
6 |
|
} |
65
|
6 |
|
if ($itemName === null) { |
66
|
6 |
|
$itemName = Inflector::singularize($associatedPropertyName); |
67
|
6 |
|
} |
68
|
|
|
|
69
|
6 |
|
$objectAssociations[$propertyName]["itemName"] = $itemName; |
70
|
6 |
|
} |
71
|
6 |
|
} else { |
72
|
7 |
|
$objectAssociations[$propertyName] = null; |
73
|
|
|
} |
74
|
7 |
|
} |
75
|
|
|
|
76
|
7 |
|
return $objectAssociations; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|