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