Issues (341)

src/XMLSchema/XML/SimpleChoice.php (1 issue)

Labels
Severity
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
    public const string LOCALNAME = 'choice';
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 28 at column 24
Loading history...
29
30
31
    /**
32
     * Create an instance of this object from its XML representation.
33
     *
34
     * @param \DOMElement $xml
35
     * @return static
36
     *
37
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
38
     *   if the qualified name of the supplied element is wrong
39
     */
40
    public static function fromXML(DOMElement $xml): static
41
    {
42
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
43
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
44
45
        // Prohibited attributes
46
        $name = self::getOptionalAttribute($xml, 'name', NCNameValue::class, null);
47
        Assert::null($name, SchemaViolationException::class);
48
49
        $ref = self::getOptionalAttribute($xml, 'ref', QNameValue::class, null);
50
        Assert::null($ref, SchemaViolationException::class);
51
52
        $minOccurs = self::getOptionalAttribute($xml, 'minOccurs', MinOccursValue::class, null);
53
        Assert::null($minOccurs, SchemaViolationException::class);
54
55
        $maxOccurs = self::getOptionalAttribute($xml, 'maxOccurs', MaxOccursValue::class, null);
56
        Assert::null($maxOccurs, SchemaViolationException::class);
57
58
        // Start here
59
        $annotation = Annotation::getChildrenOfClass($xml);
60
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
61
62
        $any = Any::getChildrenOfClass($xml);
63
        $choice = Choice::getChildrenOfClass($xml);
64
        $localElement = LocalElement::getChildrenOfClass($xml);
65
        $referencedGroup = ReferencedGroup::getChildrenOfClass($xml);
66
        $sequence = Sequence::getChildrenOfClass($xml);
67
68
        $particles = array_merge($any, $choice, $localElement, $referencedGroup, $sequence);
69
70
        return new static(
71
            nestedParticles: $particles,
72
            annotation: array_pop($annotation),
73
            id: self::getOptionalAttribute($xml, 'id', IDValue::class, null),
74
            namespacedAttributes: self::getAttributesNSFromXML($xml),
75
        );
76
    }
77
}
78