|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace gossi\codegen\model\parts; |
|
5
|
|
|
|
|
6
|
|
|
use gossi\codegen\model\PhpProperty; |
|
7
|
|
|
use phootwork\collection\Map; |
|
8
|
|
|
use phootwork\collection\Set; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Properties part |
|
12
|
|
|
* |
|
13
|
|
|
* For all models that can have properties |
|
14
|
|
|
* |
|
15
|
|
|
* @author Thomas Gossmann |
|
16
|
|
|
*/ |
|
17
|
|
|
trait PropertiesPart { |
|
18
|
|
|
|
|
19
|
|
|
/** @var Map|PhpProperty[] */ |
|
20
|
|
|
private $properties; |
|
21
|
|
|
|
|
22
|
51 |
|
private function initProperties() { |
|
23
|
51 |
|
$this->properties = new Map(); |
|
24
|
51 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Sets a collection of properties |
|
28
|
|
|
* |
|
29
|
|
|
* @param PhpProperty[] $properties |
|
30
|
|
|
* @return $this |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function setProperties(array $properties) { |
|
33
|
1 |
|
foreach ($this->properties as $prop) { |
|
34
|
1 |
|
$prop->setParent(null); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
$this->properties->clear(); |
|
38
|
|
|
|
|
39
|
1 |
|
foreach ($properties as $prop) { |
|
40
|
1 |
|
$this->addProperty($prop); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Adds a property |
|
48
|
|
|
* |
|
49
|
|
|
* @param PhpProperty $property |
|
50
|
|
|
* @return $this |
|
51
|
|
|
*/ |
|
52
|
11 |
|
public function addProperty(PhpProperty $property) { |
|
53
|
11 |
|
$property->setParent($this); |
|
54
|
11 |
|
$types = $property->getTypes(); |
|
55
|
|
|
|
|
56
|
11 |
|
if ($types) { |
|
57
|
11 |
|
foreach ($types as $type) { |
|
58
|
5 |
|
$this->addUseStatement($type); |
|
|
|
|
|
|
59
|
5 |
|
$property->addType($type); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
11 |
|
$this->properties->set($property->getName(), $property); |
|
64
|
|
|
|
|
65
|
11 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Removes a property |
|
70
|
|
|
* |
|
71
|
|
|
* @param PhpProperty|string $nameOrProperty property name or instance |
|
72
|
|
|
* @throws \InvalidArgumentException If the property cannot be found |
|
73
|
|
|
* @return $this |
|
74
|
|
|
*/ |
|
75
|
2 |
|
public function removeProperty($nameOrProperty) { |
|
76
|
2 |
|
if ($nameOrProperty instanceof PhpProperty) { |
|
77
|
1 |
|
$nameOrProperty = $nameOrProperty->getName(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
if (!$this->properties->has($nameOrProperty)) { |
|
81
|
1 |
|
throw new \InvalidArgumentException(sprintf('The property "%s" does not exist.', $nameOrProperty)); |
|
82
|
|
|
} |
|
83
|
1 |
|
$p = $this->properties->get($nameOrProperty); |
|
84
|
1 |
|
$p->setParent(null); |
|
85
|
1 |
|
$this->properties->remove($nameOrProperty); |
|
86
|
|
|
|
|
87
|
1 |
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Checks whether a property exists |
|
92
|
|
|
* |
|
93
|
|
|
* @param PhpProperty|string $nameOrProperty property name or instance |
|
94
|
|
|
* @return bool `true` if a property exists and `false` if not |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function hasProperty($nameOrProperty): bool { |
|
97
|
1 |
|
if ($nameOrProperty instanceof PhpProperty) { |
|
98
|
1 |
|
$nameOrProperty = $nameOrProperty->getName(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
return $this->properties->has($nameOrProperty); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Returns a property |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $nameOrProperty property name |
|
108
|
|
|
* @throws \InvalidArgumentException If the property cannot be found |
|
109
|
|
|
* @return PhpProperty |
|
110
|
|
|
*/ |
|
111
|
5 |
|
public function getProperty($nameOrProperty): PhpProperty { |
|
112
|
5 |
|
if ($nameOrProperty instanceof PhpProperty) { |
|
113
|
1 |
|
$nameOrProperty = $nameOrProperty->getName(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
5 |
|
if (!$this->properties->has($nameOrProperty)) { |
|
117
|
1 |
|
throw new \InvalidArgumentException(sprintf('The property "%s" does not exist.', $nameOrProperty)); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
5 |
|
return $this->properties->get($nameOrProperty); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns a collection of properties |
|
125
|
|
|
* |
|
126
|
|
|
* @return Map|PhpProperty[] |
|
127
|
|
|
*/ |
|
128
|
15 |
|
public function getProperties(): Map { |
|
129
|
15 |
|
return $this->properties; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Returns all property names |
|
134
|
|
|
* |
|
135
|
|
|
* @return Set |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public function getPropertyNames(): Set { |
|
138
|
1 |
|
return $this->properties->keys(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Clears all properties |
|
143
|
|
|
* |
|
144
|
|
|
* @return $this |
|
145
|
|
|
*/ |
|
146
|
1 |
|
public function clearProperties() { |
|
147
|
1 |
|
foreach ($this->properties as $property) { |
|
148
|
1 |
|
$property->setParent(null); |
|
149
|
|
|
} |
|
150
|
1 |
|
$this->properties->clear(); |
|
151
|
|
|
|
|
152
|
1 |
|
return $this; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.