|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace WsdlToPhp\DomHandler; |
|
6
|
|
|
|
|
7
|
|
|
use DOMAttr; |
|
8
|
|
|
|
|
9
|
|
|
class AbstractAttributeHandler extends AbstractNodeHandler |
|
10
|
|
|
{ |
|
11
|
|
|
const DEFAULT_VALUE_TYPE = 'string'; |
|
12
|
|
|
|
|
13
|
|
|
const ATTRIBUTE_NAMESPACE = 'namespace'; |
|
14
|
|
|
|
|
15
|
|
|
const ATTRIBUTE_NAME = 'name'; |
|
16
|
|
|
|
|
17
|
|
|
const ATTRIBUTE_REF = 'ref'; |
|
18
|
|
|
|
|
19
|
|
|
const ATTRIBUTE_VALUE = 'value'; |
|
20
|
|
|
|
|
21
|
|
|
const ATTRIBUTE_TYPE = 'type'; |
|
22
|
|
|
|
|
23
|
|
|
const ATTRIBUTE_ABSTRACT = 'abstract'; |
|
24
|
|
|
|
|
25
|
|
|
const ATTRIBUTE_MAX_OCCURS = 'maxOccurs'; |
|
26
|
|
|
|
|
27
|
|
|
const ATTRIBUTE_MIN_OCCURS = 'minOccurs'; |
|
28
|
|
|
|
|
29
|
|
|
const ATTRIBUTE_NILLABLE = 'nillable'; |
|
30
|
|
|
|
|
31
|
|
|
const VALUE_UNBOUNDED = 'unbounded'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @deprecated |
|
35
|
|
|
*/ |
|
36
|
|
|
const DEFAULT_OCCURENCE_VALUE = 1; |
|
37
|
|
|
|
|
38
|
|
|
const DEFAULT_OCCURRENCE_VALUE = 1; |
|
39
|
|
|
|
|
40
|
40 |
|
public function getAttribute(): DOMAttr |
|
41
|
|
|
{ |
|
42
|
40 |
|
return $this->getNode(); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
/** |
|
45
|
|
|
* Tries to get attribute type on the same node in order to return the value of the attribute in its type |
|
46
|
|
|
* @return string|null |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function getType(): ?string |
|
49
|
|
|
{ |
|
50
|
2 |
|
$type = null; |
|
51
|
2 |
|
if (($parent = $this->getParent()) instanceof ElementHandler && $parent->hasAttribute(self::ATTRIBUTE_TYPE)) { |
|
52
|
2 |
|
$type = $parent->getAttribute(self::ATTRIBUTE_TYPE)->getValue(false, false); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
return $type; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
38 |
|
public function getValue(bool $withNamespace = false, bool $withinItsType = true, ?string $asType = self::DEFAULT_VALUE_TYPE) |
|
59
|
|
|
{ |
|
60
|
38 |
|
$value = $this->getAttribute()->value; |
|
61
|
38 |
|
if ($withNamespace === false && !empty($value)) { |
|
62
|
28 |
|
$value = implode('', array_slice(explode(':', $value), -1, 1)); |
|
63
|
|
|
} |
|
64
|
38 |
|
if ($value !== null && $withinItsType === true) { |
|
65
|
38 |
|
$value = self::getValueWithinItsType($value, empty($asType) ? $this->getType() : $asType); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
38 |
|
return $value; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
public function getValueNamespace(): ?string |
|
72
|
|
|
{ |
|
73
|
2 |
|
$value = $this->getAttribute()->value; |
|
74
|
2 |
|
$namespace = null; |
|
75
|
2 |
|
if (strpos($value, ':') !== false) { |
|
76
|
2 |
|
$namespace = implode('', array_slice(explode(':', $value), 0, -1)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
return $namespace; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns the value with good type |
|
84
|
|
|
* @param mixed $value the value |
|
85
|
|
|
* @param string|null $knownType the value |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
*/ |
|
88
|
38 |
|
public static function getValueWithinItsType($value, ?string $knownType = null) |
|
89
|
|
|
{ |
|
90
|
38 |
|
if (is_int($value) || (!is_null($value) && in_array($knownType, array( |
|
91
|
38 |
|
'time', |
|
92
|
|
|
'positiveInteger', |
|
93
|
|
|
'unsignedLong', |
|
94
|
|
|
'unsignedInt', |
|
95
|
|
|
'short', |
|
96
|
|
|
'long', |
|
97
|
|
|
'int', |
|
98
|
|
|
'integer', |
|
99
|
38 |
|
), true))) { |
|
100
|
2 |
|
return intval($value); |
|
101
|
38 |
|
} elseif (is_float($value) || (!is_null($value) && in_array($knownType, array( |
|
102
|
38 |
|
'float', |
|
103
|
|
|
'double', |
|
104
|
|
|
'decimal', |
|
105
|
38 |
|
), true))) { |
|
106
|
2 |
|
return floatval($value); |
|
107
|
38 |
|
} elseif (is_bool($value) || (!is_null($value) && in_array($knownType, array( |
|
108
|
38 |
|
'bool', |
|
109
|
|
|
'boolean', |
|
110
|
38 |
|
), true))) { |
|
111
|
8 |
|
return ($value === 'true' || $value === true || $value === 1 || $value === '1'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
32 |
|
return $value; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|