1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SAML2\XML\saml; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use SAML2\Constants; |
10
|
|
|
use SAML2\DOMDocumentFactory; |
11
|
|
|
use Webmozart\Assert\Assert; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Serializable class representing an AttributeValue. |
15
|
|
|
* |
16
|
|
|
* @package simplesamlphp/saml2 |
17
|
|
|
*/ |
18
|
|
|
class AttributeValue extends AbstractSamlElement |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The raw DOMElement representing this value. |
22
|
|
|
* |
23
|
|
|
* @var \DOMElement |
24
|
|
|
*/ |
25
|
|
|
protected $element; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Create an AttributeValue. |
30
|
|
|
* |
31
|
|
|
* @param mixed $value The value of this element. Can be one of: |
32
|
|
|
* - string Create an attribute value with a simple string. |
33
|
|
|
* - \DOMElement(AttributeValue) Create an attribute value of the given DOMElement. |
34
|
|
|
* - \DOMElement Create an attribute value with the given DOMElement as a child. |
35
|
|
|
* |
36
|
|
|
* @throws \InvalidArgumentException if the supplied value is neither a string or a DOMElement |
37
|
|
|
*/ |
38
|
|
|
public function __construct($value) |
39
|
|
|
{ |
40
|
|
|
Assert::true( |
41
|
|
|
is_string($value) || is_int($value) || $value instanceof DOMElement, |
42
|
|
|
'Value must be of type `string`, `int` or `DOMElement`.' |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
if (is_string($value)) { |
46
|
|
|
$doc = DOMDocumentFactory::create(); |
47
|
|
|
$this->element = $doc->createElementNS(Constants::NS_SAML, 'saml:AttributeValue'); |
48
|
|
|
$this->element->setAttributeNS(Constants::NS_XSI, 'xsi:type', 'xs:string'); |
49
|
|
|
$this->element->appendChild($doc->createTextNode($value)); |
50
|
|
|
|
51
|
|
|
/* Make sure that the xs-namespace is available in the AttributeValue (for xs:string). */ |
52
|
|
|
$this->element->setAttributeNS(Constants::NS_XS, 'xs:tmp', 'tmp'); |
53
|
|
|
$this->element->removeAttributeNS(Constants::NS_XS, 'tmp'); |
54
|
|
|
return; |
55
|
|
|
} elseif (is_int($value)) { |
56
|
|
|
$doc = DOMDocumentFactory::create(); |
57
|
|
|
$this->element = $doc->createElementNS(Constants::NS_SAML, 'saml:AttributeValue'); |
58
|
|
|
$this->element->setAttributeNS(Constants::NS_XSI, 'xsi:type', 'xs:integer'); |
59
|
|
|
$this->element->appendChild($doc->createTextNode(strval($value))); |
60
|
|
|
|
61
|
|
|
/* Make sure that the xs-namespace is available in the AttributeValue (for xs:int). */ |
62
|
|
|
$this->element->setAttributeNS(Constants::NS_XS, 'xs:tmp', 'tmp'); |
63
|
|
|
$this->element->removeAttributeNS(Constants::NS_XS, 'tmp'); |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($value->namespaceURI === Constants::NS_SAML && $value->localName === 'AttributeValue') { |
68
|
|
|
$this->element = $value; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->element = $value; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Collect the value of the element-property |
77
|
|
|
* |
78
|
|
|
* @return \DOMElement |
79
|
|
|
*/ |
80
|
|
|
public function getElement(): DOMElement |
81
|
|
|
{ |
82
|
|
|
return $this->element; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Convert XML into a AttributeValue |
88
|
|
|
* |
89
|
|
|
* @param \DOMElement $xml The XML element we should load |
90
|
|
|
* @return \SAML2\XML\saml\AttributeValue |
91
|
|
|
* @throws \InvalidArgumentException if the qualified name of the supplied element is wrong |
92
|
|
|
*/ |
93
|
|
|
public static function fromXML(DOMElement $xml): object |
94
|
|
|
{ |
95
|
|
|
Assert::same($xml->localName, 'AttributeValue'); |
96
|
|
|
Assert::same($xml->namespaceURI, AttributeValue::NS); |
97
|
|
|
|
98
|
|
|
return new self($xml); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Append this attribute value to an element. |
104
|
|
|
* |
105
|
|
|
* @param \DOMElement|null $parent The element we should append this attribute value to. |
106
|
|
|
* @return \DOMElement The generated AttributeValue element. |
107
|
|
|
*/ |
108
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
109
|
|
|
{ |
110
|
|
|
if ($parent === null) { |
111
|
|
|
return $this->element; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** @var \DOMElement $element */ |
115
|
|
|
$element = $parent->ownerDocument->importNode($this->element, true); |
116
|
|
|
$parent->appendChild($element); |
117
|
|
|
return $element; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns a plain text content of the attribute value. |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getString(): string |
127
|
|
|
{ |
128
|
|
|
return $this->element->textContent; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|