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