Issues (341)

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