-
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.
Introduce new BooleanElementTrait and URIElementTrait
- Loading branch information
Showing
9 changed files
with
301 additions
and
2 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,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\XML; | ||
|
||
use DOMElement; | ||
use SimpleSAML\Assert\Assert; | ||
use SimpleSAML\XML\Exception\InvalidDOMElementException; | ||
use SimpleSAML\XML\Exception\SchemaViolationException; | ||
|
||
/** | ||
* Trait grouping common functionality for simple boolean elements | ||
* | ||
* @package simplesamlphp/xml-common | ||
*/ | ||
trait BooleanElementTrait | ||
{ | ||
use StringElementTrait; | ||
|
||
/** | ||
* Validate the content of the element. | ||
* | ||
* @param string $content The value to go in the XML textContent | ||
* @throws \Exception on failure | ||
* @return void | ||
*/ | ||
protected function validateContent(string $content): void | ||
{ | ||
Assert::oneOf( | ||
$content, | ||
['0', '1', 'false', 'true'], | ||
sprintf('The value of %s must be a boolean, "%s" given.', $this->getQualifiedName(), $content), | ||
SchemaViolationException::class, | ||
); | ||
} | ||
|
||
|
||
/** | ||
* 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); | ||
|
||
return new static($xml->textContent); | ||
} | ||
|
||
|
||
/** | ||
* @param \DOMElement|null $parent | ||
* @return \DOMElement | ||
*/ | ||
final public function toXML(DOMElement $parent = null): DOMElement | ||
{ | ||
$e = $this->instantiateParentElement($parent); | ||
$e->textContent = $this->getContent(); | ||
|
||
return $e; | ||
} | ||
} |
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\XML; | ||
|
||
use DOMElement; | ||
use SimpleSAML\Assert\Assert; | ||
use SimpleSAML\XML\Exception\InvalidDOMElementException; | ||
use SimpleSAML\XML\Exception\SchemaViolationException; | ||
|
||
/** | ||
* Trait grouping common functionality for simple URI string elements | ||
* | ||
* @package simplesamlphp/xml-common | ||
*/ | ||
trait URIElementTrait | ||
{ | ||
use StringElementTrait; | ||
|
||
/** | ||
* Validate the content of the element. | ||
* | ||
* @param string $content The value to go in the XML textContent | ||
* @throws \Exception on failure | ||
* @return void | ||
*/ | ||
protected function validateContent(string $content): void | ||
{ | ||
Assert::validURI($content, SchemaViolationException::class); | ||
} | ||
|
||
|
||
/** | ||
* 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); | ||
|
||
return new static($xml->textContent); | ||
} | ||
|
||
|
||
/** | ||
* @param \DOMElement|null $parent | ||
* @return \DOMElement | ||
*/ | ||
final public function toXML(DOMElement $parent = null): DOMElement | ||
{ | ||
$e = $this->instantiateParentElement($parent); | ||
$e->textContent = $this->getContent(); | ||
|
||
return $e; | ||
} | ||
} |
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\BooleanElementTrait; | ||
|
||
/** | ||
* Empty shell class for testing BooleanElement. | ||
* | ||
* @package simplesaml/xml-common | ||
*/ | ||
final class BooleanElement extends AbstractElement | ||
{ | ||
use BooleanElementTrait; | ||
|
||
/** @var string */ | ||
public const NS = 'urn:x-simplesamlphp:namespace'; | ||
|
||
/** @var string */ | ||
public const NS_PREFIX = 'ssp'; | ||
|
||
|
||
/** | ||
* @param string $content | ||
*/ | ||
public function __construct(string $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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\Test\XML; | ||
|
||
use SimpleSAML\XML\AbstractElement; | ||
use SimpleSAML\XML\URIElementTrait; | ||
|
||
/** | ||
* Empty shell class for testing URIElement. | ||
* | ||
* @package simplesaml/xml-common | ||
*/ | ||
final class URIElement extends AbstractElement | ||
{ | ||
use URIElementTrait; | ||
|
||
/** @var string */ | ||
public const NS = 'urn:x-simplesamlphp:namespace'; | ||
|
||
/** @var string */ | ||
public const NS_PREFIX = 'ssp'; | ||
|
||
|
||
/** | ||
* @param string $content | ||
*/ | ||
public function __construct(string $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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\Test\XML; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use SimpleSAML\Test\XML\BooleanElement; | ||
use SimpleSAML\XML\DOMDocumentFactory; | ||
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait; | ||
|
||
use function dirname; | ||
use function strval; | ||
|
||
/** | ||
* Class \SimpleSAML\XML\BooleanElementTraitTest | ||
* | ||
* @covers \SimpleSAML\XML\TestUtils\SerializableElementTestTrait | ||
* @covers \SimpleSAML\XML\BooleanElementTrait | ||
* @covers \SimpleSAML\XML\AbstractElement | ||
* | ||
* @package simplesamlphp\xml-common | ||
*/ | ||
final class BooleanElementTraitTest extends TestCase | ||
{ | ||
use SerializableElementTestTrait; | ||
|
||
|
||
/** | ||
*/ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
self::$testedClass = BooleanElement::class; | ||
|
||
self::$xmlRepresentation = DOMDocumentFactory::fromFile( | ||
dirname(__FILE__, 2) . '/resources/xml/ssp_BooleanElement.xml', | ||
); | ||
} | ||
|
||
/** | ||
*/ | ||
public function testMarshalling(): void | ||
{ | ||
$booleanElement = new BooleanElement('true'); | ||
|
||
$this->assertEquals( | ||
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), | ||
strval($booleanElement), | ||
); | ||
} | ||
} |
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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\Test\XML; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use SimpleSAML\Test\XML\URIElement; | ||
use SimpleSAML\XML\DOMDocumentFactory; | ||
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait; | ||
|
||
use function dirname; | ||
use function strval; | ||
|
||
/** | ||
* Class \SimpleSAML\XML\URIElementTraitTest | ||
* | ||
* @covers \SimpleSAML\XML\TestUtils\SerializableElementTestTrait | ||
* @covers \SimpleSAML\XML\URIElementTrait | ||
* @covers \SimpleSAML\XML\AbstractElement | ||
* | ||
* @package simplesamlphp\xml-common | ||
*/ | ||
final class URIElementTraitTest extends TestCase | ||
{ | ||
use SerializableElementTestTrait; | ||
|
||
|
||
/** | ||
*/ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
self::$testedClass = URIElement::class; | ||
|
||
self::$xmlRepresentation = DOMDocumentFactory::fromFile( | ||
dirname(__FILE__, 2) . '/resources/xml/ssp_URIElement.xml', | ||
); | ||
} | ||
|
||
/** | ||
*/ | ||
public function testMarshalling(): void | ||
{ | ||
$URIElement = new URIElement('https://example.org'); | ||
|
||
$this->assertEquals( | ||
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), | ||
strval($URIElement), | ||
); | ||
} | ||
} |
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:BooleanElement xmlns:ssp="urn:x-simplesamlphp:namespace">true</ssp:BooleanElement> |
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:URIElement xmlns:ssp="urn:x-simplesamlphp:namespace">https://example.org</ssp:URIElement> |