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