Attribute::getAttributeValues()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The trait SimpleSAML\SAML2\XML\EncryptableElementTrait requires the property $ownerDocument which is not provided by SimpleSAML\SAML2\XML\saml\Attribute.
Loading history...
33
    use ExtendableAttributesTrait;
34
    use SchemaValidatableElementTrait;
35
36
37
    /** The namespace-attribute for the xs:anyAttribute element */
38
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
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 list<\SimpleSAML\XML\Attribute> $namespacedAttribute
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\saml\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
    public function getEncryptionBackend(): ?EncryptionBackend
124
    {
125
        // return the encryption backend you want to use,
126
        // or null if you are fine with the default
127
        return null;
128
    }
129
130
131
    /**
132
     * Convert XML into a Attribute
133
     *
134
     * @param \DOMElement $xml The XML element we should load
135
     * @return static
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
     * @param \DOMElement|null $parent The element we should append this Attribute to.
161
     * @return \DOMElement
162
     */
163
    public function toXML(?DOMElement $parent = null): DOMElement
164
    {
165
        $e = $this->instantiateParentElement($parent);
166
        $e->setAttribute('Name', strval($this->getName()));
167
168
        if ($this->getNameFormat() !== null) {
169
            $e->setAttribute('NameFormat', strval($this->getNameFormat()));
170
        }
171
172
        if ($this->getFriendlyName() !== null) {
173
            $e->setAttribute('FriendlyName', strval($this->getFriendlyName()));
174
        }
175
176
        foreach ($this->getAttributesNS() as $attr) {
177
            $attr->toXML($e);
178
        }
179
180
        foreach ($this->getAttributeValues() as $av) {
181
            $av->toXML($e);
182
        }
183
184
        return $e;
185
    }
186
}
187