|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\XSD\XML\xsd; |
| 6 | + |
| 7 | +use DOMElement; |
| 8 | +use SimpleSAML\Assert\Assert; |
| 9 | +use SimpleSAML\XML\Exception\{ |
| 10 | + InvalidDOMElementException, |
| 11 | + MissingElementException, |
| 12 | + SchemaViolationException, |
| 13 | + TooManyElementsException, |
| 14 | +}; |
| 15 | +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; |
| 16 | +use SimpleSAML\XML\Type\{IDValue, NCNameValue, QNameValue}; |
| 17 | +use SimpleSAML\XSD\Type\{MinOccursValue, MaxOccursValue}; |
| 18 | + |
| 19 | +use function array_merge; |
| 20 | +use function array_pop; |
| 21 | + |
| 22 | +/** |
| 23 | + * Class representing the all-element. |
| 24 | + * |
| 25 | + * @package simplesamlphp/xml-xsd |
| 26 | + */ |
| 27 | +final class All extends AbstractAll implements ParticleInterface, SchemaValidatableElementInterface |
| 28 | +{ |
| 29 | + use SchemaValidatableElementTrait; |
| 30 | + |
| 31 | + /** @var string */ |
| 32 | + public const LOCALNAME = 'all'; |
| 33 | + |
| 34 | + |
| 35 | + /** |
| 36 | + * Create an instance of this object from its XML representation. |
| 37 | + * |
| 38 | + * @param \DOMElement $xml |
| 39 | + * @return static |
| 40 | + * |
| 41 | + * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
| 42 | + * if the qualified name of the supplied element is wrong |
| 43 | + */ |
| 44 | + public static function fromXML(DOMElement $xml): static |
| 45 | + { |
| 46 | + Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
| 47 | + Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
| 48 | + |
| 49 | + // Prohibited attributes |
| 50 | + $name = self::getOptionalAttribute($xml, 'name', NCNameValue::class, null); |
| 51 | + Assert::null($name, SchemaViolationException::class); |
| 52 | + |
| 53 | + $ref = self::getOptionalAttribute($xml, 'ref', QNameValue::class, null); |
| 54 | + Assert::null($ref, SchemaViolationException::class); |
| 55 | + |
| 56 | + $annotation = Annotation::getChildrenOfClass($xml); |
| 57 | + Assert::maxCount($annotation, 1, TooManyElementsException::class); |
| 58 | + |
| 59 | + $all = All::getChildrenOfClass($xml); |
| 60 | + $choice = Choice::getChildrenOfClass($xml); |
| 61 | + $sequence = Sequence::getChildrenOfClass($xml); |
| 62 | + |
| 63 | + $particles = array_merge($all, $choice, $sequence); |
| 64 | + Assert::minCount($particles, 1, MissingElementException::class); |
| 65 | + Assert::maxCount($particles, 1, TooManyElementsException::class); |
| 66 | + |
| 67 | + return new static( |
| 68 | + self::getOptionalAttribute($xml, 'minCount', MinOccursValue::class, null), |
| 69 | + self::getOptionalAttribute($xml, 'maxCount', MaxOccursValue::class, null), |
| 70 | + $particles, |
| 71 | + annotation: array_pop($annotation), |
| 72 | + id: self::getOptionalAttribute($xml, 'id', IDValue::class, null), |
| 73 | + namespacedAttributes: self::getAttributesNSFromXML($xml), |
| 74 | + ); |
| 75 | + } |
| 76 | +} |
0 commit comments