XPath   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 7
dl 0
loc 52
rs 10
c 5
b 1
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromXML() 0 6 1
A __construct() 0 3 1
A getExpression() 0 3 1
A toXML() 0 6 1
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\XMLSchema\Exception\InvalidDOMElementException;
10
use SimpleSAML\XMLSchema\Type\StringValue;
11
12
use function strval;
13
14
/**
15
 * Class implementing the XPath element.
16
 *
17
 * @package simplesamlphp/xml-security
18
 */
19
final class XPath extends AbstractDsElement
20
{
21
    /**
22
     * Construct an XPath element.
23
     *
24
     * @param \SimpleSAML\XMLSchema\Type\StringValue $expression The XPath expression itself.
25
     */
26
    final public function __construct(
27
        protected StringValue $expression,
28
    ) {
29
    }
30
31
32
    /**
33
     * Get the actual XPath expression.
34
     *
35
     * @return \SimpleSAML\XMLSchema\Type\StringValue
36
     */
37
    public function getExpression(): StringValue
38
    {
39
        return $this->expression;
40
    }
41
42
43
    /**
44
     * Convert XML into a class instance
45
     *
46
     * @param \DOMElement $xml
47
     * @return static
48
     *
49
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
50
     *   If the qualified name of the supplied element is wrong
51
     */
52
    public static function fromXML(DOMElement $xml): static
53
    {
54
        Assert::same($xml->localName, 'XPath', InvalidDOMElementException::class);
55
        Assert::same($xml->namespaceURI, self::NS, InvalidDOMElementException::class);
56
57
        return new static(StringValue::fromString($xml->textContent));
58
    }
59
60
61
    /**
62
     * @param \DOMElement|null $parent
63
     * @return \DOMElement
64
     */
65
    public function toXML(?DOMElement $parent = null): DOMElement
66
    {
67
        $e = $this->instantiateParentElement($parent);
68
        $e->textContent = strval($this->getExpression());
69
70
        return $e;
71
    }
72
}
73