Scoping::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\samlp;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Constants as C;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\SchemaValidatableElementInterface;
12
use SimpleSAML\XML\SchemaValidatableElementTrait;
13
14
use function array_pop;
15
16
/**
17
 * Class for handling SAML2 Scoping.
18
 *
19
 * @package simplesamlphp/saml2
20
 */
21
final class Scoping extends AbstractSamlpElement implements SchemaValidatableElementInterface
22
{
23
    use SchemaValidatableElementTrait;
24
25
    /**
26
     * Initialize a Scoping element.
27
     *
28
     * @param int|null $proxyCount
29
     * @param \SimpleSAML\SAML2\XML\samlp\IDPList|null $IDPList
30
     * @param \SimpleSAML\SAML2\XML\samlp\RequesterID[] $requesterId
31
     */
32
    public function __construct(
33
        protected ?int $proxyCount = null,
34
        protected ?IDPList $IDPList = null,
35
        protected array $requesterId = [],
36
    ) {
37
        Assert::maxCount($requesterId, C::UNBOUNDED_LIMIT);
38
        Assert::allIsInstanceOf($requesterId, RequesterID::class);
39
        Assert::nullOrNatural($proxyCount);
40
    }
41
42
43
    /**
44
     * @return \SimpleSAML\SAML2\XML\samlp\IDPList|null
45
     */
46
    public function getIDPList(): ?IDPList
47
    {
48
        return $this->IDPList;
49
    }
50
51
52
    /**
53
     * @return \SimpleSAML\SAML2\XML\samlp\RequesterID[]
54
     */
55
    public function getRequesterId(): array
56
    {
57
        return $this->requesterId;
58
    }
59
60
61
    /**
62
     * @return int|null
63
     */
64
    public function getProxyCount(): ?int
65
    {
66
        return $this->proxyCount;
67
    }
68
69
70
    /**
71
     * Test if an object, at the state it's in, would produce an empty XML-element
72
     *
73
     * @return bool
74
     */
75
    public function isEmptyElement(): bool
76
    {
77
        return empty($this->proxyCount)
78
            && empty($this->IDPList)
79
            && empty($this->requesterId);
80
    }
81
82
83
    /**
84
     * Convert XML into a Scoping-element
85
     *
86
     * @param \DOMElement $xml The XML element we should load
87
     * @return static
88
     *
89
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
90
     *   if the qualified name of the supplied element is wrong
91
     */
92
    public static function fromXML(DOMElement $xml): static
93
    {
94
        Assert::same($xml->localName, 'Scoping', InvalidDOMElementException::class);
95
        Assert::same($xml->namespaceURI, Scoping::NS, InvalidDOMElementException::class);
96
97
        $proxyCount = self::getOptionalIntegerAttribute($xml, 'ProxyCount', null);
98
        $idpList = IDPList::getChildrenOfClass($xml);
99
        $requesterId = RequesterID::getChildrenOfClass($xml);
100
101
        return new static(
102
            $proxyCount,
103
            array_pop($idpList),
104
            $requesterId,
105
        );
106
    }
107
108
109
    /**
110
     * Convert this Scoping to XML.
111
     *
112
     * @param \DOMElement|null $parent The element we should append this Scoping to.
113
     * @return \DOMElement
114
     */
115
    public function toXML(?DOMElement $parent = null): DOMElement
116
    {
117
        $e = $this->instantiateParentElement($parent);
118
119
        if ($this->getProxyCount() !== null) {
120
            $e->setAttribute('ProxyCount', strval($this->getProxyCount()));
121
        }
122
123
        $this->getIDPList()?->toXML($e);
124
125
        foreach ($this->getRequesterId() as $rid) {
126
            $rid->toXML($e);
127
        }
128
129
        return $e;
130
    }
131
}
132