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

Unique::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
nc 1
nop 1
dl 0
loc 21
rs 9.7666
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, MissingElementException, TooManyElementsException};
11
use SimpleSAML\XMLSchema\Type\Builtin\{IDValue, NCNameValue};
12
13
use function array_pop;
14
15
/**
16
 * Class representing the unique-element.
17
 *
18
 * @package simplesamlphp/xml-common
19
 */
20
final class Unique extends AbstractKeybase implements IdentityConstraintInterface, SchemaValidatableElementInterface
21
{
22
    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\Unique: $message, $line
Loading history...
23
24
    /** @var string */
25
    public const LOCALNAME = 'unique';
26
27
28
    /**
29
     * Unique constructor
30
     *
31
     * @param \SimpleSAML\XMLSchema\Type\Builtin\NCNameValue $name
32
     * @param \SimpleSAML\XMLSchema\XML\xs\Selector $selector
33
     * @param array<\SimpleSAML\XMLSchema\XML\xs\Field> $field
34
     * @param \SimpleSAML\XMLSchema\XML\xs\Annotation|null $annotation
35
     * @param \SimpleSAML\XMLSchema\Type\Builtin\IDValue|null $id
36
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
37
     */
38
    public function __construct(
39
        NCNameValue $name,
40
        Selector $selector,
41
        array $field = [],
42
        ?Annotation $annotation = null,
43
        ?IDValue $id = null,
44
        array $namespacedAttributes = [],
45
    ) {
46
        parent::__construct($name, $selector, $field, $annotation, $id, $namespacedAttributes);
47
    }
48
49
50
    /**
51
     * Create an instance of this object from its XML representation.
52
     *
53
     * @param \DOMElement $xml
54
     * @return static
55
     *
56
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
57
     *   if the qualified name of the supplied element is wrong
58
     */
59
    public static function fromXML(DOMElement $xml): static
60
    {
61
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
62
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
63
64
        $annotation = Annotation::getChildrenOfClass($xml);
65
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
66
67
        $selector = Selector::getChildrenOfClass($xml);
68
        Assert::minCount($selector, 1, MissingElementException::class);
69
        Assert::maxCount($selector, 1, TooManyElementsException::class);
70
71
        $field = Field::getChildrenOfClass($xml);
72
73
        return new static(
74
            self::getAttribute($xml, 'name', NCNameValue::class),
75
            $selector[0],
76
            $field,
77
            array_pop($annotation),
78
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
79
            self::getAttributesNSFromXML($xml),
80
        );
81
    }
82
}
83