Issues (341)

src/XMLSchema/XML/Choice.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\XML\SchemaValidatableElementInterface;
10
use SimpleSAML\XML\SchemaValidatableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
13
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
14
use SimpleSAML\XMLSchema\Type\IDValue;
15
use SimpleSAML\XMLSchema\Type\NCNameValue;
16
use SimpleSAML\XMLSchema\Type\QNameValue;
17
use SimpleSAML\XMLSchema\Type\Schema\MaxOccursValue;
18
use SimpleSAML\XMLSchema\Type\Schema\MinOccursValue;
19
use SimpleSAML\XMLSchema\XML\Interface\NestedParticleInterface;
20
use SimpleSAML\XMLSchema\XML\Interface\TypeDefParticleInterface;
21
22
use function array_merge;
23
use function array_pop;
24
25
/**
26
 * Class representing the choice-element.
27
 *
28
 * @package simplesamlphp/xml-common
29
 */
30
final class Choice extends AbstractExplicitGroup implements
31
    NestedParticleInterface,
32
    SchemaValidatableElementInterface,
33
    TypeDefParticleInterface
34
{
35
    use SchemaValidatableElementTrait;
36
37
38
    public const string LOCALNAME = 'choice';
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 38 at column 24
Loading history...
39
40
41
    /**
42
     * Create an instance of this object from its XML representation.
43
     *
44
     * @param \DOMElement $xml
45
     * @return static
46
     *
47
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
48
     *   if the qualified name of the supplied element is wrong
49
     */
50
    public static function fromXML(DOMElement $xml): static
51
    {
52
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
53
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
54
55
        // Prohibited attributes
56
        $name = self::getOptionalAttribute($xml, 'name', NCNameValue::class, null);
57
        Assert::null($name, SchemaViolationException::class);
58
59
        $ref = self::getOptionalAttribute($xml, 'ref', QNameValue::class, null);
60
        Assert::null($ref, SchemaViolationException::class);
61
62
        $annotation = Annotation::getChildrenOfClass($xml);
63
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
64
65
        $any = Any::getChildrenOfClass($xml);
66
        $choice = Choice::getChildrenOfClass($xml);
67
        $localElement = LocalElement::getChildrenOfClass($xml);
68
        $referencedGroup = ReferencedGroup::getChildrenOfClass($xml);
69
        $sequence = Sequence::getChildrenOfClass($xml);
70
71
        $particles = array_merge($any, $choice, $localElement, $referencedGroup, $sequence);
72
73
        return new static(
74
            self::getOptionalAttribute($xml, 'minOccurs', MinOccursValue::class, null),
75
            self::getOptionalAttribute($xml, 'maxOccurs', MaxOccursValue::class, null),
76
            $particles,
77
            array_pop($annotation),
78
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
79
            self::getAttributesNSFromXML($xml),
80
        );
81
    }
82
}
83