Passed
Push — main ( 4e0970...2bf451 )
by Stefan
09:05
created

XMLHelper::getAttribFloat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 6
rs 10
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
     * Get the string value of named attrib or return default value, if attrib not exist
17
     * @param \DOMElement $oXMLElement
18
     * @param string $strName
19
     * @param string $strDefault
20
     * @return string|null
21
     */
22
    static protected function getAttribString(\DOMElement $oXMLElement, string $strName, ?string $strDefault = null) : ?string
23
    {
24
        if (!$oXMLElement->hasAttribute($strName)) {
25
            return $strDefault;
26
        }
27
        return $oXMLElement->getAttribute($strName);
28
    }
29
30
    /**
31
     * Get the integer value of named attrib or return default value, if attrib not exist
32
     * @param \DOMElement $oXMLElement
33
     * @param string $strName
34
     * @param int $iDefault
35
     * @return int|null
36
     */
37
    static protected function getAttribInt(\DOMElement $oXMLElement, string $strName, ?int $iDefault = null) : ?int
38
    {
39
        if (!$oXMLElement->hasAttribute($strName)) {
40
            return $iDefault;
41
        }
42
        return intval($oXMLElement->getAttribute($strName));
43
    }
44
45
    /**
46
     * Get the float value of named attrib or return default value, if attrib not exist
47
     * @param \DOMElement $oXMLElement
48
     * @param string $strName
49
     * @param float $fltDefault
50
     * @return float|null
51
     */
52
    static protected function getAttribFloat(\DOMElement $oXMLElement, string $strName, ?float $fltDefault = null) : ?int
53
    {
54
        if (!$oXMLElement->hasAttribute($strName)) {
55
            return $fltDefault;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $fltDefault could return the type double which is incompatible with the type-hinted return integer|null. Consider adding an additional type-check to rule them out.
Loading history...
56
        }
57
        return floatval($oXMLElement->getAttribute($strName));
0 ignored issues
show
Bug Best Practice introduced by
The expression return floatval($oXMLEle...getAttribute($strName)) returns the type double which is incompatible with the type-hinted return integer|null.
Loading history...
58
    }
59
60
    /**
61
     * Get an array of string values of the named attrib.
62
     * The attrib must contain a list spearated by whitespace(s).
63
     * @param \DOMElement $oXMLElement
64
     * @param string $strName
65
     * @return array|null
66
     */
67
    static protected function getAttribStringArray(\DOMElement $oXMLElement, string $strName) : ?array
68
    {
69
        $aValues = null;
70
        $strArray = $oXMLElement->getAttribute($strName);
71
        if (strlen($strArray) > 0) {
72
            // to make it validateable by XSD-schema, we use a whitespace-separated list since
73
            // there is no way to define the delimiter for xs:list in XSD...
74
            $aValues = array_map('trim', preg_split('/\s+/', trim($strArray)));
75
        }
76
        return $aValues;
77
    }
78
79
    /**
80
     * Get an array of integer values of the named attrib.
81
     * The attrib must contain a list spearated by comma.
82
     * @param \DOMElement $oXMLElement
83
     * @param string $strName
84
     * @return array|null
85
     */
86
    static protected function getAttribIntArray(\DOMElement $oXMLElement, string $strName) : ?array
87
    {
88
        $aValues = null;
89
        $strArray = $oXMLElement->getAttribute($strName);
90
        if (strlen($strArray) > 0) {
91
            $aValues = array_map('intval', explode(',', $strArray));
92
        }
93
        return $aValues;
94
    }
95
96
    /**
97
     * Get the flags specified by the named attrib.
98
     * The attrib must contain a list of FormFlag - constants spearated by any whitespace(s).
99
     * @param \DOMElement $oXMLElement
100
     * @return int
101
     */
102
    static protected function getAttribFlags(\DOMElement $oXMLElement) : int
103
    {
104
        $wFlags = 0;
105
        $strFlags = $oXMLElement->getAttribute('flags');
106
        if (strlen($strFlags) > 0) {
107
            // to make it validateable by XSD-schema, we use a whitespace-separated list since
108
            // there is no way to define the delimiter for xs:list in XSD...
109
            $aFlags = array_map('trim', preg_split('/\s+/', trim($strFlags)));
110
            foreach ($aFlags as $strFlag) {
111
                $strConstName = __NAMESPACE__ . '\FormFlags::' . strtoupper($strFlag);
112
                if (defined($strConstName)) {
113
                    $wFlags += constant($strConstName);
114
                } else {
115
                    trigger_error('Unknown Constant [' . $strConstName . '] for the FormFlag property!', E_USER_ERROR);
116
                }
117
            }
118
        }
119
        return $wFlags;
120
    }
121
122
    /**
123
     * Read all known attributes that don't need any further processing.
124
     * @param \DOMElement $oXMLElement
125
     */
126
    protected function readElementAttributes(\DOMElement $oXMLElement, ?array $aAttributes) : array
127
    {
128
        $aAttributesToRead = [
129
            'onclick',
130
            'ondblclick',
131
            'onchange',
132
            'oninput',
133
            'onfocus',
134
            'onblur',
135
            'onkeydown',
136
            'onkeypress',
137
            'onkeyup',
138
            'title',
139
            'placeholder',
140
            'maxlength',
141
        ];
142
        if ($aAttributes == null) {
143
            $aAttributes = array();
144
        }
145
        foreach ($aAttributesToRead as $strAttribute) {
146
            if (($strValue = self::getAttribString($oXMLElement, $strAttribute)) !== null) {
147
                $aAttributes[$strAttribute] = $strValue;
148
            }
149
        }
150
        return $aAttributes;
151
    }
152
153
    /**
154
     * Get the child with the given tag name.
155
     * The given parent must only contain one chuld with this name!
156
     * @param string $strName
157
     * @return \DOMElement|null
158
     */
159
    protected function getXMLChild(\DOMElement $oXMLElement, string $strName) : ?\DOMElement
160
    {
161
        $oList = $oXMLElement->getElementsByTagName($strName);
162
        if ($oList->count() === 1) {
163
            $oChild = $oList->item(0);
164
            if ($oChild instanceof \DOMElement) {
165
                return $oChild;
166
            }
167
        }
168
        return null;
169
    }
170
}
171