|
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; |
|
9
|
|
|
|
|
10
|
|
|
use PhUml\Code\ClassDefinition; |
|
11
|
|
|
use PhUml\Code\Variable; |
|
12
|
|
|
|
|
13
|
|
|
class ClassGraphElements |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var HasDotRepresentation[] */ |
|
16
|
|
|
private $dotElements = []; |
|
17
|
|
|
|
|
18
|
|
|
/** @var bool[] */ |
|
19
|
|
|
private $associations = []; |
|
20
|
|
|
|
|
21
|
|
|
/** @var bool */ |
|
22
|
|
|
private $createAssociations; |
|
23
|
|
|
|
|
24
|
|
|
/** @var NodeLabelBuilder */ |
|
25
|
|
|
private $labelBuilder; |
|
26
|
|
|
|
|
27
|
|
|
/** @var HasNodeIdentifier[] */ |
|
28
|
|
|
private $structure; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(bool $createAssociations, NodeLabelBuilder $labelBuilder) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->createAssociations = $createAssociations; |
|
33
|
|
|
$this->labelBuilder = $labelBuilder; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param HasNodeIdentifier[] $structure |
|
38
|
|
|
* @return HasDotRepresentation[] |
|
39
|
|
|
*/ |
|
40
|
|
|
public function extractFrom(ClassDefinition $class, array $structure): array |
|
41
|
|
|
{ |
|
42
|
|
|
$this->dotElements = []; |
|
43
|
|
|
$this->associations = []; |
|
44
|
|
|
$this->structure = $structure; |
|
45
|
|
|
|
|
46
|
|
|
if ($this->createAssociations) { |
|
47
|
|
|
$this->addElementsForAttributes($class); |
|
48
|
|
|
$this->addElementsForParameters($class); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->dotElements[] = new Node($class, $this->labelBuilder->labelForClass($class)); |
|
52
|
|
|
|
|
53
|
|
|
if ($class->hasParent()) { |
|
54
|
|
|
$this->dotElements[] = Edge::inheritance($class->extends, $class); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
foreach ($class->implements as $interface) { |
|
58
|
|
|
$this->dotElements[] = Edge::implementation($interface, $class); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this->dotElements; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** @return HasDotRepresentation[] */ |
|
65
|
|
|
private function addElementsForAttributes(ClassDefinition $class): void |
|
66
|
|
|
{ |
|
67
|
|
|
/** @var \PhUml\Code\Attribute $attribute */ |
|
68
|
|
|
foreach ($class->attributes as $attribute) { |
|
69
|
|
|
$this->addAssociationForVariable($class, $attribute); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** @return HasDotRepresentation[] */ |
|
74
|
|
|
private function addElementsForParameters(ClassDefinition $class): void |
|
75
|
|
|
{ |
|
76
|
|
|
if (!$class->hasConstructor()) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
foreach ($class->constructorParameters() as $parameter) { |
|
80
|
|
|
$this->addAssociationForVariable($class, $parameter); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function addAssociationForVariable(ClassDefinition $class, Variable $attribute): void |
|
85
|
|
|
{ |
|
86
|
|
|
if ($this->needAssociation($attribute)) { |
|
87
|
|
|
$this->dotElements[] = Edge::association($this->structure[(string)$attribute->type], $class); |
|
88
|
|
|
$this->associations[strtolower($attribute->type)] = true; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function needAssociation(Variable $attribute): bool |
|
93
|
|
|
{ |
|
94
|
|
|
return $attribute->hasType() && !$attribute->isBuiltIn() && !$this->isAssociationResolved($attribute->type); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private function isAssociationResolved(string $type): bool |
|
98
|
|
|
{ |
|
99
|
|
|
return array_key_exists(strtolower($type), $this->associations); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|