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\BooleanValue; |
||
| 15 | use SimpleSAML\XMLSchema\Type\IDValue; |
||
| 16 | use SimpleSAML\XMLSchema\Type\NCNameValue; |
||
| 17 | use SimpleSAML\XMLSchema\Type\QNameValue; |
||
| 18 | use SimpleSAML\XMLSchema\Type\Schema\BlockSetValue; |
||
| 19 | use SimpleSAML\XMLSchema\Type\Schema\DerivationSetValue; |
||
| 20 | use SimpleSAML\XMLSchema\Type\Schema\FormChoiceValue; |
||
| 21 | use SimpleSAML\XMLSchema\Type\Schema\MaxOccursValue; |
||
| 22 | use SimpleSAML\XMLSchema\Type\Schema\MinOccursValue; |
||
| 23 | use SimpleSAML\XMLSchema\Type\StringValue; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Class representing the topLevelElement-type. |
||
| 27 | * |
||
| 28 | * @package simplesamlphp/xml-common |
||
| 29 | */ |
||
| 30 | final class TopLevelElement extends AbstractTopLevelElement implements SchemaValidatableElementInterface |
||
| 31 | { |
||
| 32 | use SchemaValidatableElementTrait; |
||
| 33 | |||
| 34 | |||
| 35 | public const string LOCALNAME = 'element'; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * Create an instance of this object from its XML representation. |
||
| 40 | * |
||
| 41 | * @param \DOMElement $xml |
||
| 42 | * @return static |
||
| 43 | * |
||
| 44 | * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException |
||
| 45 | * if the qualified name of the supplied element is wrong |
||
| 46 | */ |
||
| 47 | public static function fromXML(DOMElement $xml): static |
||
| 48 | { |
||
| 49 | Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
||
| 50 | Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
||
| 51 | |||
| 52 | // Prohibited attributes |
||
| 53 | $ref = self::getOptionalAttribute($xml, 'ref', QNameValue::class, null); |
||
| 54 | Assert::null($ref, SchemaViolationException::class); |
||
| 55 | |||
| 56 | $form = self::getOptionalAttribute($xml, 'form', FormChoiceValue::class, null); |
||
| 57 | Assert::null($form, SchemaViolationException::class); |
||
| 58 | |||
| 59 | $minCount = self::getOptionalAttribute($xml, 'minCount', MinOccursValue::class, null); |
||
| 60 | Assert::null($minCount, SchemaViolationException::class); |
||
| 61 | |||
| 62 | $maxCount = self::getOptionalAttribute($xml, 'maxCount', MaxOccursValue::class, null); |
||
| 63 | Assert::null($maxCount, SchemaViolationException::class); |
||
| 64 | |||
| 65 | // The annotation |
||
| 66 | $annotation = Annotation::getChildrenOfClass($xml); |
||
| 67 | Assert::maxCount($annotation, 1, TooManyElementsException::class); |
||
| 68 | |||
| 69 | // The local type |
||
| 70 | $localSimpleType = LocalSimpleType::getChildrenOfClass($xml); |
||
| 71 | Assert::maxCount($localSimpleType, 1, TooManyElementsException::class); |
||
| 72 | |||
| 73 | $localComplexType = LocalComplexType::getChildrenOfClass($xml); |
||
| 74 | Assert::maxCount($localComplexType, 1, TooManyElementsException::class); |
||
| 75 | |||
| 76 | $localType = array_merge($localSimpleType, $localComplexType); |
||
| 77 | Assert::maxCount($localType, 1, TooManyElementsException::class); |
||
| 78 | |||
| 79 | // The identity constraint |
||
| 80 | $key = Key::getChildrenOfClass($xml); |
||
| 81 | $keyref = Keyref::getChildrenOfClass($xml); |
||
| 82 | $unique = Unique::getChildrenOfClass($xml); |
||
| 83 | $identityConstraint = array_merge($key, $keyref, $unique); |
||
| 84 | |||
| 85 | return new static( |
||
| 86 | self::getAttribute($xml, 'name', NCNameValue::class), |
||
| 87 | array_pop($localType), |
||
| 88 | $identityConstraint, |
||
| 89 | self::getOptionalAttribute($xml, 'type', QNameValue::class, null), |
||
| 90 | self::getOptionalAttribute($xml, 'substitutionGroup', QNameValue::class, null), |
||
| 91 | self::getOptionalAttribute($xml, 'default', StringValue::class, null), |
||
| 92 | self::getOptionalAttribute($xml, 'fixed', StringValue::class, null), |
||
| 93 | self::getOptionalAttribute($xml, 'nillable', BooleanValue::class, null), |
||
| 94 | self::getOptionalAttribute($xml, 'abstract', BooleanValue::class, null), |
||
| 95 | self::getOptionalAttribute($xml, 'final', DerivationSetValue::class, null), |
||
| 96 | self::getOptionalAttribute($xml, 'block', BlockSetValue::class, null), |
||
| 97 | array_pop($annotation), |
||
| 98 | self::getOptionalAttribute($xml, 'id', IDValue::class, null), |
||
| 99 | self::getAttributesNSFromXML($xml), |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | } |
||
| 103 |