AbstractRequestTypeOpenEnum::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
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\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
use SimpleSAML\XML\StringElementTrait;
12
13
use function array_map;
14
use function explode;
15
use function implode;
16
17
/**
18
 * A RequestTypeOpenEnum element
19
 *
20
 * @package simplesamlphp/ws-security
21
 *
22
 * @phpstan-consistent-constructor
23
 */
24
abstract class AbstractRequestTypeOpenEnum extends AbstractWstElement
25
{
26
    use StringElementTrait;
27
28
29
    /**
30
     * @param (\SimpleSAML\WSSecurity\XML\wst_200512\RequestTypeEnum|string)[] $values
31
     */
32
    public function __construct(array $values)
33
    {
34
        $values = array_map(
35
            function (RequestTypeEnum|string $v): string {
36
                return ($v instanceof RequestTypeEnum) ? $v->value : $v;
37
            },
38
            $values,
39
        );
40
        Assert::allValidURI($values, SchemaViolationException::class);
41
42
        $this->setContent(implode(' ', $values));
43
    }
44
45
46
    /**
47
     * Convert XML into a class instance
48
     *
49
     * @param \DOMElement $xml The XML element we should load
50
     * @return static
51
     *
52
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
53
     *   If the qualified name of the supplied element is wrong
54
     */
55
    public static function fromXML(DOMElement $xml): static
56
    {
57
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
58
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
59
60
        return new static(explode(' ', $xml->textContent));
61
    }
62
}
63