1
|
|
|
<?php |
2
|
|
|
namespace gossi\codegen\model\parts; |
3
|
|
|
|
4
|
|
|
use gossi\codegen\model\PhpProperty; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Properties part |
8
|
|
|
* |
9
|
|
|
* For all models that can have properties |
10
|
|
|
* |
11
|
|
|
* @author Thomas Gossmann |
12
|
|
|
*/ |
13
|
|
|
trait PropertiesPart { |
14
|
|
|
|
15
|
|
|
/** @var PhpProperty[] */ |
16
|
|
|
private $properties = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Sets a collection of properties |
20
|
|
|
* |
21
|
|
|
* @param PhpProperty[] $properties |
22
|
|
|
* @return $this |
23
|
|
|
*/ |
24
|
1 |
|
public function setProperties(array $properties) { |
25
|
1 |
|
foreach ($this->properties as $prop) { |
26
|
1 |
|
$prop->setParent(null); |
27
|
1 |
|
} |
28
|
|
|
|
29
|
1 |
|
$this->properties = []; |
30
|
|
|
|
31
|
1 |
|
foreach ($properties as $prop) { |
32
|
1 |
|
$this->setProperty($prop); |
33
|
1 |
|
} |
34
|
|
|
|
35
|
1 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Adds a property |
40
|
|
|
* |
41
|
|
|
* @param PhpProperty $property |
42
|
|
|
* @return $this |
43
|
|
|
*/ |
44
|
14 |
|
public function setProperty(PhpProperty $property) { |
45
|
14 |
|
$property->setParent($this); |
46
|
14 |
|
$this->properties[$property->getName()] = $property; |
47
|
|
|
|
48
|
14 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Removes a property |
53
|
|
|
* |
54
|
|
|
* @param PhpProperty|string $nameOrProperty property name or instance |
55
|
|
|
* @throws \InvalidArgumentException If the property cannot be found |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
2 |
|
public function removeProperty($nameOrProperty) { |
59
|
2 |
|
if ($nameOrProperty instanceof PhpProperty) { |
60
|
1 |
|
$nameOrProperty = $nameOrProperty->getName(); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
2 |
|
if (!array_key_exists($nameOrProperty, $this->properties)) { |
64
|
1 |
|
throw new \InvalidArgumentException(sprintf('The property "%s" does not exist.', $nameOrProperty)); |
65
|
|
|
} |
66
|
1 |
|
$p = $this->properties[$nameOrProperty]; |
67
|
1 |
|
$p->setParent(null); |
68
|
1 |
|
unset($this->properties[$nameOrProperty]); |
69
|
|
|
|
70
|
1 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Checks whether a property exists |
75
|
|
|
* |
76
|
|
|
* @param PhpProperty|string $nameOrProperty property name or instance |
77
|
|
|
* @return bool `true` if a property exists and `false` if not |
78
|
|
|
*/ |
79
|
1 |
|
public function hasProperty($nameOrProperty) { |
80
|
1 |
|
if ($nameOrProperty instanceof PhpProperty) { |
81
|
1 |
|
$nameOrProperty = $nameOrProperty->getName(); |
82
|
1 |
|
} |
83
|
|
|
|
84
|
1 |
|
return isset($this->properties[$nameOrProperty]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns a property |
89
|
|
|
* |
90
|
|
|
* @param string $nameOrProperty property name |
91
|
|
|
* @throws \InvalidArgumentException If the property cannot be found |
92
|
|
|
* @return PhpProperty |
93
|
|
|
*/ |
94
|
4 |
|
public function getProperty($nameOrProperty) { |
95
|
4 |
|
if ($nameOrProperty instanceof PhpProperty) { |
96
|
1 |
|
$nameOrProperty = $nameOrProperty->getName(); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
4 |
|
if (!array_key_exists($nameOrProperty, $this->properties)) { |
100
|
1 |
|
throw new \InvalidArgumentException(sprintf('The property "%s" does not exist.', $nameOrProperty)); |
101
|
|
|
} |
102
|
|
|
|
103
|
4 |
|
return $this->properties[$nameOrProperty]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns a collection of properties |
108
|
|
|
* |
109
|
|
|
* @return PhpProperty[] |
110
|
|
|
*/ |
111
|
13 |
|
public function getProperties() { |
112
|
13 |
|
return $this->properties; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns all property names |
117
|
|
|
* |
118
|
|
|
* @return string[] |
119
|
|
|
*/ |
120
|
1 |
|
public function getPropertyNames() { |
121
|
1 |
|
return array_keys($this->properties); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Clears all properties |
126
|
|
|
* |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
1 |
|
public function clearProperties() { |
130
|
1 |
|
foreach ($this->properties as $property) { |
131
|
1 |
|
$property->setParent(null); |
132
|
1 |
|
} |
133
|
1 |
|
$this->properties = []; |
134
|
|
|
|
135
|
1 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|