Passed
Push — master ( 92e283...797c98 )
by Tim
02:38
created

XPath::getNamespaces()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
11
/**
12
 * Class implementing the XPath element.
13
 *
14
 * @package simplesamlphp/xml-security
15
 */
16
class XPath extends AbstractDsElement
17
{
18
    /**
19
     * Construct an XPath element.
20
     *
21
     * @param string $expression The XPath expression itself.
22
     */
23
    final public function __construct(
24
        protected string $expression,
25
    ) {
26
    }
27
28
29
    /**
30
     * Get the actual XPath expression.
31
     *
32
     * @return string
33
     */
34
    public function getExpression(): string
35
    {
36
        return $this->expression;
37
    }
38
39
40
    /**
41
     * Convert XML into a class instance
42
     *
43
     * @param DOMElement $xml
44
     * @return static
45
     *
46
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
47
     *   If the qualified name of the supplied element is wrong
48
     */
49
    public static function fromXML(DOMElement $xml): static
50
    {
51
        Assert::same($xml->localName, 'XPath', InvalidDOMElementException::class);
52
        Assert::same($xml->namespaceURI, self::NS, InvalidDOMElementException::class);
53
54
        return new static($xml->textContent);
55
    }
56
57
58
    /**
59
     * @param DOMElement|null $parent
60
     * @return DOMElement
61
     */
62
    public function toXML(?DOMElement $parent = null): DOMElement
63
    {
64
        $e = $this->instantiateParentElement($parent);
65
        $e->textContent = $this->getExpression();
66
67
        return $e;
68
    }
69
}
70