1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace gossi\codegen\model\parts; |
5
|
|
|
|
6
|
|
|
use gossi\codegen\model\PhpTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Traits part |
10
|
|
|
* |
11
|
|
|
* For all models that can have traits |
12
|
|
|
* |
13
|
|
|
* @author Thomas Gossmann |
14
|
|
|
*/ |
15
|
|
|
trait TraitsPart { |
16
|
|
|
|
17
|
|
|
/** @var string[] */ |
18
|
|
|
private $traits = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Adds a use statement with an optional alias |
22
|
|
|
* |
23
|
|
|
* @param string $qualifiedName |
24
|
|
|
* @param null|string $alias |
25
|
|
|
* @return $this |
26
|
|
|
*/ |
27
|
|
|
abstract public function addUseStatement($qualifiedName, string $alias = null); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Removes a use statement |
31
|
|
|
* |
32
|
|
|
* @param string $qualifiedName |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
|
|
abstract public function removeUseStatement(string $qualifiedName); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns the namespace |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
abstract public function getNamespace(): ?string; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Adds a trait. |
46
|
|
|
* |
47
|
|
|
* If the trait is passed as PhpTrait object, |
48
|
|
|
* the trait is also added as use statement. |
49
|
|
|
* |
50
|
|
|
* @param PhpTrait|string $trait trait or qualified name |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
4 |
|
public function addTrait($trait) { |
54
|
4 |
|
if ($trait instanceof PhpTrait) { |
55
|
1 |
|
$name = $trait->getName(); |
56
|
1 |
|
$qname = $trait->getQualifiedName(); |
57
|
1 |
|
$this->addUseStatement($qname); |
58
|
|
|
} else { |
59
|
4 |
|
$name = $trait; |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
if (!in_array($name, $this->traits, true)) { |
63
|
4 |
|
$this->traits[] = $name; |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns a collection of traits |
71
|
|
|
* |
72
|
|
|
* @return string[] |
73
|
|
|
*/ |
74
|
15 |
|
public function getTraits(): array { |
75
|
15 |
|
return $this->traits; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return iterable|PhpTrait[] |
80
|
|
|
*/ |
81
|
|
|
public function getPhpTraits(): iterable { |
82
|
|
|
$traits = []; |
83
|
|
|
foreach ($this->traits as $trait) { |
84
|
|
|
$traits[] = trait_exists($trait) ? |
85
|
|
|
PhpTrait::fromName($trait) : PhpTrait::create($trait); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $traits; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Checks whether a trait exists |
93
|
|
|
* |
94
|
|
|
* @param PhpTrait|string $trait |
95
|
|
|
* @return bool `true` if it exists and `false` if not |
96
|
|
|
*/ |
97
|
2 |
|
public function hasTrait($trait): bool { |
98
|
2 |
|
if (!$trait instanceof PhpTrait) { |
99
|
2 |
|
$trait = new PhpTrait($trait); |
100
|
|
|
} |
101
|
2 |
|
$name = $trait->getName(); |
102
|
2 |
|
return in_array($name, $this->traits); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Removes a trait. |
107
|
|
|
* |
108
|
|
|
* If the trait is passed as PhpTrait object, |
109
|
|
|
* the trait is also removed from use statements. |
110
|
|
|
* |
111
|
|
|
* @param PhpTrait|string $trait trait or qualified name |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
1 |
|
public function removeTrait($trait) { |
115
|
1 |
|
if ($trait instanceof PhpTrait) { |
116
|
1 |
|
$name = $trait->getName(); |
117
|
|
|
} else { |
118
|
1 |
|
$name = $trait; |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
$index = array_search($name, $this->traits); |
122
|
1 |
|
if ($index !== false) { |
123
|
1 |
|
unset($this->traits[$index]); |
124
|
|
|
|
125
|
1 |
|
if ($trait instanceof PhpTrait) { |
126
|
1 |
|
$qname = $trait->getQualifiedName(); |
127
|
1 |
|
$this->removeUseStatement($qname); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Sets a collection of traits |
136
|
|
|
* |
137
|
|
|
* @param PhpTrait[] $traits |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
1 |
|
public function setTraits(array $traits) { |
141
|
1 |
|
foreach ($traits as $trait) { |
142
|
1 |
|
$this->addTrait($trait); |
143
|
|
|
} |
144
|
|
|
|
145
|
1 |
|
return $this; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|