AbstractRequestedReferenceType::toXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wst_200512;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\WSSecurity\XML\wsse\SecurityTokenReference;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\MissingElementException;
12
use SimpleSAML\XML\Exception\TooManyElementsException;
13
14
use function array_pop;
15
16
/**
17
 * Class defining the RequestedReferenceType element
18
 *
19
 * @package simplesamlphp/ws-security
20
 */
21
abstract class AbstractRequestedReferenceType extends AbstractWstElement
22
{
23
    /**
24
     * AbstractRequestedReferenceType constructor
25
     *
26
     * @param \SimpleSAML\WSSecurity\XML\wsse\SecurityTokenReference $securityTokenReference
27
     */
28
    final public function __construct(
29
        protected SecurityTokenReference $securityTokenReference,
30
    ) {
31
    }
32
33
34
    /**
35
     * Collect the value of the securityTokenReference property.
36
     *
37
     * @return \SimpleSAML\WSSecurity\XML\wsse\SecurityTokenReference
38
     */
39
    public function getSecurityTokenReference(): SecurityTokenReference
40
    {
41
        return $this->securityTokenReference;
42
    }
43
44
45
    /**
46
     * Create an instance of this object from its XML representation.
47
     *
48
     * @param \DOMElement $xml
49
     * @return static
50
     *
51
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
52
     *   if the qualified name of the supplied element is wrong
53
     */
54
    public static function fromXML(DOMElement $xml): static
55
    {
56
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
57
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
58
59
        $securityTokenReference = SecurityTokenReference::getChildrenOfClass($xml);
60
        Assert::minCount($securityTokenReference, 1, MissingElementException::class);
61
        Assert::maxCount($securityTokenReference, 1, TooManyElementsException::class);
62
63
        return new static(array_pop($securityTokenReference));
64
    }
65
66
67
    /**
68
     * Add this RequestedReferenceType to an XML element.
69
     *
70
     * @param \DOMElement|null $parent The element we should append this element to.
71
     * @return \DOMElement
72
     */
73
    public function toXML(?DOMElement $parent = null): DOMElement
74
    {
75
        $e = parent::instantiateParentElement($parent);
76
77
        $this->getSecurityTokenReference()->toXML($e);
78
79
        return $e;
80
    }
81
}
82