Unique   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

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