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\Structure; |
12
|
|
|
use PhUml\Code\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
|
|
|
public function fromAttributes(ClassDefinition $class, Structure $structure): array |
35
|
|
|
{ |
36
|
24 |
|
return array_map(function (Variable $attribute) use ($class, $structure) { |
37
|
6 |
|
return $this->addAssociation($class, $attribute, $structure); |
38
|
24 |
|
}, array_filter($class->attributes(), [$this, 'needAssociation'])); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* It creates an edge if the constructor parameter |
43
|
|
|
* |
44
|
|
|
* - Has type information and it's not a PHP's built-in type |
45
|
|
|
* - The association hasn't already been resolved |
46
|
|
|
* |
47
|
|
|
* @return \PhUml\Graphviz\HasDotRepresentation[] |
48
|
|
|
*/ |
49
|
24 |
|
public function fromConstructor(ClassDefinition $class, Structure $structure): array |
50
|
|
|
{ |
51
|
24 |
|
if (!$class->hasConstructor()) { |
52
|
18 |
|
return []; |
53
|
|
|
} |
54
|
21 |
|
return array_map(function (Variable $attribute) use ($class, $structure) { |
55
|
9 |
|
return $this->addAssociation($class, $attribute, $structure); |
56
|
21 |
|
}, array_filter($class->constructorParameters(), [$this, 'needAssociation'])); |
57
|
|
|
} |
58
|
|
|
|
59
|
12 |
|
private function addAssociation(ClassDefinition $class, Variable $attribute, Structure $structure): Edge |
60
|
|
|
{ |
61
|
12 |
|
$this->markAssociationResolvedFor($attribute); |
62
|
12 |
|
return Edge::association( |
63
|
12 |
|
$structure->get((string)$attribute->type()), |
64
|
12 |
|
$class |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
24 |
|
private function needAssociation(Variable $attribute): bool |
69
|
|
|
{ |
70
|
24 |
|
return $attribute->isAReference() && !$this->isAssociationResolved($attribute->type()); |
71
|
|
|
} |
72
|
|
|
|
73
|
12 |
|
private function isAssociationResolved(string $type): bool |
74
|
|
|
{ |
75
|
12 |
|
return array_key_exists(strtolower($type), $this->associations); |
76
|
|
|
} |
77
|
|
|
|
78
|
12 |
|
private function markAssociationResolvedFor(Variable $attribute): void |
79
|
|
|
{ |
80
|
12 |
|
$this->associations[strtolower($attribute->type())] = true; |
81
|
12 |
|
} |
82
|
|
|
} |
83
|
|
|
|