simplesamlphp /
xml-common
| 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\Type\Schema\FormChoiceValue; |
||
| 18 | use SimpleSAML\XMLSchema\Type\Schema\UseValue; |
||
| 19 | use SimpleSAML\XMLSchema\Type\StringValue; |
||
| 20 | use SimpleSAML\XMLSchema\XML\Interface\SchemaTopInterface; |
||
| 21 | |||
| 22 | use function array_pop; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Class representing the attribute-element. |
||
| 26 | * |
||
| 27 | * @package simplesamlphp/xml-common |
||
| 28 | */ |
||
| 29 | final class TopLevelAttribute extends AbstractTopLevelAttribute implements |
||
| 30 | SchemaTopInterface, |
||
| 31 | SchemaValidatableElementInterface |
||
| 32 | { |
||
| 33 | use SchemaValidatableElementTrait; |
||
| 34 | |||
| 35 | |||
| 36 | public const string LOCALNAME = 'attribute'; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * Create an instance of this object from its XML representation. |
||
| 41 | * |
||
| 42 | * @param \DOMElement $xml |
||
| 43 | * @return static |
||
| 44 | * |
||
| 45 | * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException |
||
| 46 | * if the qualified name of the supplied element is wrong |
||
| 47 | */ |
||
| 48 | public static function fromXML(DOMElement $xml): static |
||
| 49 | { |
||
| 50 | Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
||
| 51 | Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
||
| 52 | |||
| 53 | // Prohibited attributes |
||
| 54 | $ref = self::getOptionalAttribute($xml, 'ref', QNameValue::class, null); |
||
| 55 | Assert::null($ref, SchemaViolationException::class); |
||
| 56 | |||
| 57 | $form = self::getOptionalAttribute($xml, 'form', FormChoiceValue::class, null); |
||
| 58 | Assert::null($form, SchemaViolationException::class); |
||
| 59 | |||
| 60 | $use = self::getOptionalAttribute($xml, 'use', UseValue::class, null); |
||
| 61 | Assert::null($use, SchemaViolationException::class); |
||
| 62 | |||
| 63 | $annotation = Annotation::getChildrenOfClass($xml); |
||
| 64 | Assert::maxCount($annotation, 1, TooManyElementsException::class); |
||
| 65 | |||
| 66 | $simpleType = LocalSimpleType::getChildrenOfClass($xml); |
||
| 67 | Assert::maxCount($simpleType, 1, TooManyElementsException::class); |
||
| 68 | |||
| 69 | return new static( |
||
| 70 | self::getAttribute($xml, 'name', NCNameValue::class), |
||
| 71 | self::getOptionalAttribute($xml, 'type', QNameValue::class, null), |
||
| 72 | self::getOptionalAttribute($xml, 'default', StringValue::class, null), |
||
| 73 | self::getOptionalAttribute($xml, 'fixed', StringValue::class, null), |
||
| 74 | array_pop($simpleType), |
||
| 75 | array_pop($annotation), |
||
| 76 | self::getOptionalAttribute($xml, 'id', IDValue::class, null), |
||
| 77 | self::getAttributesNSFromXML($xml), |
||
| 78 | ); |
||
| 79 | } |
||
| 80 | } |
||
| 81 |