Passed
Pull Request — master (#33)
by Tim
01:59
created

SignatureProperty::setId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Chunk;
10
use SimpleSAML\XML\Constants as C;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\MissingElementException;
13
use SimpleSAML\XML\Exception\SchemaViolationException;
14
use SimpleSAML\XML\ExtendableElementTrait;
15
use SimpleSAML\XML\SerializableElementInterface;
16
17
/**
18
 * Class representing a ds:SignatureProperty element.
19
 *
20
 * @package simplesamlphp/xml-security
21
 */
22
final class SignatureProperty extends AbstractDsElement
23
{
24
    use ExtendableElementTrait;
25
26
27
    /** The namespace-attribute for the xs:any element */
28
    public const NAMESPACE = C::XS_ANY_NS_OTHER;
29
30
    /** @var string $Target */
31
    protected string $Target;
32
33
    /** @var string|null $Id */
34
    protected ?string $Id;
35
36
37
    /**
38
     * Initialize a ds:SignatureProperty
39
     *
40
     * @param \SimpleSAML\XML\SerializableElementInterface[] $elements
41
     * @param string $Target
42
     * @param string|null $Id
43
     */
44
    public function __construct(
45
        array $elements,
46
        string $Target,
47
        ?string $Id = null
48
    ) {
49
        $this->setElements($elements);
50
        $this->setTarget($Target);
51
        $this->setId($Id);
52
    }
53
54
55
    /**
56
     * @return string
57
     */
58
    public function getTarget(): string
59
    {
60
        return $this->Target;
61
    }
62
63
64
    /**
65
     * @param string $Target
66
     */
67
    protected function setTarget(string $Target): void
68
    {
69
        Assert::validURI($Target, SchemaViolationException::class); // Covers the empty string
70
        $this->Target = $Target;
71
    }
72
73
74
    /**
75
     * @return string|null
76
     */
77
    public function getId(): ?string
78
    {
79
        return $this->Id;
80
    }
81
82
83
    /**
84
     * @param string|null $Id
85
     */
86
    private function setId(?string $Id): void
87
    {
88
        Assert::nullOrValidNCName($Id);
89
        $this->Id = $Id;
90
    }
91
92
93
    /**
94
     * Convert XML into a SignatureProperty element
95
     *
96
     * @param \DOMElement $xml The XML element we should load
97
     * @return static
98
     *
99
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
100
     *   If the qualified name of the supplied element is wrong
101
     */
102
    public static function fromXML(DOMElement $xml): static
103
    {
104
        Assert::same($xml->localName, 'SignatureProperty', InvalidDOMElementException::class);
105
        Assert::same($xml->namespaceURI, SignatureProperty::NS, InvalidDOMElementException::class);
106
107
        /** @psalm-var string $Target */
108
        $Target = self::getAttribute($xml, 'Target');
109
        $Id = self::getAttribute($xml, 'Id', null);
110
111
        $children = [];
112
        foreach ($xml->childNodes as $child) {
113
            if (!($child instanceof DOMElement)) {
114
                continue;
115
            }
116
117
            $children[] = new Chunk($child);
118
        }
119
120
        /** @psalm-var \SimpleSAML\XML\SerializableElementInterface[] $children */
121
        Assert::minCount(
122
            $children,
123
            1,
124
            'A <ds:SignatureProperty> must contain at least one element.',
125
            MissingElementException::class,
126
        );
127
128
        return new static(
129
            $children,
130
            $Target,
131
            $Id,
132
        );
133
    }
134
135
136
    /**
137
     * Convert this SignatureProperty element to XML.
138
     *
139
     * @param \DOMElement|null $parent The element we should append this SignatureProperty element to.
140
     * @return \DOMElement
141
     */
142
    public function toXML(DOMElement $parent = null): DOMElement
143
    {
144
        $e = $this->instantiateParentElement($parent);
145
        $e->setAttribute('Target', $this->Target);
146
147
        if ($this->Id !== null) {
148
            $e->setAttribute('Id', $this->Id);
149
        }
150
151
        Assert::allImplementsInterface($this->elements, SerializableElementInterface::class);
152
        /** @psalm-var \SimpleSAML\XML\SerializableElementInterface[] $this->elements */
153
        foreach ($this->elements as $element) {
154
            $e->appendChild($e->ownerDocument->importNode($element->toXML(), true));
0 ignored issues
show
Bug introduced by
The method importNode() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

154
            $e->appendChild($e->ownerDocument->/** @scrutinizer ignore-call */ importNode($element->toXML(), true));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
155
        }
156
157
        return $e;
158
    }
159
}
160