1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SKien\Formgenerator; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Trait containing some Helper to generate the Form from XML-File |
8
|
|
|
* @package Formgenerator |
9
|
|
|
* @author Stefanius <[email protected]> |
10
|
|
|
* @copyright MIT License - see the LICENSE file for details |
11
|
|
|
* @internal |
12
|
|
|
*/ |
13
|
|
|
trait XMLHelper |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Test if requested attribute is set. |
17
|
|
|
* @param \DOMElement $oXMLElement |
18
|
|
|
* @param string $strName |
19
|
|
|
* @return bool |
20
|
|
|
*/ |
21
|
|
|
static protected function hasAttrib(\DOMElement $oXMLElement, string $strName) : bool |
22
|
|
|
{ |
23
|
|
|
return $oXMLElement->hasAttribute($strName); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get the string value of named attrib or return default value, if attrib not exist. |
28
|
|
|
* @param \DOMElement $oXMLElement |
29
|
|
|
* @param string $strName |
30
|
|
|
* @param string $strDefault |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
static protected function getAttribString(\DOMElement $oXMLElement, string $strName, string $strDefault = '') : string |
34
|
|
|
{ |
35
|
|
|
if (!$oXMLElement->hasAttribute($strName)) { |
36
|
|
|
return $strDefault; |
37
|
|
|
} |
38
|
|
|
return $oXMLElement->getAttribute($strName); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the integer value of named attrib or return default value, if attrib not exist. |
43
|
|
|
* @param \DOMElement $oXMLElement |
44
|
|
|
* @param string $strName |
45
|
|
|
* @param int $iDefault |
46
|
|
|
* @return int |
47
|
|
|
*/ |
48
|
|
|
static protected function getAttribInt(\DOMElement $oXMLElement, string $strName, int $iDefault = 0) : int |
49
|
|
|
{ |
50
|
|
|
if (!$oXMLElement->hasAttribute($strName)) { |
51
|
|
|
return $iDefault; |
52
|
|
|
} |
53
|
|
|
return intval($oXMLElement->getAttribute($strName)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the float value of named attrib or return default value, if attrib not exist. |
58
|
|
|
* @param \DOMElement $oXMLElement |
59
|
|
|
* @param string $strName |
60
|
|
|
* @param float $fltDefault |
61
|
|
|
* @return float |
62
|
|
|
*/ |
63
|
|
|
static protected function getAttribFloat(\DOMElement $oXMLElement, string $strName, float $fltDefault = 0.0) : float |
64
|
|
|
{ |
65
|
|
|
if (!$oXMLElement->hasAttribute($strName)) { |
66
|
|
|
return $fltDefault; |
67
|
|
|
} |
68
|
|
|
return floatval($oXMLElement->getAttribute($strName)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the boolean value of named attrib or return default value, if attrib not exist. |
73
|
|
|
* Legal values for boolean are true, false, 1 (which indicates true), and 0 (which indicates false). |
74
|
|
|
* @link https://www.w3schools.com/xml/schema_dtypes_misc.asp |
75
|
|
|
* @param \DOMElement $oXMLElement |
76
|
|
|
* @param string $strName |
77
|
|
|
* @param bool $bDefault |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
static protected function getAttribBool(\DOMElement $oXMLElement, string $strName, bool $bDefault = false) : bool |
81
|
|
|
{ |
82
|
|
|
if (!$oXMLElement->hasAttribute($strName)) { |
83
|
|
|
return $bDefault; |
84
|
|
|
} |
85
|
|
|
$strValue = $oXMLElement->getAttribute($strName); |
86
|
|
|
return ($strValue == 'true' || $strValue == '1'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get an array of string values of the named attrib. |
91
|
|
|
* The attrib must contain a list spearated by whitespace(s). |
92
|
|
|
* @param \DOMElement $oXMLElement |
93
|
|
|
* @param string $strName |
94
|
|
|
* @return array<string> |
95
|
|
|
*/ |
96
|
|
|
static protected function getAttribStringArray(\DOMElement $oXMLElement, string $strName) : array |
97
|
|
|
{ |
98
|
|
|
$aValues = []; |
99
|
|
|
$strArray = $oXMLElement->getAttribute($strName); |
100
|
|
|
if (strlen($strArray) > 0) { |
101
|
|
|
// to make it validateable by XSD-schema, we use a whitespace-separated list since |
102
|
|
|
// there is no way to define the delimiter for xs:list in XSD... |
103
|
|
|
$aValues = self::splitWhitespaces($strArray); |
104
|
|
|
} |
105
|
|
|
return $aValues; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get an array of integer values of the named attrib. |
110
|
|
|
* The attrib must contain a list spearated by comma. |
111
|
|
|
* @param \DOMElement $oXMLElement |
112
|
|
|
* @param string $strName |
113
|
|
|
* @return array<int> |
114
|
|
|
*/ |
115
|
|
|
static protected function getAttribIntArray(\DOMElement $oXMLElement, string $strName) : array |
116
|
|
|
{ |
117
|
|
|
$aValues = []; |
118
|
|
|
$strArray = $oXMLElement->getAttribute($strName); |
119
|
|
|
if (strlen($strArray) > 0) { |
120
|
|
|
$aValues = array_map('intval', explode(',', $strArray)); |
121
|
|
|
} |
122
|
|
|
return $aValues; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the flags specified by the named attrib. |
127
|
|
|
* The attrib must contain a list of FormFlag - constants spearated by any whitespace(s). |
128
|
|
|
* @param \DOMElement $oXMLElement |
129
|
|
|
* @return int |
130
|
|
|
*/ |
131
|
|
|
static protected function getAttribFlags(\DOMElement $oXMLElement) : int |
132
|
|
|
{ |
133
|
|
|
$wFlags = 0; |
134
|
|
|
$strFlags = $oXMLElement->getAttribute('flags'); |
135
|
|
|
if (strlen($strFlags) > 0) { |
136
|
|
|
// to make it validateable by XSD-schema, we use a whitespace-separated list since |
137
|
|
|
// there is no way to define the delimiter for xs:list in XSD... |
138
|
|
|
$aFlags = self::splitWhitespaces($strFlags); |
139
|
|
|
foreach ($aFlags as $strFlag) { |
140
|
|
|
$strConstName = __NAMESPACE__ . '\FormFlags::' . strtoupper($strFlag); |
141
|
|
|
if (defined($strConstName)) { |
142
|
|
|
$wFlags += constant($strConstName); |
143
|
|
|
} else { |
144
|
|
|
trigger_error('Unknown Constant [' . $strConstName . '] for the FormFlag property!', E_USER_ERROR); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
return $wFlags; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Read all known attributes that don't need any further processing. |
153
|
|
|
* @param \DOMElement $oXMLElement |
154
|
|
|
* @param array<string,string> $aAttributes |
155
|
|
|
* @return array<string,string> |
156
|
|
|
*/ |
157
|
|
|
protected function readElementAttributes(\DOMElement $oXMLElement, ?array $aAttributes) : array |
158
|
|
|
{ |
159
|
|
|
$aAttributesToRead = [ |
160
|
|
|
'onclick', |
161
|
|
|
'ondblclick', |
162
|
|
|
'onchange', |
163
|
|
|
'oninput', |
164
|
|
|
'onfocus', |
165
|
|
|
'onblur', |
166
|
|
|
'onkeydown', |
167
|
|
|
'onkeypress', |
168
|
|
|
'onkeyup', |
169
|
|
|
'title', |
170
|
|
|
'placeholder', |
171
|
|
|
'maxlength', |
172
|
|
|
]; |
173
|
|
|
if ($aAttributes == null) { |
174
|
|
|
$aAttributes = array(); |
175
|
|
|
} |
176
|
|
|
foreach ($aAttributesToRead as $strAttribute) { |
177
|
|
|
if (self::hasAttrib($oXMLElement, $strAttribute)) { |
178
|
|
|
$aAttributes[$strAttribute] = self::getAttribString($oXMLElement, $strAttribute); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
return $aAttributes; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get the child with the given tag name. |
186
|
|
|
* The given parent must only contain one chuld with this name! |
187
|
|
|
* @param string $strName |
188
|
|
|
* @return \DOMElement|null |
189
|
|
|
*/ |
190
|
|
|
protected function getXMLChild(\DOMElement $oXMLElement, string $strName) : ?\DOMElement |
191
|
|
|
{ |
192
|
|
|
$oList = $oXMLElement->getElementsByTagName($strName); |
193
|
|
|
if ($oList->count() === 1) { |
194
|
|
|
$oChild = $oList->item(0); |
195
|
|
|
if ($oChild instanceof \DOMElement) { |
196
|
|
|
return $oChild; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
return null; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Split the given string by whitespaces. |
204
|
|
|
* @param string $strToSplit |
205
|
|
|
* @return array<string> |
206
|
|
|
*/ |
207
|
|
|
static protected function splitWhitespaces(string $strToSplit) : array |
208
|
|
|
{ |
209
|
|
|
$aSplit = preg_split('/\s+/', trim($strToSplit)); |
210
|
|
|
if ($aSplit !== false) { |
211
|
|
|
$aSplit = array_map('trim', $aSplit); |
212
|
|
|
} else { |
213
|
|
|
$aSplit = []; |
214
|
|
|
} |
215
|
|
|
return $aSplit; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|