RepublishRequest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 98
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getRepublishTarget() 0 3 1
A toXML() 0 7 1
A __construct() 0 3 1
A fromXML() 0 14 1
A toArray() 0 3 1
A fromArray() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\emd;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Exception\ArrayValidationException;
10
use SimpleSAML\SAML2\Type\SAMLAnyURIValue;
11
use SimpleSAML\XML\ArrayizableElementInterface;
12
use SimpleSAML\XML\SchemaValidatableElementInterface;
13
use SimpleSAML\XML\SchemaValidatableElementTrait;
14
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
15
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
16
17
use function array_pop;
18
19
/**
20
 * Class implementing RepublishRequest.
21
 *
22
 * @package simplesamlphp/saml2
23
 */
24
final class RepublishRequest extends AbstractEmdElement implements
25
    ArrayizableElementInterface,
26
    SchemaValidatableElementInterface
27
{
28
    use SchemaValidatableElementTrait;
29
30
31
    /**
32
     * @param \SimpleSAML\SAML2\XML\emd\RepublishTarget $republishTarget
33
     */
34
    public function __construct(
35
        protected RepublishTarget $republishTarget,
36
    ) {
37
    }
38
39
40
    /**
41
     * Collect the value of the RepublishTarget-property
42
     *
43
     * @return \SimpleSAML\SAML2\XML\emd\RepublishTarget
44
     */
45
    public function getRepublishTarget(): RepublishTarget
46
    {
47
        return $this->republishTarget;
48
    }
49
50
51
    /**
52
     * Convert XML into a RepublishRequest
53
     *
54
     * @param \DOMElement $xml The XML element we should load
55
     * @return static
56
     *
57
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
58
     *   if the qualified name of the supplied element is wrong
59
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
60
     *   if the supplied element is missing one of the mandatory attributes
61
     */
62
    public static function fromXML(DOMElement $xml): static
63
    {
64
        Assert::same($xml->localName, 'RepublishRequest', InvalidDOMElementException::class);
65
        Assert::same($xml->namespaceURI, RepublishRequest::NS, InvalidDOMElementException::class);
66
67
        $republishTarget = RepublishTarget::getChildrenOfClass($xml);
68
        Assert::count(
69
            $republishTarget,
70
            1,
71
            'A RepublishRequest can contain exactly one RepublishTarget.',
72
            SchemaViolationException::class,
73
        );
74
75
        return new static(array_pop($republishTarget));
76
    }
77
78
79
    /**
80
     * Convert this element to XML.
81
     *
82
     * @param \DOMElement|null $parent The element we should append to.
83
     * @return \DOMElement
84
     */
85
    public function toXML(?DOMElement $parent = null): DOMElement
86
    {
87
        $e = $this->instantiateParentElement($parent);
88
89
        $this->republishTarget->toXML($e);
90
91
        return $e;
92
    }
93
94
95
    /**
96
     * Create a class from an array
97
     *
98
     * @param array $data
99
     * @return static
100
     */
101
    public static function fromArray(array $data): static
102
    {
103
        Assert::keyExists($data, 'RepublishTarget', ArrayValidationException::class);
104
        Assert::string($data['RepublishTarget'], ArrayValidationException::class);
105
106
        return new static(
107
            new RepublishTarget(
108
                SAMLAnyURIValue::fromString($data['RepublishTarget']),
109
            ),
110
        );
111
    }
112
113
114
    /**
115
     * Create an array from this class
116
     *
117
     * @return array
118
     */
119
    public function toArray(): array
120
    {
121
        return ['RepublishTarget' => $this->getRepublishTarget()->getContent()->getValue()];
122
    }
123
}
124