1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\XML\ds; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\XML\Chunk; |
10
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
11
|
|
|
use SimpleSAML\XML\Exception\SchemaViolationException; |
12
|
|
|
use SimpleSAML\XML\Registry\ElementRegistry; |
13
|
|
|
use SimpleSAML\XML\SchemaValidatableElementInterface; |
14
|
|
|
use SimpleSAML\XML\SchemaValidatableElementTrait; |
15
|
|
|
use SimpleSAML\XML\SerializableElementInterface; |
16
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Abstract class representing the SPKIDataType. |
20
|
|
|
* |
21
|
|
|
* @package simplesamlphp/xml-security |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractSPKIDataType extends AbstractDsElement implements SchemaValidatableElementInterface |
24
|
|
|
{ |
25
|
|
|
use SchemaValidatableElementTrait; |
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Initialize a SPKIData element. |
29
|
|
|
* |
30
|
|
|
* @param array{ |
31
|
|
|
* array{\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, \SimpleSAML\XML\SerializableElementInterface|null} |
32
|
|
|
* } $tuples |
33
|
|
|
*/ |
34
|
|
|
final public function __construct( |
35
|
|
|
protected array $tuples, |
36
|
|
|
) { |
37
|
|
|
Assert::allIsArray($tuples, SchemaViolationException::class); |
38
|
|
|
Assert::allCount($tuples, 2); |
39
|
|
|
|
40
|
|
|
foreach ($tuples as $tuple) { |
41
|
|
|
Assert::isInstanceOf($tuple[0], SPKISexp::class, SchemaViolationException::class); |
42
|
|
|
Assert::nullOrIsInstanceOf($tuple[1], SerializableElementInterface::class, SchemaViolationException::class); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Collect the value of the SPKISexp-property |
49
|
|
|
* |
50
|
|
|
* @return array{array{\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, \SimpleSAML\XML\SerializableElementInterface|null}} |
|
|
|
|
51
|
|
|
*/ |
52
|
|
|
public function getTuples(): array |
53
|
|
|
{ |
54
|
|
|
return $this->tuples; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Convert XML into a SPKIData |
60
|
|
|
* |
61
|
|
|
* @param \DOMElement $xml The XML element we should load |
62
|
|
|
* @return static |
63
|
|
|
* |
64
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
65
|
|
|
* If the qualified name of the supplied element is wrong |
66
|
|
|
*/ |
67
|
|
|
public static function fromXML(DOMElement $xml): static |
68
|
|
|
{ |
69
|
|
|
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
70
|
|
|
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
71
|
|
|
|
72
|
|
|
$registry = ElementRegistry::getInstance(); |
73
|
|
|
$tuples = []; |
74
|
|
|
$tuple = [null, null]; |
75
|
|
|
foreach ($xml->childNodes as $node) { |
76
|
|
|
if ($node instanceof DOMElement) { |
77
|
|
|
if ($node->namespaceURI === static::NS && $node->localName === 'SPKISexp') { |
78
|
|
|
if ($tuple[0] !== null) { |
79
|
|
|
$tuples[] = $tuple; |
80
|
|
|
} |
81
|
|
|
$tuple = [SPKISexp::fromXML($node), null]; |
82
|
|
|
} elseif ($node->namespaceURI !== static::NS && $tuple[0] !== null) { |
83
|
|
|
$handler = $registry->getElementHandler($node->namespaceURI, $node->localName); |
84
|
|
|
$tuple[1] = ($handler === null) ? Chunk::fromXML($node) : $handler::fromXML($node); |
85
|
|
|
$tuples[] = $tuple; |
86
|
|
|
$tuple = [null, null]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($tuple[0] !== null) { |
92
|
|
|
$tuples[] = $tuple; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return new static($tuples); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Convert this SPKIData to XML. |
101
|
|
|
* |
102
|
|
|
* @param \DOMElement|null $parent The element we should append this SPKIData to. |
103
|
|
|
* @return \DOMElement |
104
|
|
|
*/ |
105
|
|
|
public function toXML(?DOMElement $parent = null): DOMElement |
106
|
|
|
{ |
107
|
|
|
$e = $this->instantiateParentElement($parent); |
108
|
|
|
|
109
|
|
|
foreach ($this->getTuples() as $tuple) { |
110
|
|
|
$tuple[0]->toXML($e); |
111
|
|
|
$tuple[1]?->toXML($e); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $e; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|