Scope::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\shibmd;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\XML\StringElementTrait;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\SchemaValidatableElementInterface;
12
use SimpleSAML\XML\SchemaValidatableElementTrait;
13
14
/**
15
 * Class which represents the Scope element found in Shibboleth metadata.
16
 *
17
 * @link https://wiki.shibboleth.net/confluence/display/SC/ShibMetaExt+V1.0
18
 * @package simplesamlphp/saml2
19
 */
20
final class Scope extends AbstractShibmdElement implements SchemaValidatableElementInterface
21
{
22
    use SchemaValidatableElementTrait;
23
    use StringElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\SAML2\XML\StringElementTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\shibmd\Scope: $localName, $namespaceURI
Loading history...
24
25
26
    /**
27
     * Create a Scope.
28
     *
29
     * @param string $scope
30
     * @param bool|null $regexp
31
     */
32
    public function __construct(
33
        string $scope,
34
        protected ?bool $regexp = false,
35
    ) {
36
        $this->setContent($scope);
37
    }
38
39
40
    /**
41
     * Collect the value of the regexp-property
42
     *
43
     * @return bool|null
44
     */
45
    public function isRegexpScope(): ?bool
46
    {
47
        return $this->regexp;
48
    }
49
50
51
    /**
52
     * Convert XML into a Scope
53
     *
54
     * @param \DOMElement $xml The XML element we should load
55
     * @return static
56
     *
57
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
58
     *   if the qualified name of the supplied element is wrong
59
     */
60
    public static function fromXML(DOMElement $xml): static
61
    {
62
        Assert::same($xml->localName, 'Scope', InvalidDOMElementException::class);
63
        Assert::same($xml->namespaceURI, Scope::NS, InvalidDOMElementException::class);
64
65
        $scope = $xml->textContent;
66
        $regexp = self::getOptionalBooleanAttribute($xml, 'regexp', null);
67
68
        return new static($scope, $regexp);
69
    }
70
71
72
    /**
73
     * Convert this Scope to XML.
74
     *
75
     * @param \DOMElement|null $parent The element we should append this Scope to.
76
     * @return \DOMElement
77
     */
78
    public function toXML(?DOMElement $parent = null): DOMElement
79
    {
80
        $e = $this->instantiateParentElement($parent);
81
        $e->textContent = $this->getContent();
82
83
        if ($this->isRegexpScope() !== null) {
84
            $e->setAttribute('regexp', $this->isRegexpScope() ? 'true' : 'false');
85
        }
86
87
        return $e;
88
    }
89
}
90