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\Constants as C; |
||
| 10 | use SimpleSAML\XML\SchemaValidatableElementInterface; |
||
| 11 | use SimpleSAML\XML\SchemaValidatableElementTrait; |
||
| 12 | use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException; |
||
| 13 | use SimpleSAML\XMLSchema\Exception\MissingElementException; |
||
| 14 | use SimpleSAML\XMLSchema\Exception\SchemaViolationException; |
||
| 15 | use SimpleSAML\XMLSchema\Exception\TooManyElementsException; |
||
| 16 | use SimpleSAML\XMLSchema\Type\IDValue; |
||
| 17 | use SimpleSAML\XMLSchema\Type\QNameValue; |
||
| 18 | use SimpleSAML\XMLSchema\Type\StringValue; |
||
| 19 | use SimpleSAML\XMLSchema\XML\Interface\SimpleDerivationInterface; |
||
| 20 | |||
| 21 | use function array_fill; |
||
| 22 | use function array_map; |
||
| 23 | use function array_pop; |
||
| 24 | use function implode; |
||
| 25 | use function strval; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Class representing the union-element. |
||
| 29 | * |
||
| 30 | * @package simplesamlphp/xml-common |
||
| 31 | */ |
||
| 32 | final class Union extends AbstractAnnotated implements |
||
| 33 | SchemaValidatableElementInterface, |
||
| 34 | SimpleDerivationInterface |
||
| 35 | { |
||
| 36 | use SchemaValidatableElementTrait; |
||
| 37 | |||
| 38 | |||
| 39 | public const string LOCALNAME = 'union'; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 40 | |||
| 41 | |||
| 42 | /** |
||
| 43 | * Notation constructor |
||
| 44 | * |
||
| 45 | * @param \SimpleSAML\XMLSchema\XML\LocalSimpleType[] $simpleType |
||
| 46 | * @param array<\SimpleSAML\XMLSchema\Type\QNameValue> $memberTypes |
||
| 47 | * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation |
||
| 48 | * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id |
||
| 49 | * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes |
||
| 50 | */ |
||
| 51 | public function __construct( |
||
| 52 | protected array $simpleType, |
||
| 53 | protected array $memberTypes = [], |
||
| 54 | ?Annotation $annotation = null, |
||
| 55 | ?IDValue $id = null, |
||
| 56 | array $namespacedAttributes = [], |
||
| 57 | ) { |
||
| 58 | Assert::allIsInstanceOf($simpleType, LocalSimpleType::class, SchemaViolationException::class); |
||
| 59 | Assert::allIsInstanceOf($memberTypes, QNameValue::class, SchemaViolationException::class); |
||
| 60 | |||
| 61 | Assert::maxCount($memberTypes, C::UNBOUNDED_LIMIT); |
||
| 62 | if (empty($memberTypes)) { |
||
| 63 | Assert::minCount($simpleType, 1, MissingElementException::class); |
||
| 64 | } |
||
| 65 | |||
| 66 | parent::__construct($annotation, $id, $namespacedAttributes); |
||
| 67 | } |
||
| 68 | |||
| 69 | |||
| 70 | /** |
||
| 71 | * Collect the value of the simpleType-property |
||
| 72 | * |
||
| 73 | * @return \SimpleSAML\XMLSchema\XML\LocalSimpleType[] |
||
| 74 | */ |
||
| 75 | public function getSimpleType(): array |
||
| 76 | { |
||
| 77 | return $this->simpleType; |
||
| 78 | } |
||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * Collect the value of the memberTypes-property |
||
| 83 | * |
||
| 84 | * @return array<\SimpleSAML\XMLSchema\Type\QNameValue> |
||
| 85 | */ |
||
| 86 | public function getMemberTypes(): array |
||
| 87 | { |
||
| 88 | return $this->memberTypes; |
||
| 89 | } |
||
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * Add this Union to an XML element. |
||
| 94 | * |
||
| 95 | * @param \DOMElement|null $parent The element we should append this union to. |
||
| 96 | * @return \DOMElement |
||
| 97 | */ |
||
| 98 | public function toXML(?DOMElement $parent = null): DOMElement |
||
| 99 | { |
||
| 100 | $e = parent::toXML($parent); |
||
| 101 | |||
| 102 | $memberTypes = implode(' ', array_map('strval', $this->getMemberTypes())); |
||
| 103 | if ($memberTypes !== '') { |
||
| 104 | $e->setAttribute('memberTypes', $memberTypes); |
||
| 105 | } |
||
| 106 | |||
| 107 | foreach ($this->getSimpleType() as $simpleType) { |
||
| 108 | $simpleType->toXML($e); |
||
| 109 | } |
||
| 110 | |||
| 111 | return $e; |
||
| 112 | } |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * Create an instance of this object from its XML representation. |
||
| 117 | * |
||
| 118 | * @param \DOMElement $xml |
||
| 119 | * @return static |
||
| 120 | * |
||
| 121 | * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException |
||
| 122 | * if the qualified name of the supplied element is wrong |
||
| 123 | */ |
||
| 124 | public static function fromXML(DOMElement $xml): static |
||
| 125 | { |
||
| 126 | Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
||
| 127 | Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
||
| 128 | |||
| 129 | $annotation = Annotation::getChildrenOfClass($xml); |
||
| 130 | Assert::maxCount($annotation, 1, TooManyElementsException::class); |
||
| 131 | |||
| 132 | $memberTypes = []; |
||
| 133 | $memberTypesValue = self::getOptionalAttribute($xml, 'memberTypes', StringValue::class, null); |
||
| 134 | if ($memberTypesValue !== null) { |
||
| 135 | $exploded = explode(' ', strval($memberTypesValue)); |
||
| 136 | /** @var \SimpleSAML\XMLSchema\Type\QNameValue[] $memberTypes */ |
||
| 137 | $memberTypes = array_map( |
||
| 138 | [QNameValue::class, 'fromDocument'], |
||
| 139 | $exploded, |
||
| 140 | array_fill(0, count($exploded), $xml), |
||
| 141 | ); |
||
| 142 | } |
||
| 143 | |||
| 144 | return new static( |
||
| 145 | LocalSimpleType::getChildrenOfClass($xml), |
||
| 146 | $memberTypes, |
||
| 147 | array_pop($annotation), |
||
| 148 | self::getOptionalAttribute($xml, 'id', IDValue::class, null), |
||
| 149 | self::getAttributesNSFromXML($xml), |
||
| 150 | ); |
||
| 151 | } |
||
| 152 | } |
||
| 153 |