|
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\Parser\Code\Builders; |
|
9
|
|
|
|
|
10
|
|
|
use PhpParser\Node\Name; |
|
11
|
|
|
use PhpParser\Node\Stmt\Class_; |
|
12
|
|
|
use PhUml\Code\ClassDefinition; |
|
13
|
|
|
use PhUml\Code\Name as ClassDefinitionName; |
|
14
|
|
|
use PhUml\Parser\Code\Builders\Members\AttributesBuilder; |
|
15
|
|
|
use PhUml\Parser\Code\Builders\Members\ConstantsBuilder; |
|
16
|
|
|
use PhUml\Parser\Code\Builders\Members\MethodsBuilder; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* It builds a `ClassDefinition` |
|
20
|
|
|
* |
|
21
|
|
|
* @see ConstantsBuilder for more details about the constants creation |
|
22
|
|
|
* @see AttributesBuilder for more details about the attributes creation |
|
23
|
|
|
* @see MethodsBuilder for more details about the methods creation |
|
24
|
|
|
*/ |
|
25
|
|
|
class ClassDefinitionBuilder |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var AttributesBuilder */ |
|
28
|
|
|
protected $attributesBuilder; |
|
29
|
|
|
|
|
30
|
|
|
/** @var MethodsBuilder */ |
|
31
|
|
|
protected $methodsBuilder; |
|
32
|
|
|
|
|
33
|
|
|
/** @var ConstantsBuilder */ |
|
34
|
|
|
protected $constantsBuilder; |
|
35
|
|
|
|
|
36
|
129 |
|
public function __construct( |
|
37
|
|
|
ConstantsBuilder $constantsBuilder = null, |
|
38
|
|
|
AttributesBuilder $attributesBuilder = null, |
|
39
|
|
|
MethodsBuilder $methodsBuilder = null |
|
40
|
|
|
) { |
|
41
|
129 |
|
$this->constantsBuilder = $constantsBuilder ?? new ConstantsBuilder(); |
|
42
|
129 |
|
$this->attributesBuilder = $attributesBuilder ?? new AttributesBuilder([]); |
|
43
|
129 |
|
$this->methodsBuilder = $methodsBuilder ?? new MethodsBuilder([]); |
|
44
|
129 |
|
} |
|
45
|
|
|
|
|
46
|
96 |
|
public function build(Class_ $class): ClassDefinition |
|
47
|
|
|
{ |
|
48
|
96 |
|
return new ClassDefinition( |
|
49
|
96 |
|
ClassDefinitionName::from($class->name), |
|
|
|
|
|
|
50
|
96 |
|
$this->constantsBuilder->build($class->stmts), |
|
51
|
96 |
|
$this->methodsBuilder->build($class->getMethods()), |
|
52
|
96 |
|
!empty($class->extends) ? ClassDefinitionName::from(end($class->extends->parts)) : null, |
|
53
|
96 |
|
$this->attributesBuilder->build($class->stmts), |
|
54
|
96 |
|
$this->buildInterfaces($class->implements) |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** @return ClassDefinitionName[] */ |
|
59
|
|
|
protected function buildInterfaces(array $implements): array |
|
60
|
|
|
{ |
|
61
|
102 |
|
return array_map(function (Name $name) { |
|
62
|
42 |
|
return ClassDefinitionName::from($name->getLast()); |
|
63
|
102 |
|
}, $implements); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|