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

RequestedAttribute   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Type\SAMLAnyURIValue;
10
use SimpleSAML\SAML2\Type\SAMLStringValue;
11
use SimpleSAML\SAML2\XML\saml\Attribute;
12
use SimpleSAML\SAML2\XML\saml\AttributeValue;
13
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
14
use SimpleSAML\XMLSchema\Type\BooleanValue;
15
16
/**
17
 * Class representing SAML 2 metadata RequestedAttribute.
18
 *
19
 * @package simplesamlphp/saml2
20
 */
21
final class RequestedAttribute extends Attribute
22
{
23
    public const string NS = AbstractMdElement::NS;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 23 at column 24
Loading history...
24
25
    public const string NS_PREFIX = AbstractMdElement::NS_PREFIX;
26
27
    public const string SCHEMA = AbstractMdElement::SCHEMA;
28
29
30
    /**
31
     * RequestedAttribute constructor.
32
     *
33
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue $Name
34
     * @param \SimpleSAML\XMLSchema\Type\BooleanValue|null $isRequired
35
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $NameFormat
36
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $FriendlyName
37
     * @param \SimpleSAML\SAML2\XML\saml\AttributeValue[] $AttributeValues
38
     */
39
    public function __construct(
40
        SAMLStringValue $Name,
41
        protected ?BooleanValue $isRequired = null,
42
        ?SAMLAnyURIValue $NameFormat = null,
43
        ?SAMLStringValue $FriendlyName = null,
44
        array $AttributeValues = [],
45
    ) {
46
        parent::__construct($Name, $NameFormat, $FriendlyName, $AttributeValues);
47
    }
48
49
50
    /**
51
     * Collect the value of the isRequired-property
52
     *
53
     * @return \SimpleSAML\XMLSchema\Type\BooleanValue|null
54
     */
55
    public function getIsRequired(): ?BooleanValue
56
    {
57
        return $this->isRequired;
58
    }
59
60
61
    /**
62
     * Convert XML into a RequestedAttribute
63
     *
64
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
65
     *   if the qualified name of the supplied element is wrong
66
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
67
     *   if the supplied element is missing one of the mandatory attributes
68
     */
69
    public static function fromXML(DOMElement $xml): static
70
    {
71
        Assert::same($xml->localName, 'RequestedAttribute', InvalidDOMElementException::class);
72
        Assert::same($xml->namespaceURI, RequestedAttribute::NS, InvalidDOMElementException::class);
73
74
        return new static(
75
            self::getAttribute($xml, 'Name', SAMLStringValue::class),
76
            self::getOptionalAttribute($xml, 'isRequired', BooleanValue::class, null),
77
            self::getOptionalAttribute($xml, 'NameFormat', SAMLAnyURIValue::class, null),
78
            self::getOptionalAttribute($xml, 'FriendlyName', SAMLStringValue::class, null),
79
            AttributeValue::getChildrenOfClass($xml),
80
        );
81
    }
82
83
84
    /**
85
     * Convert this RequestedAttribute to XML.
86
     */
87
    public function toXML(?DOMElement $parent = null): DOMElement
88
    {
89
        $e = parent::toXML($parent);
90
91
        if ($this->getIsRequired()?->toBoolean() !== null) {
92
            $e->setAttribute('isRequired', $this->getIsRequired()->toBoolean() ? 'true' : 'false');
93
        }
94
95
        return $e;
96
    }
97
}
98