Skip to content

Commit 3f4af48

Browse files
committed
Create type-classes for all xsd-types
1 parent bf7c173 commit 3f4af48

File tree

6 files changed

+239
-0
lines changed

6 files changed

+239
-0
lines changed

src/Type/AbstractValueType.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
/**
8+
* Abstract class to be implemented by all types
9+
*
10+
* @package simplesamlphp/xml-common
11+
*/
12+
abstract class AbstractValueType implements ValueTypeInterface
13+
{
14+
/** @var string */
15+
protected string $content;
16+
17+
18+
/**
19+
* Set the content of the type.
20+
*
21+
* @param string $content
22+
*/
23+
protected function __construct(string $content)
24+
{
25+
$this->validateContent($content);
26+
$this->content = $content;
27+
}
28+
29+
30+
/**
31+
* Get the content.
32+
*
33+
* @return string
34+
*/
35+
public function getContent(): string
36+
{
37+
return $this->sanitizeContent($this->getRawContent());
38+
}
39+
40+
41+
/**
42+
* Get the raw content unsanitized content.
43+
*
44+
* @return string
45+
*/
46+
public function getRawContent(): string
47+
{
48+
return $this->content;
49+
}
50+
51+
52+
/**
53+
* Sanitize the content of the element.
54+
*
55+
* @param string $content The value to go in the XML textContent
56+
* @throws \Exception on failure
57+
* @return string
58+
*/
59+
protected function sanitizeContent(string $content): string
60+
{
61+
/**
62+
* Perform no sanitation by default.
63+
* Override this method on the implementing class to perform content sanitation.
64+
*/
65+
return $content;
66+
}
67+
68+
69+
/**
70+
* Validate the content of the element.
71+
*
72+
* @param string $content The value to go in the XML textContent
73+
* @throws \Exception on failure
74+
* @return void
75+
*/
76+
protected function validateContent(/** @scrutinizer ignore-unused */ string $content): void
77+
{
78+
/**
79+
* Perform no validation by default.
80+
* Override this method on the implementing class to perform content validation.
81+
*/
82+
}
83+
84+
85+
/**
86+
* @param string $value
87+
* @return \SimpleSAML\XML\Type\ValueTypeInterface
88+
*/
89+
public static function fromString(string $value): static
90+
{
91+
return new static($value);
92+
}
93+
94+
95+
/**
96+
* Output the value as a string
97+
*
98+
* @return string
99+
*/
100+
public function __toString(): string
101+
{
102+
return $this->getContent();
103+
}
104+
}

src/Type/HexBinaryValue.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
use SimpleSAML\Assert\Assert;
8+
use SimpleSAML\XML\Exception\SchemaViolationException;
9+
10+
/**
11+
* @package simplesaml/xml-common
12+
*/
13+
class HexBinaryValue extends StringValue
14+
{
15+
/**
16+
* Sanitize the content.
17+
*
18+
* Note: There are no processing rules for xs:hexBinary regarding whitespace. General consensus is to strip them
19+
*
20+
* @param string $content The unsanitized content
21+
* @throws \Exception on failure
22+
* @return string
23+
*/
24+
protected function sanitizeContent(string $content): string
25+
{
26+
return str_replace(["\f", "\r", "\n", "\t", "\v", ' '], '', $content);
27+
}
28+
29+
30+
/**
31+
* Validate the content of the element.
32+
*
33+
* @param string $content
34+
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
35+
* @return void
36+
*/
37+
protected function validateContent(string $content): void
38+
{
39+
// Note: content must already be sanitized before validating
40+
Assert::regex(
41+
$this->sanitizeContent($content),
42+
'/([0-9A-F]{2})*/i',
43+
SchemaViolationException::class,
44+
);
45+
}
46+
}

src/Type/IntegerValue.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
use SimpleSAML\Assert\Assert;
8+
use SimpleSAML\XML\Exception\SchemaViolationException;
9+
10+
/**
11+
* @package simplesaml/xml-common
12+
*/
13+
class URIValue extends StringValue
14+
{
15+
/**
16+
* Validate the content of the element.
17+
*
18+
* @param string $content
19+
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
20+
* @return void
21+
*/
22+
protected function validateContent(string $content): void
23+
{
24+
Assert::numeric($content, SchemaViolationException::class);
25+
}
26+
}

src/Type/StringValue.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
/**
8+
* @package simplesaml/xml-common
9+
*/
10+
class StringValue extends AbstractValueType
11+
{
12+
}

src/Type/URIValue.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
use SimpleSAML\Assert\Assert;
8+
use SimpleSAML\XML\Exception\SchemaViolationException;
9+
10+
/**
11+
* @package simplesaml/xml-common
12+
*/
13+
class URIValue extends StringValueType
14+
{
15+
/**
16+
* Validate the content of the element.
17+
*
18+
* @param string $content
19+
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
20+
* @return void
21+
*/
22+
protected function validateContent(string $content): void
23+
{
24+
Assert::validURI($content, SchemaViolationException::class);
25+
}
26+
}

src/Type/ValueTypeInterface.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
/**
8+
* interface class to be implemented by all the classes that represent a value type
9+
*
10+
* @package simplesamlphp/xml-common
11+
*/
12+
interface ValueTypeInterface
13+
{
14+
/**
15+
* @return string
16+
*/
17+
public static function getValue(): string;
18+
19+
20+
/**
21+
* @param string $value
22+
* @return \SimpleSAML\XML\Type\ValueTypeInterface
23+
*/
24+
public static function fromString(string $value): static;
25+
}

0 commit comments

Comments
 (0)