|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xml\Impl\Type\Attribute; |
|
4
|
|
|
|
|
5
|
|
|
use Xml\Type\Attribute\AttributeInterface; |
|
6
|
|
|
use Xml\Type\ModelElementTypeInterface; |
|
7
|
|
|
use Xml\Instance\ModelElementInstanceInterface; |
|
8
|
|
|
use Xml\Type\Reference\ReferenceInterface; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AttributeImpl implements AttributeInterface |
|
11
|
|
|
{ |
|
12
|
|
|
private $attributeName; |
|
13
|
|
|
|
|
14
|
|
|
private $namespaceUri; |
|
15
|
|
|
|
|
16
|
|
|
private $defaultValue; |
|
17
|
|
|
|
|
18
|
|
|
private $isRequired = false; |
|
19
|
|
|
|
|
20
|
|
|
private $isIdAttribute = false; |
|
21
|
|
|
|
|
22
|
|
|
private $outgoingReferences = []; |
|
23
|
|
|
|
|
24
|
|
|
private $incomingReferences = []; |
|
25
|
|
|
|
|
26
|
|
|
private $owningElementType; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(ModelElementTypeInterface $owningElementType) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->owningElementType = $owningElementType; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return mixed |
|
35
|
|
|
*/ |
|
36
|
|
|
abstract protected function convertXmlValueToModelValue(?string $rawValue); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param mixed $modelValue; |
|
40
|
|
|
*/ |
|
41
|
|
|
abstract protected function convertModelValueToXmlValue($modelValue): ?string; |
|
42
|
|
|
|
|
43
|
|
|
public function getOwningElementType(): ModelElementTypeInterface |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->owningElementType; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getValue(ModelElementInstanceInterface $modelElement) |
|
52
|
|
|
{ |
|
53
|
|
|
if ($this->namespaceUri === null) { |
|
54
|
|
|
$value = $modelElement->getAttributeValue($this->attributeName); |
|
55
|
|
|
} else { |
|
56
|
|
|
$value = $modelElement->getAttributeValueNs($this->namespaceUri, $this->attributeName); |
|
57
|
|
|
if ($value === null) { |
|
58
|
|
|
$alternativeNamespaces = $this->owningElementType->getModel() |
|
59
|
|
|
->getAlternativeNamespaces($this->namespaceUri); |
|
60
|
|
|
foreach ($alternativeNamespaces as $namespace) { |
|
61
|
|
|
$value = $modelElement->getAttributeValueNs($namespace, $this->attributeName); |
|
62
|
|
|
if ($value !== null) { |
|
63
|
|
|
break; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ($value === null && $this->defaultValue !== null) { |
|
70
|
|
|
return $this->defaultValue; |
|
71
|
|
|
} |
|
72
|
|
|
return $this->convertXmlValueToModelValue($value); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param ModelElementInstanceInterface $modelElement |
|
77
|
|
|
* @param mixed $value |
|
78
|
|
|
* @param bool $withReferenceUpdate |
|
79
|
|
|
*/ |
|
80
|
|
|
public function setValue( |
|
81
|
|
|
ModelElementInstanceInterface $modelElement, |
|
82
|
|
|
$value, |
|
83
|
|
|
bool $withReferenceUpdate = true |
|
84
|
|
|
): void { |
|
85
|
|
|
$xmlValue = $this->convertModelValueToXmlValue($value); |
|
86
|
|
|
if ($this->namespaceUri === null) { |
|
87
|
|
|
$modelElement->setAttributeValue( |
|
88
|
|
|
$this->attributeName, |
|
89
|
|
|
$xmlValue, |
|
|
|
|
|
|
90
|
|
|
$this->isIdAttribute, |
|
91
|
|
|
$withReferenceUpdate |
|
92
|
|
|
); |
|
93
|
|
|
} else { |
|
94
|
|
|
$modelElement->setAttributeValueNs( |
|
95
|
|
|
$this->namespaceUri, |
|
96
|
|
|
$this->attributeName, |
|
97
|
|
|
$xmlValue, |
|
|
|
|
|
|
98
|
|
|
$this->isIdAttribute, |
|
99
|
|
|
$withReferenceUpdate |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function updateIncomingReferences( |
|
105
|
|
|
ModelElementInstanceInterface $modelElement, |
|
106
|
|
|
string $newIdentifier, |
|
107
|
|
|
?string $oldIdentifier |
|
108
|
|
|
): void { |
|
109
|
|
|
foreach ($this->incomingReferences as $incomingReference) { |
|
110
|
|
|
$incomingReference->referencedElementUpdated($modelElement, $oldIdentifier, $newIdentifier); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return mixed |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getDefaultValue() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->defaultValue; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param mixed $defaultValue |
|
124
|
|
|
*/ |
|
125
|
|
|
public function setDefaultValue($defaultValue): void |
|
126
|
|
|
{ |
|
127
|
|
|
$this->defaultValue = $defaultValue; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function isRequired(): bool |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->isRequired; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function setRequired(bool $required): void |
|
136
|
|
|
{ |
|
137
|
|
|
$this->isRequired = $required; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function setNamespaceUri(?string $namespaceUri): void |
|
141
|
|
|
{ |
|
142
|
|
|
$this->namespaceUri = $namespaceUri; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function getNamespaceUri(): ?string |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->namespaceUri; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function isIdAttribute(): bool |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->isIdAttribute; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function setId(): void |
|
156
|
|
|
{ |
|
157
|
|
|
$this->isIdAttribute = true; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function getAttributeName(): string |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->attributeName; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function setAttributeName(string $attributeName): void |
|
166
|
|
|
{ |
|
167
|
|
|
$this->attributeName = $attributeName; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function removeAttribute(ModelElementInstanceInterface $modelElement): void |
|
171
|
|
|
{ |
|
172
|
|
|
if ($this->namespaceUri === null) { |
|
173
|
|
|
$modelElement->removeAttribute($this->attributeName); |
|
174
|
|
|
} else { |
|
175
|
|
|
$modelElement->removeAttributeNs($this->namespaceUri, $this->attributeName); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param ModelElementInstanceInterface $modelElement |
|
181
|
|
|
* @param mixed $referenceIdentifier |
|
182
|
|
|
*/ |
|
183
|
|
|
public function unlinkReference(ModelElementInstanceInterface $modelElement, $referenceIdentifier): void |
|
184
|
|
|
{ |
|
185
|
|
|
if (!empty($this->incomingReferences)) { |
|
186
|
|
|
foreach ($this->incomingReferences as $incomingReference) { |
|
187
|
|
|
$incomingReference->referencedElementRemoved($modelElement, $referenceIdentifier); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function getIncomingReferences(): array |
|
193
|
|
|
{ |
|
194
|
|
|
return $this->incomingReferences; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public function getOutgoingReferences(): array |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->outgoingReferences; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function registerOutgoingReference(ReferenceInterface $ref): void |
|
203
|
|
|
{ |
|
204
|
|
|
$this->outgoingReferences[] = $ref; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function registerIncoming(ReferenceInterface $ref): void |
|
208
|
|
|
{ |
|
209
|
|
|
$this->incomingReferences[] = $ref; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|