1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Leaditin\Code\Generator; |
4
|
|
|
|
5
|
|
|
use Leaditin\Code\Flag; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @package Leaditin\Code |
9
|
|
|
* @author Igor Vuckovic <[email protected]> |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
class ClassGenerator extends ClassAwareGenerator |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Flag |
16
|
|
|
*/ |
17
|
|
|
protected $flag; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string[] |
21
|
|
|
*/ |
22
|
|
|
protected $interfaces = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string[] |
26
|
|
|
*/ |
27
|
|
|
protected $traits = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritDoc |
31
|
|
|
*/ |
32
|
9 |
|
public function __construct( |
33
|
|
|
ConstantGenerator $constantGenerator, |
34
|
|
|
DocBlockGenerator $docBlockGenerator, |
35
|
|
|
ImportGenerator $importGenerator, |
36
|
|
|
MethodGenerator $methodGenerator, |
37
|
|
|
PropertyGenerator $propertyGenerator |
38
|
|
|
) { |
39
|
9 |
|
parent::__construct($constantGenerator, $docBlockGenerator, $importGenerator, $methodGenerator, $propertyGenerator); |
40
|
|
|
|
41
|
9 |
|
$this->flag = new Flag(); |
42
|
9 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param bool $isAbstract |
46
|
|
|
* |
47
|
|
|
* @return ClassGenerator |
48
|
|
|
*/ |
49
|
1 |
|
public function setAbstract(bool $isAbstract): self |
50
|
|
|
{ |
51
|
1 |
|
$isAbstract ? $this->flag->addFlag(Flag::FLAG_ABSTRACT) : $this->flag->removeFlag(Flag::FLAG_ABSTRACT); |
52
|
|
|
|
53
|
1 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param bool $isFinal |
58
|
|
|
* |
59
|
|
|
* @return static |
60
|
|
|
*/ |
61
|
1 |
|
public function setFinal(bool $isFinal): self |
62
|
|
|
{ |
63
|
1 |
|
$isFinal ? $this->flag->addFlag(Flag::FLAG_FINAL) : $this->flag->removeFlag(Flag::FLAG_FINAL); |
64
|
|
|
|
65
|
1 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $fullyQualifiedInterfaceName |
70
|
|
|
* |
71
|
|
|
* @return static |
72
|
|
|
*/ |
73
|
1 |
|
public function implementInterface(string $fullyQualifiedInterfaceName): self |
74
|
|
|
{ |
75
|
1 |
|
$fullyQualifiedInterfaceName = trim($fullyQualifiedInterfaceName); |
76
|
|
|
|
77
|
1 |
|
$this->interfaces[$fullyQualifiedInterfaceName] = $fullyQualifiedInterfaceName; |
78
|
|
|
|
79
|
1 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $fullyQualifiedTraitName |
84
|
|
|
* |
85
|
|
|
* @return static |
86
|
|
|
*/ |
87
|
1 |
|
public function useTrait(string $fullyQualifiedTraitName): self |
88
|
|
|
{ |
89
|
1 |
|
$fullyQualifiedTraitName = trim($fullyQualifiedTraitName); |
90
|
|
|
|
91
|
1 |
|
$this->traits[$fullyQualifiedTraitName] = $fullyQualifiedTraitName; |
92
|
|
|
|
93
|
1 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritDoc |
98
|
|
|
*/ |
99
|
4 |
|
protected function scope(): string |
100
|
|
|
{ |
101
|
4 |
|
return 'class'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @inheritDoc |
106
|
|
|
*/ |
107
|
9 |
|
protected function generateScope(): string |
108
|
|
|
{ |
109
|
9 |
|
return $this->generateLine("{$this->generateFlags()} $this->name{$this->generateInheritance()}{$this->generateImplements()}") . $this->generateLine('{') . $this->generateUses(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
9 |
|
protected function generateFlags(): string |
116
|
|
|
{ |
117
|
9 |
|
if ($this->flag->hasFlag(Flag::FLAG_FINAL)) { |
118
|
1 |
|
return 'final class'; |
119
|
|
|
} |
120
|
|
|
|
121
|
8 |
|
if ($this->flag->hasFlag(Flag::FLAG_ABSTRACT)) { |
122
|
1 |
|
return 'abstract class'; |
123
|
|
|
} |
124
|
|
|
|
125
|
7 |
|
return 'class'; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
9 |
|
protected function generateImplements(): string |
132
|
|
|
{ |
133
|
9 |
|
if ($this->interfaces === []) { |
134
|
8 |
|
return ''; |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
return ' implements ' . implode(', ', $this->interfaces); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
9 |
|
protected function generateUses(): string |
144
|
|
|
{ |
145
|
9 |
|
if ($this->traits === []) { |
146
|
8 |
|
return ''; |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
return $this->generateLine('use ' . implode(', ', $this->traits) . ';', 1, 1); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|