Skip to content

Commit b5ce290

Browse files
authored
Use XMLStringElementTrait (#18)
* Bump xml-common * Migrate ds-namespace to XMLStringElementTrait
1 parent c00c988 commit b5ce290

19 files changed

Lines changed: 115 additions & 404 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
"robrichards/xmlseclibs": "^3.1.1",
4242
"simplesamlphp/assert": "~0.2.2",
43-
"simplesamlphp/xml-common": "~0.3.2"
43+
"simplesamlphp/xml-common": "~0.4.8"
4444
},
4545
"require-dev": {
4646
"simplesamlphp/simplesamlphp-test-framework": "^1.0.5"

src/XML/ds/DigestValue.php

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use DOMElement;
88
use SimpleSAML\Assert\Assert;
99
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10+
use SimpleSAML\XML\XMLStringElementTrait;
1011

1112
/**
1213
* Class representing a ds:DigestValue element.
@@ -15,46 +16,20 @@
1516
*/
1617
final class DigestValue extends AbstractDsElement
1718
{
18-
/**
19-
* The digest value.
20-
*
21-
* @var string
22-
*/
23-
protected string $digest;
19+
use XMLStringElementTrait;
2420

2521

2622
/**
27-
* Initialize a DigestValue element.
23+
* Validate the content of the element.
2824
*
29-
* @param string $digest
25+
* @param string $content The value to go in the XML textContent
26+
* @throws \Exception on failure
27+
* @return void
3028
*/
31-
public function __construct(string $digest)
29+
protected function validateContent(string $content): void
3230
{
33-
$this->setDigest($digest);
34-
}
35-
36-
37-
/**
38-
* Collect the value of the digest-property
39-
*
40-
* @return string
41-
*/
42-
public function getDigest(): string
43-
{
44-
return $this->digest;
45-
}
46-
47-
48-
/**
49-
* Set the value of the digest-property
50-
*
51-
* @param string $digest
52-
*/
53-
private function setDigest(string $digest): void
54-
{
55-
Assert::notEmpty($digest, 'DigestValue cannot be empty');
56-
Assert::stringPlausibleBase64($digest, 'ds:DigestValue is not a valid Base64 encoded string');
57-
$this->digest = $digest;
31+
Assert::notEmpty($content, 'DigestValue cannot be empty');
32+
Assert::stringPlausibleBase64($content, 'ds:DigestValue is not a valid Base64 encoded string');
5833
}
5934

6035

@@ -74,19 +49,4 @@ public static function fromXML(DOMElement $xml): object
7449

7550
return new self($xml->textContent);
7651
}
77-
78-
79-
/**
80-
* Convert this DigestValue element to XML.
81-
*
82-
* @param \DOMElement|null $parent The element we should append this DigestValue element to.
83-
* @return \DOMElement
84-
*/
85-
public function toXML(DOMElement $parent = null): DOMElement
86-
{
87-
$e = $this->instantiateParentElement($parent);
88-
$e->textContent = $this->digest;
89-
90-
return $e;
91-
}
9252
}

src/XML/ds/KeyName.php

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use DOMElement;
88
use SimpleSAML\Assert\Assert;
99
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10+
use SimpleSAML\XML\XMLStringElementTrait;
11+
1012

1113
/**
1214
* Class representing a ds:KeyName element.
@@ -15,44 +17,22 @@
1517
*/
1618
final class KeyName extends AbstractDsElement
1719
{
18-
/**
19-
* The key name.
20-
*
21-
* @var string
22-
*/
23-
protected string $name;
24-
25-
26-
/**
27-
* Initialize a KeyName element.
28-
*
29-
* @param string $name
30-
*/
31-
public function __construct(string $name)
32-
{
33-
$this->setName($name);
34-
}
35-
36-
37-
/**
38-
* Collect the value of the name-property
39-
*
40-
* @return string
41-
*/
42-
public function getName(): string
43-
{
44-
return $this->name;
45-
}
20+
use XMLStringElementTrait;
4621

4722

4823
/**
49-
* Set the value of the name-property
24+
* Validate the content of the element.
5025
*
51-
* @param string $name
26+
* @param string $content The value to go in the XML textContent
27+
* @throws \Exception on failure
28+
* @return void
5229
*/
53-
private function setName(string $name): void
30+
protected function validateContent(string $content): void
5431
{
55-
$this->name = $name;
32+
/**
33+
* Perform no validation by default.
34+
* Override this method on the implementing class to perform content validation.
35+
*/
5636
}
5737

5838

@@ -72,19 +52,4 @@ public static function fromXML(DOMElement $xml): object
7252

7353
return new self($xml->textContent);
7454
}
75-
76-
77-
/**
78-
* Convert this KeyName element to XML.
79-
*
80-
* @param \DOMElement|null $parent The element we should append this KeyName element to.
81-
* @return \DOMElement
82-
*/
83-
public function toXML(DOMElement $parent = null): DOMElement
84-
{
85-
$e = $this->instantiateParentElement($parent);
86-
$e->textContent = $this->name;
87-
88-
return $e;
89-
}
9055
}

src/XML/ds/SignatureValue.php

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use DOMElement;
88
use SimpleSAML\Assert\Assert;
99
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10+
use SimpleSAML\XML\XMLStringElementTrait;
1011

1112
/**
1213
* Class representing a ds:SignatureValue element.
@@ -15,41 +16,20 @@
1516
*/
1617
final class SignatureValue extends AbstractDsElement
1718
{
18-
/** @var string */
19-
protected string $value;
19+
use XMLStringElementTrait;
2020

2121

2222
/**
23-
* Initialize a SignatureValue element.
23+
* Validate the content of the element.
2424
*
25-
* @param string $value
25+
* @param string $content The value to go in the XML textContent
26+
* @throws \Exception on failure
27+
* @return void
2628
*/
27-
public function __construct(string $value) {
28-
$this->setValue($value);
29-
}
30-
31-
32-
/**
33-
* Get the signature value.
34-
*
35-
* @return string
36-
*/
37-
public function getValue(): string
29+
protected function validateContent(string $content): void
3830
{
39-
return $this->value;
40-
}
41-
42-
43-
/**
44-
* Set the value.
45-
*
46-
* @param string $value
47-
*/
48-
private function setValue(string $value): void
49-
{
50-
Assert::notEmpty($value, 'SignatureValue cannot be empty');
51-
Assert::stringPlausibleBase64($value, 'SignatureValue is not a valid Base64 encoded string');
52-
$this->value = $value;
31+
Assert::notEmpty($content, 'SignatureValue cannot be empty');
32+
Assert::stringPlausibleBase64($content, 'SignatureValue is not a valid Base64 encoded string');
5333
}
5434

5535

@@ -69,19 +49,4 @@ public static function fromXML(DOMElement $xml): object
6949

7050
return new self($xml->textContent);
7151
}
72-
73-
74-
/**
75-
* Convert this SignatureValue to XML.
76-
*
77-
* @param \DOMElement|null $parent The element we should append this SignatureValue element to.
78-
* @return \DOMElement
79-
*/
80-
public function toXML(DOMElement $parent = null): \DOMElement
81-
{
82-
$e = $this->instantiateParentElement($parent);
83-
$e->textContent = $this->value;
84-
85-
return $e;
86-
}
8752
}

src/XML/ds/X509Certificate.php

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use DOMElement;
88
use SimpleSAML\Assert\Assert;
99
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10+
use SimpleSAML\XML\XMLStringElementTrait;
11+
1012

1113
/**
1214
* Class representing a ds:X509Certificate element.
@@ -15,47 +17,20 @@
1517
*/
1618
final class X509Certificate extends AbstractDsElement
1719
{
18-
/**
19-
* The base64-encoded certificate.
20-
*
21-
* @var string
22-
*/
23-
protected string $certificate;
24-
25-
26-
/**
27-
* Initialize an X509Certificate element.
28-
*
29-
* @param string $certificate
30-
*/
31-
public function __construct(string $certificate)
32-
{
33-
$this->setCertificate($certificate);
34-
}
20+
use XMLStringElementTrait;
3521

3622

3723
/**
38-
* Collect the value of the certificate-property
24+
* Validate the content of the element.
3925
*
40-
* @return string
26+
* @param string $content The value to go in the XML textContent
27+
* @throws \Exception on failure
28+
* @return void
4129
*/
42-
public function getCertificate(): string
30+
protected function validateContent(string $content): void
4331
{
44-
return $this->certificate;
45-
}
46-
47-
48-
/**
49-
* Set the value of the certificate-property
50-
*
51-
* @param string $certificate
52-
*/
53-
private function setCertificate(string $certificate): void
54-
{
55-
Assert::notEmpty($certificate, 'ds:X509Certificate cannot be empty');
56-
Assert::stringPlausibleBase64($certificate, 'ds:X509Certificate is not a valid Base64 encoded string');
57-
58-
$this->certificate = $certificate;
32+
Assert::notEmpty($content, 'ds:X509Certificate cannot be empty');
33+
Assert::stringPlausibleBase64($content, 'ds:X509Certificate is not a valid Base64 encoded string');
5934
}
6035

6136

@@ -75,19 +50,4 @@ public static function fromXML(DOMElement $xml): object
7550

7651
return new self($xml->textContent);
7752
}
78-
79-
80-
/**
81-
* Convert this X509Certificate element to XML.
82-
*
83-
* @param \DOMElement|null $parent The element we should append this X509Certificate element to.
84-
* @return \DOMElement
85-
*/
86-
public function toXML(DOMElement $parent = null): DOMElement
87-
{
88-
$e = $this->instantiateParentElement($parent);
89-
$e->textContent = $this->certificate;
90-
91-
return $e;
92-
}
9353
}

0 commit comments

Comments
 (0)