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