1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP version 7.1 |
4
|
|
|
* |
5
|
|
|
* This source file is subject to the license that is bundled with this package in the file LICENSE. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace PhUml\Graphviz\Builders; |
9
|
|
|
|
10
|
|
|
use PhUml\Code\ClassDefinition; |
11
|
|
|
use PhUml\Code\Codebase; |
12
|
|
|
use PhUml\Code\Variables\Variable; |
13
|
|
|
use PhUml\Graphviz\Edge; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* It creates edges by inspecting a class |
17
|
|
|
* |
18
|
|
|
* 1. It creates edges by inspecting the attributes of a class |
19
|
|
|
* 2. It creates edges by inspecting the parameters of the constructor of a class |
20
|
|
|
*/ |
21
|
|
|
class EdgesBuilder implements AssociationsBuilder |
22
|
|
|
{ |
23
|
|
|
/** @var bool[] */ |
24
|
|
|
private $associations = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* It creates an edge if the attribute |
28
|
|
|
* |
29
|
|
|
* - Has type information and it's not a PHP's built-in type |
30
|
|
|
* - The association hasn't already been resolved |
31
|
|
|
* |
32
|
|
|
* @return \PhUml\Graphviz\HasDotRepresentation[] |
33
|
|
|
*/ |
34
|
63 |
|
public function fromAttributes(ClassDefinition $class, Codebase $codebase): array |
35
|
|
|
{ |
36
|
63 |
|
return $this->buildEdgesFor($class, $class->attributes(), $codebase); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* It creates an edge if the constructor parameter |
41
|
|
|
* |
42
|
|
|
* - Has type information and it's not a PHP's built-in type |
43
|
|
|
* - The association hasn't already been resolved |
44
|
|
|
* |
45
|
|
|
* @return \PhUml\Graphviz\HasDotRepresentation[] |
46
|
|
|
*/ |
47
|
63 |
|
public function fromConstructor(ClassDefinition $class, Codebase $codebase): array |
48
|
|
|
{ |
49
|
63 |
|
return $this->buildEdgesFor($class, $class->constructorParameters(), $codebase); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Variable[] $variables |
54
|
|
|
* @return Edge[] |
55
|
|
|
*/ |
56
|
63 |
|
private function buildEdgesFor(ClassDefinition $class, array $variables, Codebase $codebase): array |
57
|
|
|
{ |
58
|
63 |
|
$edges = []; |
59
|
63 |
|
foreach ($variables as $parameter) { |
60
|
54 |
|
if (!$this->needAssociation($class, $parameter)) { |
61
|
39 |
|
continue; |
62
|
|
|
} |
63
|
48 |
|
$edges[] = $this->addAssociation($class, $parameter, $codebase); |
64
|
|
|
} |
65
|
63 |
|
return $edges; |
66
|
|
|
} |
67
|
|
|
|
68
|
48 |
|
private function addAssociation(ClassDefinition $class, Variable $attribute, Codebase $codebase): Edge |
69
|
|
|
{ |
70
|
48 |
|
$this->markAssociationResolvedFor($class, $attribute); |
71
|
|
|
|
72
|
48 |
|
return Edge::association($codebase->get($attribute->referenceName()), $class); |
73
|
|
|
} |
74
|
|
|
|
75
|
54 |
|
private function needAssociation(ClassDefinition $class, Variable $attribute): bool |
76
|
|
|
{ |
77
|
54 |
|
return $attribute->isAReference() && !$this->isAssociationResolved($class, $attribute); |
78
|
|
|
} |
79
|
|
|
|
80
|
48 |
|
private function isAssociationResolved(ClassDefinition $class, Variable $attribute): bool |
81
|
|
|
{ |
82
|
48 |
|
return array_key_exists($this->associationKey($class, $attribute), $this->associations); |
83
|
|
|
} |
84
|
|
|
|
85
|
48 |
|
private function markAssociationResolvedFor(ClassDefinition $class, Variable $attribute): void |
86
|
|
|
{ |
87
|
48 |
|
$this->associations[$this->associationKey($class, $attribute)] = true; |
88
|
48 |
|
} |
89
|
|
|
|
90
|
48 |
|
private function associationKey(ClassDefinition $class, Variable $attribute): string |
91
|
|
|
{ |
92
|
48 |
|
return strtolower($class->name() . '.' . $attribute->type()); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|