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

NamedAttributeGroup::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
nc 1
nop 1
dl 0
loc 25
rs 9.7
c 1
b 0
f 0
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\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
10
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, SchemaViolationException, TooManyElementsException};
11
use SimpleSAML\XMLSchema\Type\Builtin\{IDValue, NCNameValue, QNameValue};
12
13
use function array_merge;
14
use function array_pop;
15
16
/**
17
 * Class representing the attributeGroup-element.
18
 *
19
 * @package simplesamlphp/xml-common
20
 */
21
final class NamedAttributeGroup extends AbstractNamedAttributeGroup implements
22
    RedefinableInterface,
23
    SchemaValidatableElementInterface
24
{
25
    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\NamedAttributeGroup: $message, $line
Loading history...
26
27
    /** @var string */
28
    public const LOCALNAME = 'attributeGroup';
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
        $ref = self::getOptionalAttribute($xml, 'ref', QNameValue::class, null);
47
        Assert::null($ref, SchemaViolationException::class);
48
49
        $annotation = Annotation::getChildrenOfClass($xml);
50
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
51
52
        $localAttribute = LocalAttribute::getChildrenOfClass($xml);
53
        $attributeGroup = ReferencedAttributeGroup::getChildrenOfClass($xml);
54
        $attributes = array_merge($localAttribute, $attributeGroup);
55
56
        $anyAttribute = AnyAttribute::getChildrenOfClass($xml);
57
58
        return new static(
59
            self::getAttribute($xml, 'name', NCNameValue::class),
60
            $attributes,
61
            array_pop($anyAttribute),
62
            array_pop($annotation),
63
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
64
            self::getAttributesNSFromXML($xml),
65
        );
66
    }
67
}
68