-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathXMLSchemaElementsTrait.php
More file actions
66 lines (48 loc) · 1.62 KB
/
XMLSchemaElementsTrait.php
File metadata and controls
66 lines (48 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
namespace SimpleSAML\XML\Container;
use Dom;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XMLSchema\Type\AnyURIValue;
use SimpleSAML\XMLSchema\XML\Appinfo;
use function array_key_exists;
use function sprintf;
/**
* One-time instantiation of common elements in XML, for re-use in unit-tests.
*
* @package simplesamlphp\xml-common
* @phpstan-ignore trait.unused
*/
trait XMLSchemaElementsTrait
{
protected const string SOURCE = 'urn:x-simplesamlphp:source';
/** @var array<positive-int, \SimpleSAML\XMLSchema\XML\Appinfo> */
protected array $appinfo = [];
/** @var array<non-empty-string, \Dom\NodeList> */
protected array $domText = [];
/** @param positive-int $x */
public function getAppinfo(int $x = 1): Appinfo
{
if (!array_key_exists($x, $this->appinfo)) {
$domTextNode = $this->getDOMText(sprintf('Application Information (%d)', $x));
$this->appinfo[$x] = new Appinfo(
$domTextNode,
AnyURIValue::fromString(static::SOURCE),
[$this->getXMLAttribute($x)],
);
}
return $this->appinfo[$x];
}
/** @param non-empty-string $text */
public function getDOMText(string $text): Dom\NodeList
{
if (!array_key_exists($text, $this->domText)) {
$doc = DOMDocumentFactory::create();
$elt = $doc->createElement('root');
$domText = $doc->createTextNode($text);
$elt->appendChild($domText);
$this->domText[$text] = $elt->childNodes;
}
return $this->domText[$text];
}
}