Issues (341)

src/XMLSchema/XML/Unique.php (1 issue)

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