Skip to content

Commit

Permalink
Add IntegerElementTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Nov 29, 2024
1 parent 3ace522 commit aa82ab1
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 0 deletions.
139 changes: 139 additions & 0 deletions src/IntegerElementTrait.php
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;
}
33 changes: 33 additions & 0 deletions tests/Utils/IntegerElement.php
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);
}
}
53 changes: 53 additions & 0 deletions tests/XML/IntegerElementTraitTest.php
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),
);
}
}
1 change: 1 addition & 0 deletions tests/resources/xml/ssp_IntegerElement.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ssp:IntegerElement xmlns:ssp="urn:x-simplesamlphp:namespace">1</ssp:IntegerElement>

0 comments on commit aa82ab1

Please sign in to comment.