Passed
Pull Request — master (#267)
by Tim
04:21
created

Scope::validateContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
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\Utils;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\XMLStringElementTrait;
12
13
/**
14
 * Class which represents the Scope element found in Shibboleth metadata.
15
 *
16
 * @link https://wiki.shibboleth.net/confluence/display/SC/ShibMetaExt+V1.0
17
 * @package simplesamlphp/saml2
18
 */
19
final class Scope extends AbstractShibmdElement
20
{
21
    use XMLStringElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\XMLStringElementTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\shibmd\Scope: $localName, $namespaceURI
Loading history...
22
23
    /**
24
     * Whether this is a regexp scope.
25
     *
26
     * @var bool
27
     */
28
    protected bool $regexp;
29
30
31
    /**
32
     * Create a Scope.
33
     *
34
     * @param string $scope
35
     * @param bool $regexp
36
     */
37
    public function __construct(string $scope, bool $regexp = false)
38
    {
39
        $this->setContent($scope);
40
        $this->setIsRegexpScope($regexp);
41
    }
42
43
44
    /**
45
     * Validate the content of the element.
46
     *
47
     * @param string $content  The value to go in the XML textContent
48
     * @throws \Exception on failure
49
     * @return void
50
     */
51
    protected function validateContent(string $content): void
52
    {
53
        Assert::notWhitespaceOnly($content);
54
    }
55
56
57
    /**
58
     * Collect the value of the regexp-property
59
     *
60
     * @return bool
61
     */
62
    public function isRegexpScope(): bool
63
    {
64
        return $this->regexp;
65
    }
66
67
68
    /**
69
     * Set the value of the regexp-property
70
     *
71
     * @param bool $regexp
72
     */
73
    private function setIsRegexpScope(bool $regexp): void
74
    {
75
        $this->regexp = $regexp;
76
    }
77
78
79
    /**
80
     * Convert XML into a Scope
81
     *
82
     * @param \DOMElement $xml The XML element we should load
83
     * @return self
84
     *
85
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
86
     */
87
    public static function fromXML(DOMElement $xml): object
88
    {
89
        Assert::same($xml->localName, 'Scope', InvalidDOMElementException::class);
90
        Assert::same($xml->namespaceURI, Scope::NS, InvalidDOMElementException::class);
91
92
        $scope = $xml->textContent;
93
        /** @psalm-var bool $regexp */
94
        $regexp = self::getBooleanAttribute($xml, 'regexp', 'false');
95
96
        return new self($scope, $regexp);
97
    }
98
99
100
    /**
101
     * Convert this Scope to XML.
102
     *
103
     * @param \DOMElement|null $parent The element we should append this Scope to.
104
     * @return \DOMElement
105
     */
106
    public function toXML(DOMElement $parent = null): DOMElement
107
    {
108
        /** @psalm-var \DOMDocument $e->ownerDocument */
109
        $e = $this->instantiateParentElement($parent);
110
        $e->textContent = $this->content;
111
112
        if ($this->regexp === true) {
113
            $e->setAttribute('regexp', 'true');
114
        } else {
115
            $e->setAttribute('regexp', 'false');
116
        }
117
118
        return $e;
119
    }
120
}
121