Passed
Pull Request — master (#61)
by Tim
02:17
created

NamedGroup   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 55
rs 10
c 1
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromXML() 0 36 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\XML\xs;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XMLSchema\Exception\{
10
    InvalidDOMElementException,
11
    MissingElementException,
12
    SchemaViolationException,
13
    TooManyElementsException,
14
};
15
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
16
use SimpleSAML\XMLSchema\Type\Builtin\{IDValue, NCNameValue, QNameValue};
17
use SimpleSAML\XMLSchema\Type\{MinOccursValue, MaxOccursValue};
18
19
use function array_merge;
20
use function array_pop;
21
22
/**
23
 * Class representing the group-element.
24
 *
25
 * @package simplesamlphp/xml-common
26
 */
27
final class NamedGroup extends AbstractNamedGroup implements
28
    RedefinableInterface,
29
    SchemaValidatableElementInterface
30
{
31
    use SchemaValidatableElementTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSchema\XML\xs\NamedGroup: $message, $line
Loading history...
32
33
    /** @var string */
34
    public const LOCALNAME = 'group';
35
36
37
    /**
38
     * Create an instance of this object from its XML representation.
39
     *
40
     * @param \DOMElement $xml
41
     * @return static
42
     *
43
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
44
     *   if the qualified name of the supplied element is wrong
45
     */
46
    public static function fromXML(DOMElement $xml): static
47
    {
48
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
49
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
50
51
        // Prohibited attributes
52
        $ref = self::getOptionalAttribute($xml, 'ref', QNameValue::class, null);
53
        Assert::null($ref, SchemaViolationException::class);
54
55
        $minCount = self::getOptionalAttribute($xml, 'minCount', MinOccursValue::class, null);
56
        Assert::null($minCount, SchemaViolationException::class);
57
58
        $maxCount = self::getOptionalAttribute($xml, 'maxCount', MaxOccursValue::class, null);
59
        Assert::null($maxCount, SchemaViolationException::class);
60
61
        $annotation = Annotation::getChildrenOfClass($xml);
62
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
63
64
        $any = All::getChildrenOfClass($xml);
65
        Assert::maxCount($any, 1, TooManyElementsException::class);
66
67
        $choice = Choice::getChildrenOfClass($xml);
68
        Assert::maxCount($choice, 1, TooManyElementsException::class);
69
70
        $sequence = Sequence::getChildrenOfClass($xml);
71
        Assert::maxCount($sequence, 1, TooManyElementsException::class);
72
73
        $particle = array_merge($any, $choice, $sequence);
74
        Assert::maxCount($particle, 1, TooManyElementsException::class);
75
76
        return new static(
77
            $particle[0],
78
            name: self::getAttribute($xml, 'name', NCNameValue::class),
79
            annotation: array_pop($annotation),
80
            id: self::getOptionalAttribute($xml, 'id', IDValue::class, null),
81
            namespacedAttributes: self::getAttributesNSFromXML($xml),
82
        );
83
    }
84
}
85