Issues (341)

src/XMLSchema/XML/Field.php (1 issue)

Labels
Severity
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
    public const string LOCALNAME = 'field';
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 29 at column 24
Loading history...
30
31
32
    public static string $field_regex = '/^
33
        (
34
            (\.\/\/)?
35
            ((((child::)?(([_:A-Za-z][-._:A-Za-z0-9]*:)?([_:A-Za-z][-._:A-Za-z0-9]*|\*)))|\.)\/)*
36
            ((((child::)?(([_:A-Za-z][-._:A-Za-z0-9]*:)?([_:A-Za-z][-._:A-Za-z0-9]*|\*)))|\.)
37
                |((attribute::|@)(([_: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
            ((((child::)?(([_:A-Za-z][-._:A-Za-z0-9]*:)?([_:A-Za-z][-._:A-Za-z0-9]*|\*)))|\.)
40
                |((attribute::|@)(([_:A-Za-z][-._:A-Za-z0-9]*:)?([_:A-Za-z][-._:A-Za-z0-9]*|\*)))))*
41
        )$/Dx';
42
43
44
    /**
45
     * Field constructor
46
     *
47
     * @param \SimpleSAML\XMLSchema\Type\StringValue $xpath
48
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
49
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
50
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
51
     */
52
    public function __construct(
53
        protected StringValue $xpath,
54
        ?Annotation $annotation = null,
55
        ?IDValue $id = null,
56
        array $namespacedAttributes = [],
57
    ) {
58
        Assert::regex(strval($xpath), self::$field_regex, SchemaViolationException::class);
59
60
        parent::__construct($annotation, $id, $namespacedAttributes);
61
    }
62
63
64
    /**
65
     * Collect the value of the xpath-property
66
     *
67
     * @return \SimpleSAML\XMLSchema\Type\StringValue
68
     */
69
    public function getXPath(): StringValue
70
    {
71
        return $this->xpath;
72
    }
73
74
75
    /**
76
     * Add this Field to an XML element.
77
     *
78
     * @param \DOMElement|null $parent The element we should append this field to.
79
     * @return \DOMElement
80
     */
81
    public function toXML(?DOMElement $parent = null): DOMElement
82
    {
83
        $e = parent::toXML($parent);
84
        $e->setAttribute('xpath', strval($this->getXPath()));
85
86
        return $e;
87
    }
88
89
90
    /**
91
     * Create an instance of this object from its XML representation.
92
     *
93
     * @param \DOMElement $xml
94
     * @return static
95
     *
96
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
97
     *   if the qualified name of the supplied element is wrong
98
     */
99
    public static function fromXML(DOMElement $xml): static
100
    {
101
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
102
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
103
104
        $annotation = Annotation::getChildrenOfClass($xml);
105
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
106
107
        return new static(
108
            self::getAttribute($xml, 'xpath', StringValue::class),
109
            array_pop($annotation),
110
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
111
            self::getAttributesNSFromXML($xml),
112
        );
113
    }
114
}
115