Skip to content

Commit 40176e2

Browse files
committed
Add IntegerElementTrait
1 parent 3ace522 commit 40176e2

File tree

4 files changed

+227
-0
lines changed

4 files changed

+227
-0
lines changed

src/IntegerElementTrait.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10+
use SimpleSAML\XML\Exception\SchemaViolationException;
11+
12+
use function intval;
13+
use function strval;
14+
15+
/**
16+
* Trait grouping common functionality for simple elements with just some textContent
17+
*
18+
* @package simplesamlphp/xml-common
19+
*/
20+
trait IntegerElementTrait
21+
{
22+
/** @var int */
23+
protected int $content;
24+
25+
26+
/**
27+
* Set the content of the element.
28+
*
29+
* @param int $content The value to go in the XML textContent
30+
*/
31+
protected function setContent(int $content): void
32+
{
33+
$this->validateContent($content);
34+
$this->content = $content;
35+
}
36+
37+
38+
/**
39+
* Get the content of the element.
40+
*
41+
* @return int
42+
*/
43+
public function getContent(): int
44+
{
45+
return $this->sanitizeContent($this->getRawContent());
46+
}
47+
48+
49+
/**
50+
* Get the raw content of the element.
51+
*
52+
* @return int
53+
*/
54+
public function getRawContent(): int
55+
{
56+
return $this->content;
57+
}
58+
59+
60+
/**
61+
* Sanitize the content of the element.
62+
*
63+
* @param int $content The value to go in the XML textContent
64+
* @throws \Exception on failure
65+
* @return int
66+
*/
67+
protected function sanitizeContent(int $content): int
68+
{
69+
/**
70+
* Perform no sanitation by default.
71+
* Override this method on the implementing class to perform content sanitation.
72+
*/
73+
return $content;
74+
}
75+
76+
77+
/**
78+
* Validate the content of the element.
79+
*
80+
* @param int $content The value to go in the XML textContent
81+
* @throws \Exception on failure
82+
* @return void
83+
*/
84+
protected function validateContent(/** @scrutinizer ignore-unused */ int $content): void
85+
{
86+
/**
87+
* Perform no validation by default.
88+
* Override this method on the implementing class to perform content validation.
89+
*/
90+
}
91+
92+
93+
/**
94+
* Convert XML into a class instance
95+
*
96+
* @param \DOMElement $xml The XML element we should load
97+
* @return static
98+
*
99+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
100+
* If the qualified name of the supplied element is wrong
101+
*/
102+
public static function fromXML(DOMElement $xml): static
103+
{
104+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
105+
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
106+
Assert::numeric($xml->textContent, SchemaViolationException::class);
107+
108+
return new static(intval($xml->textContent));
109+
}
110+
111+
112+
/**
113+
* Convert this element to XML.
114+
*
115+
* @param \DOMElement|null $parent The element we should append this element to.
116+
* @return \DOMElement
117+
*/
118+
public function toXML(DOMElement $parent = null): DOMElement
119+
{
120+
$e = $this->instantiateParentElement($parent);
121+
$e->textContent = strval($this->getContent());
122+
123+
return $e;
124+
}
125+
126+
127+
/**
128+
* @return string
129+
*/
130+
abstract public static function getLocalName(): string;
131+
132+
133+
/**
134+
* Create a document structure for this element
135+
*
136+
* @param \DOMElement|null $parent The element we should append to.
137+
* @return \DOMElement
138+
*/
139+
abstract public function instantiateParentElement(DOMElement $parent = null): DOMElement;
140+
}

tests/Utils/IntegerElement.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML;
6+
7+
use SimpleSAML\XML\AbstractElement;
8+
use SimpleSAML\XML\IntegerElementTrait;
9+
10+
/**
11+
* Empty shell class for testing Integer elements.
12+
*
13+
* @package simplesaml/xml-common
14+
*/
15+
final class IntegerElement extends AbstractElement
16+
{
17+
use IntegerElementTrait;
18+
19+
/** @var string */
20+
public const NS = 'urn:x-simplesamlphp:namespace';
21+
22+
/** @var string */
23+
public const NS_PREFIX = 'ssp';
24+
25+
26+
/**
27+
* @param int $content
28+
*/
29+
public function __construct(int $content)
30+
{
31+
$this->setContent($content);
32+
}
33+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\Test\XML\IntegerElement;
10+
use SimpleSAML\XML\AbstractElement;
11+
use SimpleSAML\XML\DOMDocumentFactory;
12+
use SimpleSAML\XML\IntegerElementTrait;
13+
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
14+
15+
use function dirname;
16+
use function strval;
17+
18+
/**
19+
* Class \SimpleSAML\XML\IntegerElementTraitTest
20+
*
21+
* @package simplesamlphp\xml-common
22+
*/
23+
#[CoversClass(SerializableElementTestTrait::class)]
24+
#[CoversClass(IntegerElementTrait::class)]
25+
#[CoversClass(AbstractElement::class)]
26+
final class IntegerElementTraitTest extends TestCase
27+
{
28+
use SerializableElementTestTrait;
29+
30+
31+
/**
32+
*/
33+
public static function setUpBeforeClass(): void
34+
{
35+
self::$testedClass = IntegerElement::class;
36+
37+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
38+
dirname(__FILE__, 2) . '/resources/xml/ssp_IntegerElement.xml',
39+
);
40+
}
41+
42+
/**
43+
*/
44+
public function testMarshalling(): void
45+
{
46+
$integerElement = new IntegerElement(1);
47+
48+
$this->assertEquals(
49+
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
50+
strval($integerElement),
51+
);
52+
}
53+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<ssp:IntegerElement xmlns:ssp="urn:x-simplesamlphp:namespace">1</ssp:IntegerElement>

0 commit comments

Comments
 (0)