Scoping::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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