NamedAttributeGroup   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 19
dl 0
loc 45
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromXML() 0 25 1
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\XML\Interface\RedefinableInterface;
18
19
use function array_merge;
20
use function array_pop;
21
22
/**
23
 * Class representing the attributeGroup-element.
24
 *
25
 * @package simplesamlphp/xml-common
26
 */
27
final class NamedAttributeGroup extends AbstractNamedAttributeGroup implements
28
    RedefinableInterface,
29
    SchemaValidatableElementInterface
30
{
31
    use SchemaValidatableElementTrait;
32
33
34
    /** @var string */
35
    public const LOCALNAME = 'attributeGroup';
36
37
38
    /**
39
     * Create an instance of this object from its XML representation.
40
     *
41
     * @param \DOMElement $xml
42
     * @return static
43
     *
44
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
45
     *   if the qualified name of the supplied element is wrong
46
     */
47
    public static function fromXML(DOMElement $xml): static
48
    {
49
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
50
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
51
52
        // Prohibited attributes
53
        $ref = self::getOptionalAttribute($xml, 'ref', QNameValue::class, null);
54
        Assert::null($ref, SchemaViolationException::class);
55
56
        $annotation = Annotation::getChildrenOfClass($xml);
57
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
58
59
        $localAttribute = LocalAttribute::getChildrenOfClass($xml);
60
        $attributeGroup = ReferencedAttributeGroup::getChildrenOfClass($xml);
61
        $attributes = array_merge($localAttribute, $attributeGroup);
62
63
        $anyAttribute = AnyAttribute::getChildrenOfClass($xml);
64
65
        return new static(
66
            self::getAttribute($xml, 'name', NCNameValue::class),
67
            $attributes,
68
            array_pop($anyAttribute),
69
            array_pop($annotation),
70
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
71
            self::getAttributesNSFromXML($xml),
72
        );
73
    }
74
}
75