Issues (341)

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