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