-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\XML; | ||
|
||
use DOMElement; | ||
use SimpleSAML\Assert\Assert; | ||
use SimpleSAML\XML\Exception\InvalidDOMElementException; | ||
|
||
use function intval; | ||
use function strval; | ||
|
||
/** | ||
* Trait grouping common functionality for simple elements with just some textContent | ||
* | ||
* @package simplesamlphp/xml-common | ||
*/ | ||
trait IntegerElementTrait | ||
{ | ||
/** @var int */ | ||
protected int $content; | ||
|
||
|
||
/** | ||
* Set the content of the element. | ||
* | ||
* @param int $content The value to go in the XML textContent | ||
*/ | ||
protected function setContent(int $content): void | ||
{ | ||
$this->validateContent($content); | ||
$this->content = $content; | ||
} | ||
|
||
|
||
/** | ||
* Get the content of the element. | ||
* | ||
* @return int | ||
*/ | ||
public function getContent(): int | ||
{ | ||
return $this->sanitizeContent($this->getRawContent()); | ||
} | ||
|
||
|
||
/** | ||
* Get the raw content of the element. | ||
* | ||
* @return int | ||
*/ | ||
public function getRawContent(): int | ||
{ | ||
return $this->content; | ||
} | ||
|
||
|
||
/** | ||
* Sanitize the content of the element. | ||
* | ||
* @param int $content The value to go in the XML textContent | ||
* @throws \Exception on failure | ||
* @return int | ||
*/ | ||
protected function sanitizeContent(int $content): int | ||
{ | ||
/** | ||
* Perform no sanitation by default. | ||
* Override this method on the implementing class to perform content sanitation. | ||
*/ | ||
return $content; | ||
} | ||
|
||
|
||
/** | ||
* Validate the content of the element. | ||
* | ||
* @param int $content The value to go in the XML textContent | ||
* @throws \Exception on failure | ||
* @return void | ||
*/ | ||
protected function validateContent(/** @scrutinizer ignore-unused */ int $content): void | ||
{ | ||
/** | ||
* Perform no validation by default. | ||
* Override this method on the implementing class to perform content validation. | ||
*/ | ||
} | ||
|
||
|
||
/** | ||
* Convert XML into a class instance | ||
* | ||
* @param \DOMElement $xml The XML element we should load | ||
* @return static | ||
* | ||
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException | ||
* If the qualified name of the supplied element is wrong | ||
*/ | ||
public static function fromXML(DOMElement $xml): static | ||
{ | ||
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); | ||
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); | ||
Assert::numeric($xml->textContent); | ||
|
||
return new static(intval($xml->textContent)); | ||
} | ||
|
||
|
||
/** | ||
* Convert this element to XML. | ||
* | ||
* @param \DOMElement|null $parent The element we should append this element to. | ||
* @return \DOMElement | ||
*/ | ||
public function toXML(DOMElement $parent = null): DOMElement | ||
{ | ||
$e = $this->instantiateParentElement($parent); | ||
$e->textContent = strval($this->getContent()); | ||
|
||
return $e; | ||
} | ||
|
||
|
||
/** | ||
* @return string | ||
*/ | ||
abstract public static function getLocalName(): string; | ||
|
||
|
||
/** | ||
* Create a document structure for this element | ||
* | ||
* @param \DOMElement|null $parent The element we should append to. | ||
* @return \DOMElement | ||
*/ | ||
abstract public function instantiateParentElement(DOMElement $parent = null): DOMElement; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\Test\XML; | ||
|
||
use SimpleSAML\XML\AbstractElement; | ||
use SimpleSAML\XML\IntegerElementTrait; | ||
|
||
/** | ||
* Empty shell class for testing Integer elements. | ||
* | ||
* @package simplesaml/xml-common | ||
*/ | ||
final class IntegerElement extends AbstractElement | ||
{ | ||
use IntegerElementTrait; | ||
|
||
/** @var string */ | ||
public const NS = 'urn:x-simplesamlphp:namespace'; | ||
|
||
/** @var string */ | ||
public const NS_PREFIX = 'ssp'; | ||
|
||
|
||
/** | ||
* @param int $content | ||
*/ | ||
public function __construct(int $content) | ||
{ | ||
$this->setContent($content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\Test\XML; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
use SimpleSAML\Test\XML\IntegerElement; | ||
use SimpleSAML\XML\AbstractElement; | ||
use SimpleSAML\XML\DOMDocumentFactory; | ||
use SimpleSAML\XML\IntegerElementTrait; | ||
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait; | ||
|
||
use function dirname; | ||
use function strval; | ||
|
||
/** | ||
* Class \SimpleSAML\XML\IntegerElementTraitTest | ||
* | ||
* @package simplesamlphp\xml-common | ||
*/ | ||
#[CoversClass(SerializableElementTestTrait::class)] | ||
#[CoversClass(IntegerElementTrait::class)] | ||
#[CoversClass(AbstractElement::class)] | ||
final class IntegerElementTraitTest extends TestCase | ||
{ | ||
use SerializableElementTestTrait; | ||
|
||
|
||
/** | ||
*/ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
self::$testedClass = IntegerElement::class; | ||
|
||
self::$xmlRepresentation = DOMDocumentFactory::fromFile( | ||
dirname(__FILE__, 2) . '/resources/xml/ssp_IntegerElement.xml', | ||
); | ||
} | ||
|
||
/** | ||
*/ | ||
public function testMarshalling(): void | ||
{ | ||
$integerElement = new IntegerElement(1); | ||
|
||
$this->assertEquals( | ||
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), | ||
strval($integerElement), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<ssp:IntegerElement xmlns:ssp="urn:x-simplesamlphp:namespace">1</ssp:IntegerElement> |