Passed
Push — master ( a3fda0...7c0590 )
by Tim
14:58 queued 13:28
created

Scope   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

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