Skip to content

Commit 4f1257d

Browse files
committed
Introduce new BooleanElementTrait and URIElementTrait
1 parent b764f1e commit 4f1257d

9 files changed

+301
-2
lines changed

src/BooleanElementTrait.php

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
/**
13+
* Trait grouping common functionality for simple boolean elements
14+
*
15+
* @package simplesamlphp/xml-common
16+
*/
17+
trait BooleanElementTrait
18+
{
19+
use StringElementTrait;
20+
21+
/**
22+
* Validate the content of the element.
23+
*
24+
* @param string $content The value to go in the XML textContent
25+
* @throws \Exception on failure
26+
* @return void
27+
*/
28+
protected function validateContent(string $content): void
29+
{
30+
Assert::oneOf(
31+
$content,
32+
['0', '1', 'false', 'true'],
33+
sprintf('The value of %s must be a boolean, "%s" given.', $this->getQualifiedName(), $content),
34+
SchemaViolationException::class,
35+
);
36+
}
37+
38+
39+
/**
40+
* Convert XML into a class instance
41+
*
42+
* @param \DOMElement $xml The XML element we should load
43+
* @return static
44+
*
45+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
46+
* If the qualified name of the supplied element is wrong
47+
*/
48+
public static function fromXML(DOMElement $xml): static
49+
{
50+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
51+
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
52+
53+
return new static($xml->textContent);
54+
}
55+
56+
57+
/**
58+
* @param \DOMElement|null $parent
59+
* @return \DOMElement
60+
*/
61+
final public function toXML(DOMElement $parent = null): DOMElement
62+
{
63+
$e = $this->instantiateParentElement($parent);
64+
$e->textContent = $this->getContent();
65+
66+
return $e;
67+
}
68+
}

src/LocalizedStringElementTrait.php

-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ public static function fromXML(DOMElement $xml): static
8181
'Missing xml:lang from ' . static::getLocalName(),
8282
MissingAttributeException::class,
8383
);
84-
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
85-
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
8684

8785
return new static($xml->getAttributeNS(C::NS_XML, 'lang'), $xml->textContent);
8886
}

src/URIElementTrait.php

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
/**
13+
* Trait grouping common functionality for simple URI string elements
14+
*
15+
* @package simplesamlphp/xml-common
16+
*/
17+
trait URIElementTrait
18+
{
19+
use StringElementTrait;
20+
21+
/**
22+
* Validate the content of the element.
23+
*
24+
* @param string $content The value to go in the XML textContent
25+
* @throws \Exception on failure
26+
* @return void
27+
*/
28+
protected function validateContent(string $content): void
29+
{
30+
Assert::validURI($content, SchemaViolationException::class);
31+
}
32+
33+
34+
/**
35+
* Convert XML into a class instance
36+
*
37+
* @param \DOMElement $xml The XML element we should load
38+
* @return static
39+
*
40+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
41+
* If the qualified name of the supplied element is wrong
42+
*/
43+
public static function fromXML(DOMElement $xml): static
44+
{
45+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
46+
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
47+
48+
return new static($xml->textContent);
49+
}
50+
51+
52+
/**
53+
* @param \DOMElement|null $parent
54+
* @return \DOMElement
55+
*/
56+
final public function toXML(DOMElement $parent = null): DOMElement
57+
{
58+
$e = $this->instantiateParentElement($parent);
59+
$e->textContent = $this->getContent();
60+
61+
return $e;
62+
}
63+
}

tests/Utils/BooleanElement.php

+33
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\BooleanElementTrait;
9+
10+
/**
11+
* Empty shell class for testing BooleanElement.
12+
*
13+
* @package simplesaml/xml-common
14+
*/
15+
final class BooleanElement extends AbstractElement
16+
{
17+
use BooleanElementTrait;
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 string $content
28+
*/
29+
public function __construct(string $content)
30+
{
31+
$this->setContent($content);
32+
}
33+
}

tests/Utils/URIElement.php

+33
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\URIElementTrait;
9+
10+
/**
11+
* Empty shell class for testing URIElement.
12+
*
13+
* @package simplesaml/xml-common
14+
*/
15+
final class URIElement extends AbstractElement
16+
{
17+
use URIElementTrait;
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 string $content
28+
*/
29+
public function __construct(string $content)
30+
{
31+
$this->setContent($content);
32+
}
33+
}

tests/XML/BooleanElementTraitTest.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use SimpleSAML\Test\XML\BooleanElement;
9+
use SimpleSAML\XML\DOMDocumentFactory;
10+
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
11+
12+
use function dirname;
13+
use function strval;
14+
15+
/**
16+
* Class \SimpleSAML\XML\BooleanElementTraitTest
17+
*
18+
* @covers \SimpleSAML\XML\TestUtils\SerializableElementTestTrait
19+
* @covers \SimpleSAML\XML\BooleanElementTrait
20+
* @covers \SimpleSAML\XML\AbstractElement
21+
*
22+
* @package simplesamlphp\xml-common
23+
*/
24+
final class BooleanElementTraitTest extends TestCase
25+
{
26+
use SerializableElementTestTrait;
27+
28+
29+
/**
30+
*/
31+
public static function setUpBeforeClass(): void
32+
{
33+
self::$testedClass = BooleanElement::class;
34+
35+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
36+
dirname(__FILE__, 2) . '/resources/xml/ssp_BooleanElement.xml',
37+
);
38+
}
39+
40+
/**
41+
*/
42+
public function testMarshalling(): void
43+
{
44+
$booleanElement = new BooleanElement('true');
45+
46+
$this->assertEquals(
47+
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
48+
strval($booleanElement),
49+
);
50+
}
51+
}

tests/XML/URIElementTraitTest.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use SimpleSAML\Test\XML\URIElement;
9+
use SimpleSAML\XML\DOMDocumentFactory;
10+
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
11+
12+
use function dirname;
13+
use function strval;
14+
15+
/**
16+
* Class \SimpleSAML\XML\URIElementTraitTest
17+
*
18+
* @covers \SimpleSAML\XML\TestUtils\SerializableElementTestTrait
19+
* @covers \SimpleSAML\XML\URIElementTrait
20+
* @covers \SimpleSAML\XML\AbstractElement
21+
*
22+
* @package simplesamlphp\xml-common
23+
*/
24+
final class URIElementTraitTest extends TestCase
25+
{
26+
use SerializableElementTestTrait;
27+
28+
29+
/**
30+
*/
31+
public static function setUpBeforeClass(): void
32+
{
33+
self::$testedClass = URIElement::class;
34+
35+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
36+
dirname(__FILE__, 2) . '/resources/xml/ssp_URIElement.xml',
37+
);
38+
}
39+
40+
/**
41+
*/
42+
public function testMarshalling(): void
43+
{
44+
$URIElement = new URIElement('https://example.org');
45+
46+
$this->assertEquals(
47+
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
48+
strval($URIElement),
49+
);
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<ssp:BooleanElement xmlns:ssp="urn:x-simplesamlphp:namespace">true</ssp:BooleanElement>
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<ssp:URIElement xmlns:ssp="urn:x-simplesamlphp:namespace">https://example.org</ssp:URIElement>

0 commit comments

Comments
 (0)