AbstractPGPDataType::getPGPKeyPacket()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\ExtendableElementTrait;
10
use SimpleSAML\XML\SchemaValidatableElementInterface;
11
use SimpleSAML\XML\SchemaValidatableElementTrait;
12
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
13
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
14
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
15
use SimpleSAML\XMLSchema\XML\Constants\NS;
16
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
17
18
use function array_pop;
19
20
/**
21
 * Abstract class representing the PGPDataType.
22
 *
23
 * @package simplesamlphp/xml-security
24
 */
25
abstract class AbstractPGPDataType extends AbstractDsElement implements SchemaValidatableElementInterface
26
{
27
    use ExtendableElementTrait;
28
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\AbstractPGPDataType: $message, $line
Loading history...
29
30
31
    /** @var string */
32
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
33
34
35
    /**
36
     * Initialize a PGPData element.
37
     *
38
     * @param \SimpleSAML\XMLSecurity\XML\ds\PGPKeyID|null $pgpKeyId
39
     * @param \SimpleSAML\XMLSecurity\XML\ds\PGPKeyPacket|null $pgpKeyPacket
40
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
41
     * @throws \SimpleSAML\XMLSchema\Exception\SchemaViolationException
42
     */
43
    final public function __construct(
44
        protected ?PGPKeyID $pgpKeyId = null,
45
        protected ?PGPKeyPacket $pgpKeyPacket = null,
46
        array $children = [],
47
    ) {
48
        if ($pgpKeyId === null && $pgpKeyPacket === null) {
49
            throw new SchemaViolationException("ds:PGPKeyID and ds:PGPKeyPacket can't both be null.");
50
        }
51
52
        $this->setElements($children);
53
    }
54
55
56
    /**
57
     * Collect the value of the PGPKeyID-property
58
     *
59
     * @return \SimpleSAML\XMLSecurity\XML\ds\PGPKeyID|null
60
     */
61
    public function getPGPKeyID(): ?PGPKeyID
62
    {
63
        return $this->pgpKeyId;
64
    }
65
66
67
    /**
68
     * Collect the value of the PGPKeyPacket-property
69
     *
70
     * @return \SimpleSAML\XMLSecurity\XML\ds\PGPKeyPacket|null
71
     */
72
    public function getPGPKeyPacket(): ?PGPKeyPacket
73
    {
74
        return $this->pgpKeyPacket;
75
    }
76
77
78
    /**
79
     * Convert XML into a PGPData
80
     *
81
     * @param \DOMElement $xml The XML element we should load
82
     * @return static
83
     *
84
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
85
     *   If the qualified name of the supplied element is wrong
86
     */
87
    public static function fromXML(DOMElement $xml): static
88
    {
89
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
90
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
91
92
        $pgpKeyId = PGPKeyID::getChildrenOfClass($xml);
93
        Assert::maxCount($pgpKeyId, 1, TooManyElementsException::class);
94
95
        $pgpKeyPacket = PGPKeyPacket::getChildrenOfClass($xml);
96
        Assert::maxCount($pgpKeyPacket, 1, TooManyElementsException::class);
97
98
        return new static(
99
            array_pop($pgpKeyId),
100
            array_pop($pgpKeyPacket),
101
            self::getChildElementsFromXML($xml),
102
        );
103
    }
104
105
106
    /**
107
     * Convert this PGPData to XML.
108
     *
109
     * @param \DOMElement|null $parent The element we should append this PGPData to.
110
     * @return \DOMElement
111
     */
112
    public function toXML(?DOMElement $parent = null): DOMElement
113
    {
114
        $e = $this->instantiateParentElement($parent);
115
116
        $this->getPGPKeyId()?->toXML($e);
117
        $this->getPGPKeyPacket()?->toXML($e);
118
119
        foreach ($this->getElements() as $elt) {
120
            $elt->toXML($e);
121
        }
122
123
        return $e;
124
    }
125
}
126