1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\SAML2\XML\saml; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\SAML2\Constants as C; |
10
|
|
|
use SimpleSAML\SAML2\Utils; |
11
|
|
|
use SimpleSAML\SAML2\XML\ExtensionPointInterface; |
12
|
|
|
use SimpleSAML\SAML2\XML\ExtensionPointTrait; |
13
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
14
|
|
|
use SimpleSAML\XML\Exception\SchemaViolationException; |
15
|
|
|
|
16
|
|
|
use function count; |
17
|
|
|
use function explode; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* SAML Condition data type. |
21
|
|
|
* |
22
|
|
|
* @package simplesamlphp/saml2 |
23
|
|
|
*/ |
24
|
|
|
abstract class Condition extends AbstractConditionType implements ExtensionPointInterface |
25
|
|
|
{ |
26
|
|
|
use ExtensionPointTrait; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
public const LOCALNAME = 'Condition'; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Convert an XML element into a Condition. |
34
|
|
|
* |
35
|
|
|
* @param \DOMElement $xml The root XML element |
36
|
|
|
* @return \SimpleSAML\SAML2\XML\saml\Condition The condition |
37
|
|
|
* |
38
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong |
39
|
|
|
*/ |
40
|
|
|
public static function fromXML(DOMElement $xml): object |
41
|
|
|
{ |
42
|
|
|
Assert::same($xml->localName, 'Condition', InvalidDOMElementException::class); |
43
|
|
|
Assert::same($xml->namespaceURI, C::NS_SAML, InvalidDOMElementException::class); |
44
|
|
|
Assert::true( |
45
|
|
|
$xml->hasAttributeNS(C::NS_XSI, 'type'), |
46
|
|
|
'Missing required xsi:type in <saml:Condition> element.', |
47
|
|
|
SchemaViolationException::class |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$type = $xml->getAttributeNS(C::NS_XSI, 'type'); |
51
|
|
|
Assert::validQName($type, SchemaViolationException::class); |
52
|
|
|
|
53
|
|
|
$qname = explode(':', $type, 2); |
54
|
|
|
if (count($qname) === 2) { |
55
|
|
|
list($prefix, $element) = $qname; |
56
|
|
|
} else { |
57
|
|
|
$prefix = null; |
58
|
|
|
list($element) = $qname; |
59
|
|
|
} |
60
|
|
|
$ns = $xml->lookupNamespaceUri($prefix); |
61
|
|
|
$handler = Utils::getContainer()->getElementHandler($ns, $element); |
62
|
|
|
|
63
|
|
|
Assert::notNull($handler, 'Unknown Condition type `' . $type . '`.'); |
64
|
|
|
Assert::isAOf($handler, Condition::class); |
65
|
|
|
|
66
|
|
|
return $handler::fromXML($xml); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Convert this Condition to XML. |
72
|
|
|
* |
73
|
|
|
* @param \DOMElement $parent The element we are converting to XML. |
74
|
|
|
* @return \DOMElement The XML element after adding the data corresponding to this Condition. |
75
|
|
|
*/ |
76
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
77
|
|
|
{ |
78
|
|
|
$e = $this->instantiateParentElement($parent); |
79
|
|
|
|
80
|
|
|
$e->setAttribute('xmlns:' . static::NS_XSI_TYPE_PREFIX, static::NS_XSI_TYPE_NAMESPACE); |
|
|
|
|
81
|
|
|
$e->setAttributeNS(C::NS_XSI, 'xsi:type', static::getXsiType()); |
82
|
|
|
|
83
|
|
|
return $e; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|