Field   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 18
dl 0
loc 90
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getXPath() 0 3 1
A fromXML() 0 13 1
A __construct() 0 9 1
A toXML() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\XML;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XML\SchemaValidatableElementInterface;
10
use SimpleSAML\XML\SchemaValidatableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
13
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
14
use SimpleSAML\XMLSchema\Type\IDValue;
15
use SimpleSAML\XMLSchema\Type\StringValue;
16
17
use function strval;
18
19
/**
20
 * Class representing the field-element.
21
 *
22
 * @package simplesamlphp/xml-common
23
 */
24
final class Field extends AbstractAnnotated implements SchemaValidatableElementInterface
25
{
26
    use SchemaValidatableElementTrait;
27
28
29
    /** @var string */
30
    public const LOCALNAME = 'field';
31
32
33
    /** @var string */
34
    public static string $field_regex = '/^
35
        (
36
            (\.\/\/)?
37
            ((((child::)?(([_:A-Za-z][-._:A-Za-z0-9]*:)?([_:A-Za-z][-._:A-Za-z0-9]*|\*)))|\.)\/)*
38
            ((((child::)?(([_:A-Za-z][-._:A-Za-z0-9]*:)?([_:A-Za-z][-._:A-Za-z0-9]*|\*)))|\.)
39
                |((attribute::|@)(([_:A-Za-z][-._:A-Za-z0-9]*:)?([_:A-Za-z][-._:A-Za-z0-9]*|\*))))
40
            (\|(\.\/\/)?((((child::)?(([_:A-Za-z][-._:A-Za-z0-9]*:)?([_:A-Za-z][-._:A-Za-z0-9]*|\*)))|\.)\/)*
41
            ((((child::)?(([_:A-Za-z][-._:A-Za-z0-9]*:)?([_:A-Za-z][-._:A-Za-z0-9]*|\*)))|\.)
42
                |((attribute::|@)(([_:A-Za-z][-._:A-Za-z0-9]*:)?([_:A-Za-z][-._:A-Za-z0-9]*|\*)))))*
43
        )$/Dx';
44
45
46
    /**
47
     * Field constructor
48
     *
49
     * @param \SimpleSAML\XMLSchema\Type\StringValue $xpath
50
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
51
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
52
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
53
     */
54
    public function __construct(
55
        protected StringValue $xpath,
56
        ?Annotation $annotation = null,
57
        ?IDValue $id = null,
58
        array $namespacedAttributes = [],
59
    ) {
60
        Assert::regex(strval($xpath), self::$field_regex, SchemaViolationException::class);
61
62
        parent::__construct($annotation, $id, $namespacedAttributes);
63
    }
64
65
66
    /**
67
     * Collect the value of the xpath-property
68
     *
69
     * @return \SimpleSAML\XMLSchema\Type\StringValue
70
     */
71
    public function getXPath(): StringValue
72
    {
73
        return $this->xpath;
74
    }
75
76
77
    /**
78
     * Add this Field to an XML element.
79
     *
80
     * @param \DOMElement|null $parent The element we should append this field to.
81
     * @return \DOMElement
82
     */
83
    public function toXML(?DOMElement $parent = null): DOMElement
84
    {
85
        $e = parent::toXML($parent);
86
        $e->setAttribute('xpath', strval($this->getXPath()));
87
88
        return $e;
89
    }
90
91
92
    /**
93
     * Create an instance of this object from its XML representation.
94
     *
95
     * @param \DOMElement $xml
96
     * @return static
97
     *
98
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
99
     *   if the qualified name of the supplied element is wrong
100
     */
101
    public static function fromXML(DOMElement $xml): static
102
    {
103
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
104
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
105
106
        $annotation = Annotation::getChildrenOfClass($xml);
107
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
108
109
        return new static(
110
            self::getAttribute($xml, 'xpath', StringValue::class),
111
            array_pop($annotation),
112
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
113
            self::getAttributesNSFromXML($xml),
114
        );
115
    }
116
}
117