|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\XML\Container; |
| 6 | + |
| 7 | +use SimpleSAML\XML\Assert\Assert; |
| 8 | +use SimpleSAML\XML\Attribute as XMLAttribute; |
| 9 | +use SimpleSAML\XML\Chunk; |
| 10 | +use SimpleSAML\XML\DOMDocumentFactory; |
| 11 | +use SimpleSAML\XML\Type\LangValue; |
| 12 | +use SimpleSAML\XMLSchema\Type\LanguageValue; |
| 13 | +use SimpleSAML\XMLSchema\Type\StringValue; |
| 14 | + |
| 15 | +use function array_key_exists; |
| 16 | +use function sprintf; |
| 17 | + |
| 18 | +/** |
| 19 | + * One-time instantiation of common elements in XML, for re-use in unit-tests. |
| 20 | + * |
| 21 | + * @package simplesamlphp\xml-common |
| 22 | + * @phpstan-ignore trait.unused |
| 23 | + */ |
| 24 | +trait XMLElementsTrait |
| 25 | +{ |
| 26 | + protected const string NAMESPACE = 'urn:x-simplesamlphp:namespace'; |
| 27 | + |
| 28 | + |
| 29 | + protected ?Chunk $chunk = null; |
| 30 | + |
| 31 | + /** @var array<string, \SimpleSAML\XML\Type\LangValue> $lang */ |
| 32 | + protected array $lang = []; |
| 33 | + |
| 34 | + /** @var array<string, \SimpleSAML\XMLSchema\Type\LanguageValue> $language */ |
| 35 | + protected array $language = []; |
| 36 | + |
| 37 | + /** @var array<positive-int, \SimpleSAML\XML\Attribute> $attribute */ |
| 38 | + protected array $attribute = []; |
| 39 | + |
| 40 | + |
| 41 | + public function getChunk(): Chunk |
| 42 | + { |
| 43 | + if ($this->chunk === null) { |
| 44 | + $this->chunk = new Chunk(DOMDocumentFactory::fromString( |
| 45 | + '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>', |
| 46 | + )->documentElement); |
| 47 | + } |
| 48 | + |
| 49 | + return $this->chunk; |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + public function getLangValue(string $lang = 'en'): LangValue |
| 54 | + { |
| 55 | + if (array_key_exists($lang, $this->lang)) { |
| 56 | + $this->lang[$lang] = LangValue::fromString($lang); |
| 57 | + } |
| 58 | + |
| 59 | + return $this->lang[$lang]; |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + public function getLanguageValue(string $language = 'en'): LanguageValue |
| 64 | + { |
| 65 | + if (array_key_exists($language, $this->language)) { |
| 66 | + $this->language[$language] = LanguageValue::fromString($language); |
| 67 | + } |
| 68 | + |
| 69 | + return $this->language[$language]; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + /** @param positive-int $x */ |
| 74 | + public function getXMLAttribute(int $x = 1): XMLAttribute |
| 75 | + { |
| 76 | + Assert::positiveInteger($x); |
| 77 | + |
| 78 | + if (!array_key_exists($x, $this->attribute)) { |
| 79 | + $this->attribute[$x] = new XMLAttribute( |
| 80 | + static::NAMESPACE, |
| 81 | + 'ssp', |
| 82 | + sprintf('attr%d', $x), |
| 83 | + StringValue::fromString(sprintf('value%d', $x)), |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + return $this->attribute[$x]; |
| 88 | + } |
| 89 | +} |
0 commit comments