Passed
Pull Request — master (#47)
by Tim
13:14 queued 11:16
created

AbstractEncryptionPropertyType::getId()   A

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\xenc;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Chunk;
10
use SimpleSAML\XML\Constants as C;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\MissingElementException;
13
use SimpleSAML\XML\Exception\SchemaViolationException;
14
use SimpleSAML\XML\ExtendableAttributesTrait;
15
use SimpleSAML\XML\ExtendableElementTrait;
16
use SimpleSAML\XML\XsNamespace as NS;
17
18
/**
19
 * Class representing <xenc:EncryptionPropertyType>.
20
 *
21
 * @package simplesamlphp/xml-security
22
 */
23
abstract class AbstractEncryptionPropertyType extends AbstractXencElement
24
{
25
    use ExtendableAttributesTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableAttributesTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\X...tEncryptionPropertyType: $localName, $nodeValue, $namespaceURI, $prefix, $attributes
Loading history...
26
    use ExtendableElementTrait;
27
28
    /** The namespace-attribute for the xs:anyAttribute element */
29
    public const XS_ANY_ATTR_NAMESPACE = [C::NS_XML];
30
31
    /** The namespace-attribute for the xs:any element */
32
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
33
34
35
    /**
36
     * EncryptionProperty constructor.
37
     *
38
     * @param \SimpleSAML\XML\SerializableElement[] $children
39
     * @param string|null $Target
40
     * @param string|null $Id
41
     * @param \SimpleSAML\XML\Attribute[] $namespacedAttributes
42
     */
43
    final public function __construct(
44
        array $children,
45
        protected ?string $Target = null,
46
        protected ?string $Id = null,
47
        array $namespacedAttributes = [],
48
    ) {
49
        Assert::minCount($children, 1, MissingElementException::class);
50
        Assert::nullOrValidURI($Target, SchemaViolationException::class);
51
        Assert::nullOrValidNCName($Id, SchemaViolationException::class);
52
53
        $this->setElements($children);
54
        $this->setAttributesNS($namespacedAttributes);
55
    }
56
57
58
    /**
59
     * Get the value of the $Target property.
60
     *
61
     * @return string|null
62
     */
63
    public function getTarget(): ?string
64
    {
65
        return $this->Target;
66
    }
67
68
69
    /**
70
     * Get the value of the $Id property.
71
     *
72
     * @return string|null
73
     */
74
    public function getId(): ?string
75
    {
76
        return $this->Id;
77
    }
78
79
80
    /**
81
     * @inheritDoc
82
     *
83
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
84
     *   If the qualified name of the supplied element is wrong
85
     */
86
    public static function fromXML(DOMElement $xml): static
87
    {
88
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
89
        Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
90
91
        $children = [];
92
        foreach ($xml->childNodes as $child) {
93
            if (!($child instanceof DOMElement)) {
94
                continue;
95
            }
96
97
            $children[] = new Chunk($child);
98
        }
99
100
        return new static(
101
            $children,
102
            self::getOptionalAttribute($xml, 'Target', null),
103
            self::getOptionalAttribute($xml, 'Id', null),
104
            self::getAttributesNSFromXML($xml),
105
        );
106
    }
107
108
109
    /**
110
     * @inheritDoc
111
     */
112
    public function toXML(DOMElement $parent = null): DOMElement
113
    {
114
        $e = $this->instantiateParentElement($parent);
115
116
        if ($this->getTarget() !== null) {
117
            $e->setAttribute('Target', $this->getTarget());
118
        }
119
120
        if ($this->getId() !== null) {
121
            $e->setAttribute('Id', $this->getId());
122
        }
123
124
        foreach ($this->getAttributesNS() as $attr) {
125
            $attr->toXML($e);
126
        }
127
128
        foreach ($this->getElements() as $child) {
129
            if (!$child->isEmptyElement()) {
130
                $child->toXML($e);
131
            }
132
        }
133
134
        return $e;
135
    }
136
}
137