-
Notifications
You must be signed in to change notification settings - Fork 2
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
5 changed files
with
158 additions
and
133 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,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\XML; | ||
|
||
use DOMElement; | ||
use SimpleSAML\XML\Assert\Assert; | ||
use SimpleSAML\XML\Exception\InvalidDOMElementException; | ||
use SimpleSAML\XML\Type\{ValueTypeInterface, StringValue}; | ||
|
||
use function defined; | ||
use function strval; | ||
|
||
/** | ||
* Trait for elements that hold a typed textContent value. | ||
* | ||
* @package simplesaml/xml-common | ||
*/ | ||
trait TypedTextContentTrait | ||
{ | ||
/** | ||
* @param \SimpleSAML\XML\Type\ValueTypeInterface $content | ||
*/ | ||
public function __construct( | ||
protected ValueTypeInterface $content, | ||
) { | ||
} | ||
|
||
|
||
/** | ||
* Create a class from XML | ||
* | ||
* @param \DOMElement $xml | ||
* @return static | ||
*/ | ||
public static function fromXML(DOMElement $xml): static | ||
{ | ||
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); | ||
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); | ||
|
||
$type = self::getTextContentType(); | ||
$text = $type::fromString($xml->textContent); | ||
|
||
return new static($text); | ||
} | ||
|
||
|
||
/** | ||
* Create XML from this class | ||
* | ||
* @param \DOMElement|null $parent | ||
* @return \DOMElement | ||
*/ | ||
public function toXML(?DOMElement $parent = null): DOMElement | ||
{ | ||
$e = $this->instantiateParentElement($parent); | ||
$e->textContent = strval($this->getContent()); | ||
|
||
return $e; | ||
} | ||
|
||
|
||
/** | ||
* Get the type the element's textContent value. | ||
* | ||
* @return class-string | ||
*/ | ||
public static function getTextContentType(): string | ||
{ | ||
if (defined('static::TEXTCONTENT_TYPE')) { | ||
$type = static::TEXTCONTENT_TYPE; | ||
} else { | ||
$type = StringValue::class; | ||
} | ||
|
||
Assert::isAOf($type, ValueTypeInterface::class); | ||
return $type; | ||
} | ||
} |
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
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
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
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,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\Test\XML; | ||
|
||
use DOMDocument; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
use SimpleSAML\XML\DOMDocumentFactory; | ||
use SimpleSAML\XML\Exception\IOException; | ||
use SimpleSAML\XML\Exception\SchemaViolationException; | ||
use SimpleSAML\XML\TypedTextContentTrait; | ||
|
||
/** | ||
* Class \SimpleSAML\XML\TypedTextContentTraitTest | ||
* | ||
* @package simplesamlphp\xml-common | ||
*/ | ||
#[CoversClass(TypedTextContentTrait::class)] | ||
final class TypedTextContentTraitTest extends TestCase | ||
{ | ||
public function testTypedContentPassesForString(): void | ||
{ | ||
$file = 'tests/resources/xml/ssp_StringElement.xml'; | ||
$chunk = DOMDocumentFactory::fromFile($file); | ||
|
||
$stringElt = StringElement::fromXML($chunk->documentElement); | ||
$this->assertInstanceOf(StringElement::class, $stringElt); | ||
} | ||
|
||
|
||
public function testTypedContentPassesForBoolean(): void | ||
{ | ||
$file = 'tests/resources/xml/ssp_BooleanElement.xml'; | ||
$chunk = DOMDocumentFactory::fromFile($file); | ||
|
||
$stringElt = BooleanElement::fromXML($chunk->documentElement); | ||
$this->assertInstanceOf(BooleanElement::class, $stringElt); | ||
} | ||
|
||
|
||
public function testTypedContentFailsForWrongType(): void | ||
{ | ||
$file = 'tests/resources/xml/ssp_BooleanElement.xml'; | ||
$chunk = DOMDocumentFactory::fromFile($file); | ||
$chunk->documentElement->textContent = 'not-a-boolean'; | ||
|
||
$this->expectException(SchemaViolationException::class); | ||
BooleanElement::fromXML($chunk->documentElement); | ||
} | ||
|
||
|
||
public function testTypedContentFailsForWrongClass(): void | ||
{ | ||
$file = 'tests/resources/xml/ssp_BooleanElement.xml'; | ||
$chunk = DOMDocumentFactory::fromFile($file); | ||
$chunk->documentElement->textContent = 'not-a-boolean'; | ||
|
||
$this->expectException(SchemaViolationException::class); | ||
BooleanElement::fromXML($chunk->documentElement); | ||
} | ||
} |