Passed
Branch feature/php83 (e52173)
by Tim
33:00 queued 16:50
created

AbstractKeyInfoType   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 39
c 1
b 0
f 0
dl 0
loc 114
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\XML\ExtendableElementTrait;
9
use SimpleSAML\XML\SerializableElementInterface;
10
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
11
use SimpleSAML\XMLSchema\Type\IDValue;
12
use SimpleSAML\XMLSchema\XML\Constants\NS;
13
use SimpleSAML\XMLSecurity\Assert\Assert;
14
use SimpleSAML\XMLSecurity\Constants as C;
15
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
16
use SimpleSAML\XMLSecurity\XML\dsig11\AbstractDsig11Element;
17
use SimpleSAML\XMLSecurity\XML\dsig11\DEREncodedKeyValue;
18
19
use function strval;
20
21
/**
22
 * Abstract class representing the KeyInfoType.
23
 *
24
 * @package simplesamlphp/xml-security
25
 */
26
abstract class AbstractKeyInfoType extends AbstractDsElement
27
{
28
    use ExtendableElementTrait;
29
30
31
    public const string XS_ANY_ELT_NAMESPACE = NS::OTHER;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 31 at column 24
Loading history...
32
33
34
    /**
35
     * Initialize a KeyInfo element.
36
     *
37
     * @param (
38
     *     \SimpleSAML\XMLSecurity\XML\ds\KeyName|
39
     *     \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
40
     *     \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
41
     *     \SimpleSAML\XMLSecurity\XML\ds\X509Data|
42
     *     \SimpleSAML\XMLSecurity\XML\ds\PGPData|
43
     *     \SimpleSAML\XMLSecurity\XML\ds\SPKIData|
44
     *     \SimpleSAML\XMLSecurity\XML\ds\MgmtData|
45
     *     \SimpleSAML\XMLSecurity\XML\dsig11\DEREncodedKeyValue|
46
     *     \SimpleSAML\XML\SerializableElementInterface
47
     * )[] $info
48
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $Id
49
     */
50
    final public function __construct(
51
        protected array $info,
52
        protected ?IDValue $Id = null,
53
    ) {
54
        Assert::notEmpty(
55
            $info,
56
            sprintf(
57
                '%s:%s cannot be empty',
58
                static::getNamespacePrefix(),
59
                static::getLocalName(),
60
            ),
61
            InvalidArgumentException::class,
62
        );
63
        Assert::maxCount($info, C::UNBOUNDED_LIMIT);
64
        Assert::allIsInstanceOf(
65
            $info,
66
            SerializableElementInterface::class,
67
            InvalidArgumentException::class,
68
        );
69
70
        foreach ($info as $item) {
71
            if ($item instanceof AbstractDsElement) {
72
                Assert::isInstanceOfAny(
73
                    $item,
74
                    [
75
                        KeyName::class,
76
                        KeyValue::class,
77
                        RetrievalMethod::class,
78
                        X509Data::class,
79
                        PGPData::class,
80
                        SPKIData::class,
81
                        MgmtData::class,
82
                    ],
83
                    SchemaViolationException::class,
84
                );
85
            } elseif ($item instanceof AbstractDsig11Element) {
86
                Assert::isInstanceOfAny(
87
                    $item,
88
                    [
89
                        DEREncodedKeyValue::class,
90
                    ],
91
                    SchemaViolationException::class,
92
                );
93
            }
94
        }
95
    }
96
97
98
    /**
99
     * Collect the value of the Id-property
100
     *
101
     * @return \SimpleSAML\XMLSchema\Type\IDValue|null
102
     */
103
    public function getId(): ?IDValue
104
    {
105
        return $this->Id;
106
    }
107
108
109
    /**
110
     * Collect the value of the info-property
111
     *
112
     * @return list<\SimpleSAML\XML\SerializableElementInterface>
113
     */
114
    public function getInfo(): array
115
    {
116
        return $this->info;
117
    }
118
119
120
    /**
121
     * Convert this KeyInfo to XML.
122
     *
123
     * @param \DOMElement|null $parent The element we should append this KeyInfo to.
124
     */
125
    public function toXML(?DOMElement $parent = null): DOMElement
126
    {
127
        $e = $this->instantiateParentElement($parent);
128
129
        if ($this->getId() !== null) {
130
            $e->setAttribute('Id', strval($this->getId()));
131
        }
132
133
        foreach ($this->getInfo() as $elt) {
134
            $elt->toXML($e);
135
        }
136
137
        return $e;
138
    }
139
}
140