Completed
Push — master ( f72cfb...4ef12f )
by Tim
21s queued 18s
created

Keyref::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
nc 1
nop 1
dl 0
loc 21
rs 9.7666
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\XML\xs;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
10
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, TooManyElementsException};
11
use SimpleSAML\XMLSchema\Type\Builtin\{IDValue, NCNameValue, QNameValue};
12
13
use function strval;
14
15
/**
16
 * Class representing the keyref-element.
17
 *
18
 * @package simplesamlphp/xml-common
19
 */
20
final class Keyref extends AbstractKeybase implements IdentityConstraintInterface, 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\xs\Keyref: $message, $line
Loading history...
23
24
    /** @var string */
25
    public const LOCALNAME = 'keyref';
26
27
28
    /**
29
     * Keyref constructor
30
     *
31
     * @param \SimpleSAML\XMLSchema\Type\Builtin\QNameValue $refer
32
     * @param \SimpleSAML\XMLSchema\Type\Builtin\NCNameValue $name
33
     * @param \SimpleSAML\XMLSchema\XML\xs\Selector $selector
34
     * @param array<\SimpleSAML\XMLSchema\XML\xs\Field> $field
35
     * @param \SimpleSAML\XMLSchema\XML\xs\Annotation|null $annotation
36
     * @param \SimpleSAML\XMLSchema\Type\Builtin\IDValue|null $id
37
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
38
     */
39
    public function __construct(
40
        protected QNameValue $refer,
41
        NCNameValue $name,
42
        Selector $selector,
43
        array $field = [],
44
        ?Annotation $annotation = null,
45
        ?IDValue $id = null,
46
        array $namespacedAttributes = [],
47
    ) {
48
        parent::__construct($name, $selector, $field, $annotation, $id, $namespacedAttributes);
49
    }
50
51
52
    /**
53
     * Collect the value of the refer-property
54
     *
55
     * @return \SimpleSAML\XMLSchema\Type\Builtin\QNameValue
56
     */
57
    public function getRefer(): QNameValue
58
    {
59
        return $this->refer;
60
    }
61
62
63
    /**
64
     * Add this Keyref to an XML element.
65
     *
66
     * @param \DOMElement|null $parent The element we should append this Keyref to.
67
     * @return \DOMElement
68
     */
69
    public function toXML(?DOMElement $parent = null): DOMElement
70
    {
71
        $e = parent::toXML($parent);
72
        $e->setAttribute('refer', strval($this->getRefer()));
73
74
        return $e;
75
    }
76
77
78
    /**
79
     * Create an instance of this object from its XML representation.
80
     *
81
     * @param \DOMElement $xml
82
     * @return static
83
     *
84
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
85
     *   if the qualified name of the supplied element is wrong
86
     */
87
    public static function fromXML(DOMElement $xml): static
88
    {
89
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
90
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
91
92
        $annotation = Annotation::getChildrenOfClass($xml);
93
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
94
95
        $selector = Selector::getChildrenOfClass($xml);
96
        Assert::maxCount($selector, 1, TooManyElementsException::class);
97
98
        $field = Field::getChildrenOfClass($xml);
99
100
        return new static(
101
            self::getAttribute($xml, 'refer', QNameValue::class),
102
            self::getAttribute($xml, 'name', NCNameValue::class),
103
            $selector[0],
104
            $field,
105
            array_pop($annotation),
106
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
107
            self::getAttributesNSFromXML($xml),
108
        );
109
    }
110
}
111