Issues (81)

src/XML/ds/SignatureProperty.php (1 issue)

Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\XML\ExtendableElementTrait;
9
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
10
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, MissingElementException, SchemaViolationException};
11
use SimpleSAML\XMLSchema\Type\{AnyURIValue, IDValue};
12
use SimpleSAML\XMLSchema\XML\Enumeration\NamespaceEnum;
13
use SimpleSAML\XMLSecurity\Assert\Assert;
14
15
use function strval;
16
17
/**
18
 * Class representing a ds:SignatureProperty element.
19
 *
20
 * @package simplesamlphp/xml-security
21
 */
22
final class SignatureProperty extends AbstractDsElement implements SchemaValidatableElementInterface
23
{
24
    use ExtendableElementTrait;
25
    use SchemaValidatableElementTrait;
0 ignored issues
show
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\SignatureProperty: $message, $line
Loading history...
26
27
    /** The namespace-attribute for the xs:any element */
28
    public const XS_ANY_ELT_NAMESPACE = NamespaceEnum::Other;
29
30
31
    /**
32
     * Initialize a ds:SignatureProperty
33
     *
34
     * @param \SimpleSAML\XML\SerializableElementInterface[] $elements
35
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $Target
36
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $Id
37
     */
38
    public function __construct(
39
        array $elements,
40
        protected AnyURIValue $Target,
41
        protected ?IDValue $Id = null,
42
    ) {
43
        $this->setElements($elements);
44
    }
45
46
47
    /**
48
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue
49
     */
50
    public function getTarget(): AnyURIValue
51
    {
52
        return $this->Target;
53
    }
54
55
56
    /**
57
     * @return \SimpleSAML\XMLSchema\Type\IDValue|null
58
     */
59
    public function getId(): ?IDValue
60
    {
61
        return $this->Id;
62
    }
63
64
65
    /**
66
     * Convert XML into a SignatureProperty element
67
     *
68
     * @param \DOMElement $xml The XML element we should load
69
     * @return static
70
     *
71
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
72
     *   If the qualified name of the supplied element is wrong
73
     */
74
    public static function fromXML(DOMElement $xml): static
75
    {
76
        Assert::same($xml->localName, 'SignatureProperty', InvalidDOMElementException::class);
77
        Assert::same($xml->namespaceURI, SignatureProperty::NS, InvalidDOMElementException::class);
78
79
        $children = self::getChildElementsFromXML($xml);
80
        Assert::minCount(
81
            $children,
82
            1,
83
            'A <ds:SignatureProperty> must contain at least one element.',
84
            MissingElementException::class,
85
        );
86
87
        return new static(
88
            $children,
89
            self::getAttribute($xml, 'Target', AnyURIValue::class),
90
            self::getOptionalAttribute($xml, 'Id', IDValue::class, null),
91
        );
92
    }
93
94
95
    /**
96
     * Convert this SignatureProperty element to XML.
97
     *
98
     * @param \DOMElement|null $parent The element we should append this SignatureProperty element to.
99
     * @return \DOMElement
100
     */
101
    public function toXML(?DOMElement $parent = null): DOMElement
102
    {
103
        $e = $this->instantiateParentElement($parent);
104
        $e->setAttribute('Target', strval($this->getTarget()));
105
106
        if ($this->getId() !== null) {
107
            $e->setAttribute('Id', strval($this->getId()));
108
        }
109
110
        foreach ($this->getElements() as $element) {
111
            $element->toXML($e);
112
        }
113
114
        return $e;
115
    }
116
}
117