Passed
Push — master ( 7a371e...4a8d98 )
by Tim
02:17
created

RegistrationInfo::getRegistrationInstant()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\mdrpi;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Utils as XMLUtils;
12
13
/**
14
 * Class for handling the mdrpi:RegistrationInfo element.
15
 *
16
 * @link: http://docs.oasis-open.org/security/saml/Post2.0/saml-metadata-rpi/v1.0/saml-metadata-rpi-v1.0.pdf
17
 * @package simplesamlphp/saml2
18
 */
19
final class RegistrationInfo extends AbstractMdrpiElement
20
{
21
    /**
22
     * Create/parse a mdrpi:RegistrationInfo element.
23
     *
24
     * @param string $registrationAuthority
25
     * @param int|null $registrationInstant
26
     * @param \SimpleSAML\SAML2\XML\mdrpi\RegistrationPolicy[] $RegistrationPolicy
27
     */
28
    public function __construct(
29
        protected string $registrationAuthority,
30
        protected ?int $registrationInstant = null,
31
        protected array $registrationPolicy = [],
32
    ) {
33
        Assert::allIsInstanceOf($registrationPolicy, RegistrationPolicy::class);
34
35
        /**
36
         * 2.1.1:  There MUST NOT be more than one <mdrpi:RegistrationPolicy>,
37
         *         within a given <mdrpi:RegistrationInfo>, for a given language
38
         */
39
        $languages = array_map(
40
            function ($rp) {
41
                return $rp->getLanguage();
42
            },
43
            $registrationPolicy,
44
        );
45
        Assert::uniqueValues(
46
            $languages,
47
            'There MUST NOT be more than one <mdrpi:RegistrationPolicy>,'
48
            . ' within a given <mdrpi:RegistrationInfo>, for a given language',
49
            ProtocolViolationException::class,
50
        );
51
    }
52
53
54
    /**
55
     * Collect the value of the RegistrationAuthority property
56
     *
57
     * @return string
58
     */
59
    public function getRegistrationAuthority(): string
60
    {
61
        return $this->registrationAuthority;
62
    }
63
64
65
    /**
66
     * Collect the value of the registrationInstant property
67
     *
68
     * @return int|null
69
     */
70
    public function getRegistrationInstant(): ?int
71
    {
72
        return $this->registrationInstant;
73
    }
74
75
76
    /**
77
     * Collect the value of the RegistrationPolicy property
78
     *
79
     * @return \SimpleSAML\SAML2\XML\mdrpi\RegistrationPolicy[]
80
     */
81
    public function getRegistrationPolicy(): array
82
    {
83
        return $this->registrationPolicy;
84
    }
85
86
87
    /**
88
     * Convert XML into a RegistrationInfo
89
     *
90
     * @param \DOMElement $xml The XML element we should load
91
     * @return self
92
     *
93
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
94
     *   if the qualified name of the supplied element is wrong
95
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
96
     *   if the supplied element is missing one of the mandatory attributes
97
     */
98
    public static function fromXML(DOMElement $xml): static
99
    {
100
        Assert::same($xml->localName, 'RegistrationInfo', InvalidDOMElementException::class);
101
        Assert::same($xml->namespaceURI, RegistrationInfo::NS, InvalidDOMElementException::class);
102
103
        $registrationAuthority = self::getAttribute($xml, 'registrationAuthority');
104
        $registrationInstant = self::getAttribute($xml, 'registrationInstant', null);
105
106
        // 2.1.1:  Time values MUST be expressed in the UTC timezone using the 'Z' timezone identifier
107
        if ($registrationInstant !== null) {
108
            // Strip sub-seconds - See paragraph 1.3.3 of SAML core specifications
109
            $registrationInstant = preg_replace('/([.][0-9]+Z)$/', 'Z', $registrationInstant, 1);
110
111
            Assert::validDateTimeZulu($registrationInstant, ProtocolViolationException::class);
112
            $registrationInstant = XMLUtils::xsDateTimeToTimestamp($registrationInstant);
113
        }
114
        $RegistrationPolicy = RegistrationPolicy::getChildrenOfClass($xml);
115
116
        return new static($registrationAuthority, $registrationInstant, $RegistrationPolicy);
117
    }
118
119
120
    /**
121
     * Convert this element to XML.
122
     *
123
     * @param \DOMElement|null $parent The element we should append to.
124
     * @return \DOMElement
125
     */
126
    public function toXML(DOMElement $parent = null): DOMElement
127
    {
128
        $e = $this->instantiateParentElement($parent);
129
        $e->setAttribute('registrationAuthority', $this->getRegistrationAuthority());
130
131
        if ($this->getRegistrationInstant() !== null) {
132
            $e->setAttribute('registrationInstant', gmdate('Y-m-d\TH:i:s\Z', $this->getRegistrationInstant()));
133
        }
134
135
        foreach ($this->getRegistrationPolicy() as $rp) {
136
            $rp->toXML($e);
137
        }
138
139
        return $e;
140
    }
141
142
143
    /**
144
     * Create a class from an array
145
     *
146
     * @param array $data
147
     * @return self
148
     */
149
    public static function fromArray(array $data): static
150
    {
151
        Assert::keyExists($data, 'registrationAuthority');
152
153
        $registrationAuthority = $data['registrationAuthority'];
154
        Assert::string($registrationAuthority);
155
156
        $registrationInstant = $data['registrationInstant'] ?? null;
157
        Assert::nullOrInteger($registrationInstant);
158
159
        $rp = $data['registrationPolicy'] ?? [];
160
        Assert::isArray($rp);
161
162
        $registrationPolicy = [];
163
        foreach ($rp as $k => $v) {
164
            $registrationPolicy[] = RegistrationPolicy::fromArray([$k => $v]);
165
        }
166
167
        return new static($registrationAuthority, $registrationInstant, $registrationPolicy);
168
    }
169
170
171
    /**
172
     * Create an array from this class
173
     *
174
     * @return array
175
     */
176
    public function toArray(): array
177
    {
178
        $data = [];
179
        $data['registrationAuthority'] = $this->getRegistrationAuthority();
180
181
        if ($this->getRegistrationInstant() !== null) {
182
            $data['registrationInstant'] = $this->getRegistrationInstant();
183
        }
184
185
        if (!empty($this->getRegistrationPolicy())) {
186
            $data['registrationPolicy'] = [];
187
            foreach ($this->getRegistrationPolicy() as $rp) {
188
                $data['registrationPolicy'] = array_merge($data['registrationPolicy'], $rp->toArray());
189
            }
190
        }
191
192
        return $data;
193
    }
194
}
195