Skip to content

Commit 97a9420

Browse files
authored
Merge pull request #62 from simplesamlphp/feature/xml-types
Feature/xml types
2 parents 4ef12f0 + 6bb1a12 commit 97a9420

21 files changed

+3450
-1
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
/tools/ export-ignore
44
/tests/bootstrap.php export-ignore
55
/tests/resources/xml export-ignore
6+
/tests/InterOperability/ export-ignore
67
/tests/Utils/ export-ignore
78
/tests/XML/ export-ignore
9+
/tests/XMLSchema/ export-ignore
810
codecov.yml export-ignore
911
.editorconfig export-ignore
1012
.gitattributes export-ignore

src/XML/Type/BaseValue.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
use SimpleSAML\XML\Attribute;
8+
use SimpleSAML\XML\Constants as C;
9+
use SimpleSAML\XMLSchema\Type\Builtin\AnyURIValue;
10+
use SimpleSAML\XMLSchema\Type\Helper\AttributeTypeInterface;
11+
12+
/**
13+
* @package simplesaml/xml-common
14+
*/
15+
class BaseValue extends AnyURIValue implements AttributeTypeInterface
16+
{
17+
/** @var string */
18+
public const SCHEMA_TYPE = 'xs:anyURI';
19+
20+
21+
/**
22+
* Convert this value to an attribute
23+
*
24+
* @return \SimpleSAML\XML\Attribute
25+
*/
26+
public function toAttribute(): Attribute
27+
{
28+
return new Attribute(C::NS_XML, 'xml', 'base', $this);
29+
}
30+
}

src/XML/Type/IDValue.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
use SimpleSAML\XML\Attribute;
8+
use SimpleSAML\XML\Constants as C;
9+
use SimpleSAML\XMLSchema\Type\Builtin\IDValue as BaseIDValue;
10+
use SimpleSAML\XMLSchema\Type\Helper\AttributeTypeInterface;
11+
12+
/**
13+
* @package simplesaml/xml-common
14+
*/
15+
class IDValue extends BaseIDValue implements AttributeTypeInterface
16+
{
17+
/** @var string */
18+
public const SCHEMA_TYPE = 'xs:ID';
19+
20+
21+
/**
22+
* Convert this value to an attribute
23+
*
24+
* @return \SimpleSAML\XML\Attribute
25+
*/
26+
public function toAttribute(): Attribute
27+
{
28+
return new Attribute(C::NS_XML, 'xml', 'id', $this);
29+
}
30+
}

src/XML/Type/LangValue.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
use SimpleSAML\XML\Attribute;
8+
use SimpleSAML\XML\Constants as C;
9+
use SimpleSAML\XMLSchema\Type\Builtin\LanguageValue;
10+
use SimpleSAML\XMLSchema\Type\Helper\AttributeTypeInterface;
11+
12+
/**
13+
* @package simplesaml/xml-common
14+
*/
15+
class LangValue extends LanguageValue implements AttributeTypeInterface
16+
{
17+
/** @var string */
18+
public const SCHEMA_TYPE = 'xs:language';
19+
20+
21+
/**
22+
* Validate the value.
23+
*
24+
* @param string $value
25+
* @throws \SimpleSAML\XMLSchema\Exception\SchemaViolationException on failure
26+
* @return void
27+
*/
28+
protected function validateValue(string $value): void
29+
{
30+
$sanitized = $this->sanitizeValue($value);
31+
32+
if ($sanitized !== '') {
33+
parent::validateValue($sanitized);
34+
}
35+
}
36+
37+
38+
/**
39+
* Convert this value to an attribute
40+
*
41+
* @return \SimpleSAML\XML\Attribute
42+
*/
43+
public function toAttribute(): Attribute
44+
{
45+
return new Attribute(C::NS_XML, 'xml', 'lang', $this);
46+
}
47+
}

src/XML/Type/SpaceValue.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
use SimpleSAML\XML\Assert\Assert;
8+
use SimpleSAML\XML\Attribute;
9+
use SimpleSAML\XML\Constants as C;
10+
use SimpleSAML\XML\XML\xml\SpaceEnum;
11+
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
12+
use SimpleSAML\XMLSchema\Type\Builtin\NCNameValue;
13+
use SimpleSAML\XMLSchema\Type\Helper\AttributeTypeInterface;
14+
15+
/**
16+
* @package simplesaml/xml-common
17+
*/
18+
class SpaceValue extends NCNameValue implements AttributeTypeInterface
19+
{
20+
/**
21+
* Validate the value.
22+
*
23+
* @param string $value
24+
* @throws \SimpleSAML\XMLSchema\Exception\SchemaViolationException on failure
25+
* @return void
26+
*/
27+
protected function validateValue(string $value): void
28+
{
29+
$sanitized = $this->sanitizeValue($value);
30+
parent::validateValue($sanitized);
31+
32+
Assert::oneOf(
33+
$sanitized,
34+
[
35+
SpaceEnum::Default->value,
36+
SpaceEnum::Preserve->value,
37+
],
38+
SchemaViolationException::class,
39+
);
40+
}
41+
42+
43+
/**
44+
* @param \SimpleSAML\XML\XML\xml\SpaceEnum $value
45+
* @return static
46+
*/
47+
public static function fromEnum(SpaceEnum $value): static
48+
{
49+
return new static($value->value);
50+
}
51+
52+
53+
/**
54+
* @return \SimpleSAML\XML\XML\xml\SpaceEnum $value
55+
*/
56+
public function toEnum(): SpaceEnum
57+
{
58+
return SpaceEnum::from($this->getValue());
59+
}
60+
61+
62+
/**
63+
* Convert this value to an attribute
64+
*
65+
* @return \SimpleSAML\XML\Attribute
66+
*/
67+
public function toAttribute(): Attribute
68+
{
69+
return new Attribute(C::NS_XML, 'xml', 'space', $this);
70+
}
71+
}

src/XML/XML/xml/SpaceEnum.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\XML\xml;
6+
7+
enum SpaceEnum: string
8+
{
9+
case Default = 'default';
10+
case Preserve = 'preserve';
11+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\XML\xml;
6+
7+
use SimpleSAML\XML\Type\{BaseValue, IDValue, LangValue, SpaceValue};
8+
9+
/**
10+
* Trait grouping common functionality for elements that use the specialAttrs-attributeGroup.
11+
*
12+
* @package simplesamlphp/xml-common
13+
*/
14+
trait SpecialAttrsTrait
15+
{
16+
/**
17+
* The base.
18+
*
19+
* @var \SimpleSAML\XML\Type\BaseValue|null
20+
*/
21+
protected ?BaseValue $base = null;
22+
23+
24+
/**
25+
* The id.
26+
*
27+
* @var \SimpleSAML\XML\Type\IDValue|null
28+
*/
29+
protected ?IDValue $id = null;
30+
31+
32+
/**
33+
* The lang.
34+
*
35+
* @var \SimpleSAML\XML\Type\LangValue|null
36+
*/
37+
protected ?LangValue $lang = null;
38+
39+
40+
/**
41+
* The space.
42+
*
43+
* @var \SimpleSAML\XML\Type\SpaceValue|null
44+
*/
45+
protected ?SpaceValue $space = null;
46+
47+
48+
/**
49+
* Collect the value of the base-property
50+
*
51+
* @return \SimpleSAML\XML\Type\BaseValue|null
52+
*/
53+
public function getBase(): ?BaseValue
54+
{
55+
return $this->base;
56+
}
57+
58+
59+
/**
60+
* Set the value of the base-property
61+
*
62+
* @param \SimpleSAML\XML\Type\BaseValue|null $base
63+
*/
64+
protected function setBase(?BaseValue $base): void
65+
{
66+
$this->base = $base;
67+
}
68+
69+
70+
/**
71+
* Collect the value of the id-property
72+
*
73+
* @return \SimpleSAML\XML\Type\IDValue|null
74+
*/
75+
public function getId(): ?IDValue
76+
{
77+
return $this->id;
78+
}
79+
80+
81+
/**
82+
* Set the value of the id-property
83+
*
84+
* @param \SimpleSAML\XML\Type\IDValue|null $id
85+
*/
86+
protected function setID(?IDValue $id): void
87+
{
88+
$this->id = $id;
89+
}
90+
91+
92+
/**
93+
* Collect the value of the lang-property
94+
*
95+
* @return \SimpleSAML\XML\Type\LangValue|null
96+
*/
97+
public function getLang(): ?LangValue
98+
{
99+
return $this->lang;
100+
}
101+
102+
103+
/**
104+
* Set the value of the lang-property
105+
*
106+
* @param \SimpleSAML\XML\Type\LangValue|null $id
107+
*/
108+
protected function setLang(?LangValue $lang): void
109+
{
110+
$this->lang = $lang;
111+
}
112+
113+
114+
/**
115+
* Collect the value of the space-property
116+
*
117+
* @return \SimpleSAML\XML\Type\SpaceValue|null
118+
*/
119+
public function getSpace(): ?SpaceValue
120+
{
121+
return $this->space;
122+
}
123+
124+
125+
/**
126+
* Set the value of the space-property
127+
*
128+
* @param \SimpleSAML\XML\Type\SpaceValue|null $id
129+
*/
130+
protected function setSpace(?SpaceValue $space): void
131+
{
132+
$this->space = $space;
133+
}
134+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSchema\Type\Helper;
6+
7+
use SimpleSAML\XML\Attribute;
8+
9+
/**
10+
* interface class to be implemented by all the classes that represent a DOM Attribute
11+
*
12+
* @package simplesamlphp/xml-common
13+
*/
14+
interface AttributeTypeInterface extends ValueTypeInterface
15+
{
16+
/**
17+
* Convert this value to an attribute
18+
*
19+
* @return \SimpleSAML\XML\Attribute
20+
*/
21+
public function toAttribute(): Attribute;
22+
}

tests/InterOperability/SchemaTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testUnmarshalling(DOMElement $schema): void
4747
*/
4848
public static function provideSchema(): array
4949
{
50-
$dir = dirname(__FILE__, 3);
50+
$dir = dirname(__FILE__, 2);
5151

5252
/** @var string $xml */
5353
$xml = file_get_contents($dir . '/resources/schemas/xml.xsd');

0 commit comments

Comments
 (0)