Skip to content

Commit 280661e

Browse files
committed
Refactor ds:KeyInfo
1 parent 271d943 commit 280661e

File tree

2 files changed

+120
-95
lines changed

2 files changed

+120
-95
lines changed

src/XML/ds/AbstractKeyInfoType.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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\Exception\SchemaViolationException;
10+
use SimpleSAML\XML\ExtendableElementTrait;
11+
use SimpleSAML\XML\SerializableElementInterface;
12+
use SimpleSAML\XML\XsNamespace as NS;
13+
use SimpleSAML\XMLSecurity\Constants as C;
14+
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
15+
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
16+
17+
/**
18+
* Abstract class representing the KeyInfoType.
19+
*
20+
* @package simplesamlphp/xml-security
21+
*/
22+
abstract class AbstractKeyInfoType extends AbstractDsElement
23+
{
24+
use ExtendableElementTrait;
25+
26+
/** @var \SimpleSAML\XML\XsNamespace */
27+
public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
28+
29+
30+
/**
31+
* Initialize a KeyInfo element.
32+
*
33+
* @param (
34+
* \SimpleSAML\XMLSecurity\XML\ds\KeyName|
35+
* \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
36+
* \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
37+
* \SimpleSAML\XMLSecurity\XML\ds\X509Data|
38+
* \SimpleSAML\XML\SerializableElementInterface
39+
* )[] $info
40+
* @param string|null $Id
41+
*/
42+
final public function __construct(
43+
protected array $info,
44+
protected ?string $Id = null,
45+
) {
46+
Assert::notEmpty(
47+
$info,
48+
sprintf(
49+
'%s:%s cannot be empty',
50+
static::getNamespacePrefix(),
51+
static::getLocalName(),
52+
),
53+
InvalidArgumentException::class,
54+
);
55+
Assert::maxCount($info, C::UNBOUNDED_LIMIT);
56+
Assert::allIsInstanceOf(
57+
$info,
58+
SerializableElementInterface::class,
59+
InvalidArgumentException::class,
60+
);
61+
Assert::nullOrValidNCName($Id);
62+
63+
foreach ($info as $item) {
64+
if ($item instanceof AbstractDsElement) {
65+
Assert::isInstanceOfAny(
66+
$item,
67+
[KeyName::class, KeyValue::class, RetrievalMethod::class, X509Data::class],
68+
SchemaViolationException::class,
69+
);
70+
}
71+
}
72+
}
73+
74+
75+
/**
76+
* Collect the value of the Id-property
77+
*
78+
* @return string|null
79+
*/
80+
public function getId(): ?string
81+
{
82+
return $this->Id;
83+
}
84+
85+
86+
/**
87+
* Collect the value of the info-property
88+
*
89+
* @return list<\SimpleSAML\XML\SerializableElementInterface>
90+
*/
91+
public function getInfo(): array
92+
{
93+
return $this->info;
94+
}
95+
96+
97+
/**
98+
* Convert this KeyInfo to XML.
99+
*
100+
* @param \DOMElement|null $parent The element we should append this KeyInfo to.
101+
* @return \DOMElement
102+
*/
103+
public function toXML(DOMElement $parent = null): DOMElement
104+
{
105+
$e = $this->instantiateParentElement($parent);
106+
107+
if ($this->getId() !== null) {
108+
$e->setAttribute('Id', $this->getId());
109+
}
110+
111+
foreach ($this->getInfo() as $elt) {
112+
$elt->toXML($e);
113+
}
114+
115+
return $e;
116+
}
117+
}

src/XML/ds/KeyInfo.php

Lines changed: 3 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -7,86 +7,16 @@
77
use DOMElement;
88
use SimpleSAML\Assert\Assert;
99
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10-
use SimpleSAML\XML\Exception\SchemaViolationException;
11-
use SimpleSAML\XML\ExtendableElementTrait;
12-
use SimpleSAML\XML\SerializableElementInterface;
13-
use SimpleSAML\XML\XsNamespace as NS;
14-
use SimpleSAML\XMLSecurity\Constants as C;
15-
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
16-
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
10+
11+
use function array_merge;
1712

1813
/**
1914
* Class representing a ds:KeyInfo element.
2015
*
2116
* @package simplesamlphp/xml-security
2217
*/
23-
final class KeyInfo extends AbstractDsElement
18+
final class KeyInfo extends AbstractKeyInfoType
2419
{
25-
use ExtendableElementTrait;
26-
27-
/** @var \SimpleSAML\XML\XsNamespace */
28-
public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
29-
30-
31-
/**
32-
* Initialize a KeyInfo element.
33-
*
34-
* @param (
35-
* \SimpleSAML\XMLSecurity\XML\ds\KeyName|
36-
* \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
37-
* \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
38-
* \SimpleSAML\XMLSecurity\XML\ds\X509Data|
39-
* \SimpleSAML\XML\SerializableElementInterface
40-
* )[] $info
41-
* @param string|null $Id
42-
*/
43-
public function __construct(
44-
protected array $info,
45-
protected ?string $Id = null,
46-
) {
47-
Assert::notEmpty($info, 'ds:KeyInfo cannot be empty', InvalidArgumentException::class);
48-
Assert::maxCount($info, C::UNBOUNDED_LIMIT);
49-
Assert::allIsInstanceOf(
50-
$info,
51-
SerializableElementInterface::class,
52-
InvalidArgumentException::class,
53-
);
54-
Assert::nullOrValidNCName($Id);
55-
56-
foreach ($info as $item) {
57-
if ($item instanceof AbstractDsElement) {
58-
Assert::isInstanceOfAny(
59-
$item,
60-
[KeyName::class, KeyValue::class, RetrievalMethod::class, X509Data::class],
61-
SchemaViolationException::class,
62-
);
63-
}
64-
}
65-
}
66-
67-
68-
/**
69-
* Collect the value of the Id-property
70-
*
71-
* @return string|null
72-
*/
73-
public function getId(): ?string
74-
{
75-
return $this->Id;
76-
}
77-
78-
79-
/**
80-
* Collect the value of the info-property
81-
*
82-
* @return list<\SimpleSAML\XML\SerializableElementInterface>
83-
*/
84-
public function getInfo(): array
85-
{
86-
return $this->info;
87-
}
88-
89-
9020
/**
9121
* Convert XML into a KeyInfo
9222
*
@@ -125,26 +55,4 @@ public static function fromXML(DOMElement $xml): static
12555

12656
return new static($info, $Id);
12757
}
128-
129-
130-
/**
131-
* Convert this KeyInfo to XML.
132-
*
133-
* @param \DOMElement|null $parent The element we should append this KeyInfo to.
134-
* @return \DOMElement
135-
*/
136-
public function toXML(DOMElement $parent = null): DOMElement
137-
{
138-
$e = $this->instantiateParentElement($parent);
139-
140-
if ($this->getId() !== null) {
141-
$e->setAttribute('Id', $this->getId());
142-
}
143-
144-
foreach ($this->getInfo() as $elt) {
145-
$elt->toXML($e);
146-
}
147-
148-
return $e;
149-
}
15058
}

0 commit comments

Comments
 (0)