RequestedAttribute   A
last analyzed

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getIsRequired() 0 3 1
A fromXML() 0 11 1
A toXML() 0 9 3
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
    /** @var string */
24
    public const NS = AbstractMdElement::NS;
25
26
    /** @var string */
27
    public const NS_PREFIX = AbstractMdElement::NS_PREFIX;
28
29
    /** @var string */
30
    public const SCHEMA = AbstractMdElement::SCHEMA;
31
32
33
    /**
34
     * RequestedAttribute constructor.
35
     *
36
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue $Name
37
     * @param \SimpleSAML\XMLSchema\Type\BooleanValue|null $isRequired
38
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $NameFormat
39
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $FriendlyName
40
     * @param \SimpleSAML\SAML2\XML\saml\AttributeValue[] $AttributeValues
41
     */
42
    public function __construct(
43
        SAMLStringValue $Name,
44
        protected ?BooleanValue $isRequired = null,
45
        ?SAMLAnyURIValue $NameFormat = null,
46
        ?SAMLStringValue $FriendlyName = null,
47
        array $AttributeValues = [],
48
    ) {
49
        parent::__construct($Name, $NameFormat, $FriendlyName, $AttributeValues);
50
    }
51
52
53
    /**
54
     * Collect the value of the isRequired-property
55
     *
56
     * @return \SimpleSAML\XMLSchema\Type\BooleanValue|null
57
     */
58
    public function getIsRequired(): ?BooleanValue
59
    {
60
        return $this->isRequired;
61
    }
62
63
64
    /**
65
     * Convert XML into a RequestedAttribute
66
     *
67
     * @param \DOMElement $xml The XML element we should load
68
     * @return static
69
     *
70
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
71
     *   if the qualified name of the supplied element is wrong
72
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
73
     *   if the supplied element is missing one of the mandatory attributes
74
     */
75
    public static function fromXML(DOMElement $xml): static
76
    {
77
        Assert::same($xml->localName, 'RequestedAttribute', InvalidDOMElementException::class);
78
        Assert::same($xml->namespaceURI, RequestedAttribute::NS, InvalidDOMElementException::class);
79
80
        return new static(
81
            self::getAttribute($xml, 'Name', SAMLStringValue::class),
82
            self::getOptionalAttribute($xml, 'isRequired', BooleanValue::class, null),
83
            self::getOptionalAttribute($xml, 'NameFormat', SAMLAnyURIValue::class, null),
84
            self::getOptionalAttribute($xml, 'FriendlyName', SAMLStringValue::class, null),
85
            AttributeValue::getChildrenOfClass($xml),
86
        );
87
    }
88
89
90
    /**
91
     * Convert this RequestedAttribute to XML.
92
     *
93
     * @param \DOMElement|null $parent The element we should append this RequestedAttribute to.
94
     * @return \DOMElement
95
     */
96
    public function toXML(?DOMElement $parent = null): DOMElement
97
    {
98
        $e = parent::toXML($parent);
99
100
        if ($this->getIsRequired()?->toBoolean() !== null) {
101
            $e->setAttribute('isRequired', $this->getIsRequired()->toBoolean() ? 'true' : 'false');
102
        }
103
104
        return $e;
105
    }
106
}
107