1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace SimpleSAML\XMLSecurity\XML\ec; |
||
6 | |||
7 | use DOMElement; |
||
8 | use SimpleSAML\Assert\Assert; |
||
9 | use SimpleSAML\XML\Constants as C; |
||
10 | use SimpleSAML\XML\Exception\SchemaViolationException; |
||
11 | use SimpleSAML\XML\SchemaValidatableElementInterface; |
||
12 | use SimpleSAML\XML\SchemaValidatableElementTrait; |
||
13 | use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; |
||
14 | |||
15 | use function explode; |
||
16 | use function join; |
||
17 | |||
18 | /** |
||
19 | * Class implementing InclusiveNamespaces |
||
20 | * |
||
21 | * @package simplesamlphp/xml-security |
||
22 | */ |
||
23 | class InclusiveNamespaces extends AbstractEcElement implements SchemaValidatableElementInterface |
||
24 | { |
||
25 | use SchemaValidatableElementTrait; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
26 | |||
27 | /** |
||
28 | * Initialize the InclusiveNamespaces element. |
||
29 | * |
||
30 | * @param string[] $prefixes |
||
31 | */ |
||
32 | final public function __construct( |
||
33 | protected array $prefixes, |
||
34 | ) { |
||
35 | Assert::maxCount($prefixes, C::UNBOUNDED_LIMIT); |
||
36 | Assert::allString( |
||
37 | $prefixes, |
||
38 | 'Can only add string InclusiveNamespaces prefixes.', |
||
39 | InvalidArgumentException::class, |
||
40 | ); |
||
41 | Assert::allRegex($prefixes, '/^[a-z0-9._\\-:]*$/i', SchemaViolationException::class); // xsd:NMTOKEN |
||
42 | } |
||
43 | |||
44 | |||
45 | /** |
||
46 | * Get the prefixes specified by this element. |
||
47 | * |
||
48 | * @return string[] |
||
49 | */ |
||
50 | public function getPrefixes(): array |
||
51 | { |
||
52 | return $this->prefixes; |
||
53 | } |
||
54 | |||
55 | |||
56 | /** |
||
57 | * Convert XML into an InclusiveNamespaces element. |
||
58 | * |
||
59 | * @param \DOMElement $xml The XML element we should load. |
||
60 | * @return static |
||
61 | */ |
||
62 | public static function fromXML(DOMElement $xml): static |
||
63 | { |
||
64 | $prefixes = self::getOptionalAttribute($xml, 'PrefixList', ''); |
||
65 | |||
66 | return new static(array_filter(explode(' ', $prefixes))); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Convert this InclusiveNamespaces to XML. |
||
71 | * |
||
72 | * @param \DOMElement|null $parent The element we should append this InclusiveNamespaces to. |
||
73 | * @return \DOMElement |
||
74 | */ |
||
75 | public function toXML(?DOMElement $parent = null): DOMElement |
||
76 | { |
||
77 | $e = $this->instantiateParentElement($parent); |
||
78 | |||
79 | if (!empty($this->getPrefixes())) { |
||
80 | $e->setAttribute('PrefixList', join(' ', $this->getPrefixes())); |
||
81 | } |
||
82 | |||
83 | return $e; |
||
84 | } |
||
85 | } |
||
86 |