Issues (341)

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