Passed
Pull Request — master (#59)
by Tim
12:14
created

Salt::toXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\xenc11;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\MissingElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
13
use function array_merge;
14
use function array_pop;
15
16
/**
17
 * Class representing <xenc11:Salt>.
18
 *
19
 * @package simplesamlphp/xml-security
20
 */
21
final class Salt extends AbstractXenc11Element
22
{
23
    /**
24
     * Salt constructor.
25
     *
26
     * @param \SimpleSAML\XMLSecurity\XML\xenc11\OtherSource|\SimpleSAML\XMLSecurity\XML\xenc11\Specified $content
27
     */
28
    public function __construct(
29
        protected OtherSource|Specified $content,
30
    ) {
31
    }
32
33
34
    /**
35
     * Get the value of the $content property.
36
     *
37
     * @return \SimpleSAML\XMLSecurity\XML\xenc11\OtherSource|\SimpleSAML\XMLSecurity\XML\xenc11\Specified
38
     */
39
    public function getContent(): OtherSource|Specified
40
    {
41
        return $this->content;
42
    }
43
44
45
    /**
46
     * @inheritDoc
47
     *
48
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
49
     *   If the qualified name of the supplied element is wrong
50
     */
51
    public static function fromXML(DOMElement $xml): static
52
    {
53
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
54
        Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
55
56
        $otherSource = OtherSource::getChildrenOfClass($xml);
57
        $specified = Specified::getChildrenOfClass($xml);
58
59
        $content = array_merge($otherSource, $specified);
60
        Assert::minCount($content, 1, MissingElementException::class);
61
        Assert::maxCount($content, 1, TooManyElementsException::class);
62
63
        return new static(array_pop($content));
64
    }
65
66
67
    /**
68
     * @inheritDoc
69
     */
70
    public function toXML(?DOMElement $parent = null): DOMElement
71
    {
72
        $e = $this->instantiateParentElement($parent);
73
        $this->getContent()->toXML($e);
74
75
        return $e;
76
    }
77
}
78