Passed
Push — master ( 8413b2...00f101 )
by Tim
02:19
created

Field   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

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

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