Passed
Branch feature/php83 (cb5cde)
by Tim
14:20
created

Attribute   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 157
rs 10
c 0
b 0
f 0
wmc 14
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
10
use SimpleSAML\SAML2\Type\SAMLAnyURIValue;
11
use SimpleSAML\SAML2\Type\SAMLStringValue;
12
use SimpleSAML\SAML2\XML\EncryptableElementTrait;
13
use SimpleSAML\XML\ExtendableAttributesTrait;
14
use SimpleSAML\XML\SchemaValidatableElementInterface;
15
use SimpleSAML\XML\SchemaValidatableElementTrait;
16
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
17
use SimpleSAML\XMLSchema\XML\Constants\NS;
18
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
19
use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface;
20
21
use function strval;
22
23
/**
24
 * Class representing SAML 2 Attribute.
25
 *
26
 * @package simplesamlphp/saml2
27
 */
28
class Attribute extends AbstractSamlElement implements
29
    EncryptableElementInterface,
30
    SchemaValidatableElementInterface
31
{
32
    use EncryptableElementTrait;
33
    use ExtendableAttributesTrait;
34
    use SchemaValidatableElementTrait;
35
36
37
    /** The namespace-attribute for the xs:anyAttribute element */
38
    public const string XS_ANY_ATTR_NAMESPACE = NS::OTHER;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 38 at column 24
Loading history...
39
40
41
    /**
42
     * Initialize an Attribute.
43
     *
44
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue $name
45
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $nameFormat
46
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $friendlyName
47
     * @param \SimpleSAML\SAML2\XML\saml\AttributeValue[] $attributeValue
48
     * @param \SimpleSAML\XML\Attribute[] $namespacedAttribute
49
     */
50
    public function __construct(
51
        protected SAMLStringValue $name,
52
        protected ?SAMLAnyURIValue $nameFormat = null,
53
        protected ?SAMLStringValue $friendlyName = null,
54
        protected array $attributeValue = [],
55
        array $namespacedAttribute = [],
56
    ) {
57
        Assert::maxCount($attributeValue, C::UNBOUNDED_LIMIT);
58
        Assert::allIsInstanceOf($attributeValue, AttributeValue::class, 'Invalid AttributeValue.');
59
60
        switch (strval($nameFormat)) {
61
            case C::NAMEFORMAT_URI:
62
                Assert::validURI(
63
                    strval($name),
64
                    sprintf("Attribute name `%s` does not match its declared format `%s`", $name, $nameFormat),
65
                );
66
                break;
67
            case C::NAMEFORMAT_BASIC:
68
                Assert::validNCName(
69
                    strval($name),
70
                    sprintf("Attribute name `%s` does not match its declared format `%s`", $name, $nameFormat),
71
                );
72
                break;
73
        }
74
75
        $this->setAttributesNS($namespacedAttribute);
76
    }
77
78
79
    /**
80
     * Collect the value of the Name-property
81
     *
82
     * @return \SimpleSAML\SAML2\Type\SAMLStringValue
83
     */
84
    public function getName(): SAMLStringValue
85
    {
86
        return $this->name;
87
    }
88
89
90
    /**
91
     * Collect the value of the NameFormat-property
92
     *
93
     * @return \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null
94
     */
95
    public function getNameFormat(): ?SAMLAnyURIValue
96
    {
97
        return $this->nameFormat;
98
    }
99
100
101
    /**
102
     * Collect the value of the FriendlyName-property
103
     *
104
     * @return \SimpleSAML\SAML2\Type\SAMLStringValue|null
105
     */
106
    public function getFriendlyName(): ?SAMLStringValue
107
    {
108
        return $this->friendlyName;
109
    }
110
111
112
    /**
113
     * Collect the value of the attributeValues-property
114
     *
115
     * @return \SimpleSAML\SAML2\XML\saml\AttributeValue[]
116
     */
117
    public function getAttributeValues(): array
118
    {
119
        return $this->attributeValue;
120
    }
121
122
123
    /**
124
     * @return \SimpleSAML\XMLSecurity\Backend\EncryptionBackend|null
125
     */
126
    public function getEncryptionBackend(): ?EncryptionBackend
127
    {
128
        // return the encryption backend you want to use,
129
        // or null if you are fine with the default
130
        return null;
131
    }
132
133
134
    /**
135
     * Convert XML into a Attribute
136
     *
137
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
138
     *   if the qualified name of the supplied element is wrong
139
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
140
     *   if the supplied element is missing one of the mandatory attributes
141
     */
142
    public static function fromXML(DOMElement $xml): static
143
    {
144
        Assert::same($xml->localName, 'Attribute', InvalidDOMElementException::class);
145
        Assert::same($xml->namespaceURI, Attribute::NS, InvalidDOMElementException::class);
146
147
        return new static(
148
            self::getAttribute($xml, 'Name', SAMLStringValue::class),
149
            self::getOptionalAttribute($xml, 'NameFormat', SAMLAnyURIValue::class, null),
150
            self::getOptionalAttribute($xml, 'FriendlyName', SAMLStringValue::class, null),
151
            AttributeValue::getChildrenOfClass($xml),
152
            self::getAttributesNSFromXML($xml),
153
        );
154
    }
155
156
157
    /**
158
     * Convert this Attribute to XML.
159
     */
160
    public function toXML(?DOMElement $parent = null): DOMElement
161
    {
162
        $e = $this->instantiateParentElement($parent);
163
        $e->setAttribute('Name', strval($this->getName()));
164
165
        if ($this->getNameFormat() !== null) {
166
            $e->setAttribute('NameFormat', strval($this->getNameFormat()));
167
        }
168
169
        if ($this->getFriendlyName() !== null) {
170
            $e->setAttribute('FriendlyName', strval($this->getFriendlyName()));
171
        }
172
173
        foreach ($this->getAttributesNS() as $attr) {
174
            $attr->toXML($e);
175
        }
176
177
        foreach ($this->getAttributeValues() as $av) {
178
            $av->toXML($e);
179
        }
180
181
        return $e;
182
    }
183
}
184