|
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
|
|
|
public const DEFAULT_VALUE_TYPE = 'string'; |
|
12
|
|
|
|
|
13
|
|
|
public const ATTRIBUTE_NAMESPACE = 'namespace'; |
|
14
|
|
|
|
|
15
|
|
|
public const ATTRIBUTE_NAME = 'name'; |
|
16
|
|
|
|
|
17
|
|
|
public const ATTRIBUTE_REF = 'ref'; |
|
18
|
|
|
|
|
19
|
|
|
public const ATTRIBUTE_VALUE = 'value'; |
|
20
|
|
|
|
|
21
|
|
|
public const ATTRIBUTE_TYPE = 'type'; |
|
22
|
|
|
|
|
23
|
|
|
public const ATTRIBUTE_ABSTRACT = 'abstract'; |
|
24
|
|
|
|
|
25
|
|
|
public const ATTRIBUTE_MAX_OCCURS = 'maxOccurs'; |
|
26
|
|
|
|
|
27
|
|
|
public const ATTRIBUTE_MIN_OCCURS = 'minOccurs'; |
|
28
|
|
|
|
|
29
|
|
|
public const ATTRIBUTE_NILLABLE = 'nillable'; |
|
30
|
|
|
|
|
31
|
|
|
public const VALUE_UNBOUNDED = 'unbounded'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @deprecated |
|
35
|
|
|
*/ |
|
36
|
|
|
public const DEFAULT_OCCURENCE_VALUE = 1; |
|
37
|
|
|
|
|
38
|
|
|
public const DEFAULT_OCCURRENCE_VALUE = 1; |
|
39
|
|
|
|
|
40
|
40 |
|
public function getAttribute(): DOMAttr |
|
41
|
|
|
{ |
|
42
|
40 |
|
return $this->getNode(); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Tries to get attribute type on the same node in order to return the value of the attribute in its type. |
|
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 (false === $withNamespace && !empty($value)) { |
|
62
|
28 |
|
$value = implode('', array_slice(explode(':', $value), -1, 1)); |
|
63
|
|
|
} |
|
64
|
38 |
|
if (null !== $value && true === $withinItsType) { |
|
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 (false !== strpos($value, ':')) { |
|
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
|
|
|
* |
|
85
|
|
|
* @param mixed $value the value |
|
86
|
|
|
* @param null|string $knownType the value |
|
87
|
|
|
* |
|
88
|
|
|
* @return mixed |
|
89
|
|
|
*/ |
|
90
|
38 |
|
public static function getValueWithinItsType($value, ?string $knownType = null) |
|
91
|
|
|
{ |
|
92
|
38 |
|
if (is_int($value) || (!is_null($value) && in_array($knownType, [ |
|
93
|
38 |
|
'time', |
|
94
|
|
|
'positiveInteger', |
|
95
|
|
|
'unsignedLong', |
|
96
|
|
|
'unsignedInt', |
|
97
|
|
|
'short', |
|
98
|
|
|
'long', |
|
99
|
|
|
'int', |
|
100
|
|
|
'integer', |
|
101
|
38 |
|
], true))) { |
|
102
|
2 |
|
return intval($value); |
|
103
|
|
|
} |
|
104
|
38 |
|
if (is_float($value) || (!is_null($value) && in_array($knownType, [ |
|
105
|
38 |
|
'float', |
|
106
|
|
|
'double', |
|
107
|
|
|
'decimal', |
|
108
|
38 |
|
], true))) { |
|
109
|
2 |
|
return floatval($value); |
|
110
|
|
|
} |
|
111
|
38 |
|
if (is_bool($value) || (!is_null($value) && in_array($knownType, [ |
|
112
|
38 |
|
'bool', |
|
113
|
|
|
'boolean', |
|
114
|
38 |
|
], true))) { |
|
115
|
8 |
|
return 'true' === $value || true === $value || 1 === $value || '1' === $value; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
32 |
|
return $value; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|