SimpleChoice::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 23
nc 1
nop 1
dl 0
loc 35
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\XML;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
10
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
11
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
12
use SimpleSAML\XMLSchema\Type\IDValue;
13
use SimpleSAML\XMLSchema\Type\NCNameValue;
14
use SimpleSAML\XMLSchema\Type\QNameValue;
15
use SimpleSAML\XMLSchema\Type\Schema\MaxOccursValue;
16
use SimpleSAML\XMLSchema\Type\Schema\MinOccursValue;
17
18
use function array_merge;
19
use function array_pop;
20
21
/**
22
 * Class representing the choice-element.
23
 *
24
 * @package simplesamlphp/xml-common
25
 */
26
final class SimpleChoice extends AbstractSimpleExplicitGroup
27
{
28
    /** @var string */
29
    public const LOCALNAME = 'choice';
30
31
32
    /**
33
     * Create an instance of this object from its XML representation.
34
     *
35
     * @param \DOMElement $xml
36
     * @return static
37
     *
38
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
39
     *   if the qualified name of the supplied element is wrong
40
     */
41
    public static function fromXML(DOMElement $xml): static
42
    {
43
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
44
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
45
46
        // Prohibited attributes
47
        $name = self::getOptionalAttribute($xml, 'name', NCNameValue::class, null);
48
        Assert::null($name, SchemaViolationException::class);
49
50
        $ref = self::getOptionalAttribute($xml, 'ref', QNameValue::class, null);
51
        Assert::null($ref, SchemaViolationException::class);
52
53
        $minOccurs = self::getOptionalAttribute($xml, 'minOccurs', MinOccursValue::class, null);
54
        Assert::null($minOccurs, SchemaViolationException::class);
55
56
        $maxOccurs = self::getOptionalAttribute($xml, 'maxOccurs', MaxOccursValue::class, null);
57
        Assert::null($maxOccurs, SchemaViolationException::class);
58
59
        // Start here
60
        $annotation = Annotation::getChildrenOfClass($xml);
61
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
62
63
        $any = Any::getChildrenOfClass($xml);
64
        $choice = Choice::getChildrenOfClass($xml);
65
        $localElement = LocalElement::getChildrenOfClass($xml);
66
        $referencedGroup = ReferencedGroup::getChildrenOfClass($xml);
67
        $sequence = Sequence::getChildrenOfClass($xml);
68
69
        $particles = array_merge($any, $choice, $localElement, $referencedGroup, $sequence);
70
71
        return new static(
72
            nestedParticles: $particles,
73
            annotation: array_pop($annotation),
74
            id: self::getOptionalAttribute($xml, 'id', IDValue::class, null),
75
            namespacedAttributes: self::getAttributesNSFromXML($xml),
76
        );
77
    }
78
}
79