Skip to content

Commit 6f25d90

Browse files
committed
More refactoring
1 parent 64aed61 commit 6f25d90

19 files changed

+286
-140
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
"autoload": {
1919
"psr-4": {
2020
"SimpleSAML\\XMLSchema\\": "src/XMLSchema",
21-
"SimpleSAML\\XML\\": "src/XML"
21+
"SimpleSAML\\XML\\": "src/XML",
22+
"SimpleSAML\\XPath\\": "src/XPath"
2223
}
2324
},
2425
"autoload-dev": {
2526
"psr-4": {
2627
"SimpleSAML\\Test\\Helper\\": ["tests/Helper"],
28+
"SimpleSAML\\Test\\Registry\\": ["tests/Registry"],
2729
"SimpleSAML\\Test\\XML\\": ["tests/XML"],
30+
"SimpleSAML\\Test\\XPath\\": ["tests/XPath"],
2831
"SimpleSAML\\Test\\XMLSchema\\": ["tests/XMLSchema"]
2932
}
3033
},

src/XML/Assert/XPathFilterTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use InvalidArgumentException;
88
use SimpleSAML\XML\Assert\Assert as BaseAssert;
9-
use SimpleSAML\XML\Constants as C;
10-
use SimpleSAML\XML\Exception\RuntimeException;
11-
use SimpleSAML\XML\Utils\XPathFilter;
9+
use SimpleSAML\XPath\Constants as C;
10+
use SimpleSAML\XPath\Exception\RuntimeException;
11+
use SimpleSAML\XPath\XPathFilter;
1212

1313
use function sprintf;
1414

src/XML/Constants.php

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -16,76 +16,9 @@ class Constants
1616
*/
1717
public const NS_XML = 'http://www.w3.org/XML/1998/namespace';
1818

19-
/**
20-
* The namespace for XML schema.
21-
*/
22-
public const NS_XS = 'http://www.w3.org/2001/XMLSchema';
23-
24-
/**
25-
* The namespace for XML schema instance.
26-
*/
27-
public const NS_XSI = 'http://www.w3.org/2001/XMLSchema-instance';
28-
2919
/**
3020
* The maximum amount of child nodes this library is willing to handle.
3121
* By specification, this limit is 150K, but that opens up for denial of service.
3222
*/
3323
public const UNBOUNDED_LIMIT = 10000;
34-
35-
/**
36-
* The namespace for the XML Path Language 1.0
37-
*/
38-
public const XPATH10_URI = 'http://www.w3.org/TR/1999/REC-xpath-19991116';
39-
40-
/** @var array<string> */
41-
public const DEFAULT_ALLOWED_AXES = [
42-
'ancestor',
43-
'ancestor-or-self',
44-
'attribute',
45-
'child',
46-
'descendant',
47-
'descendant-or-self',
48-
'following',
49-
'following-sibling',
50-
// 'namespace', // By default, we do not allow using the namespace axis
51-
'parent',
52-
'preceding',
53-
'preceding-sibling',
54-
'self',
55-
];
56-
57-
/** @var array<string> */
58-
public const DEFAULT_ALLOWED_FUNCTIONS = [
59-
// 'boolean',
60-
// 'ceiling',
61-
// 'concat',
62-
// 'contains',
63-
// 'count',
64-
// 'false',
65-
// 'floor',
66-
// 'id',
67-
// 'lang',
68-
// 'last',
69-
// 'local-name',
70-
// 'name',
71-
// 'namespace-uri',
72-
// 'normalize-space',
73-
'not',
74-
// 'number',
75-
// 'position',
76-
// 'round',
77-
// 'starts-with',
78-
// 'string',
79-
// 'string-length',
80-
// 'substring',
81-
// 'substring-after',
82-
// 'substring-before',
83-
// 'sum',
84-
// 'text',
85-
// 'translate',
86-
// 'true',
87-
];
88-
89-
/** @var int */
90-
public const XPATH_FILTER_MAX_LENGTH = 100;
9124
}

src/XMLSchema/Constants.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSchema;
6+
7+
/**
8+
* Various XML constants.
9+
*
10+
* @package simplesamlphp/xml-common
11+
*/
12+
class Constants
13+
{
14+
/**
15+
* The namespace for XML schema.
16+
*/
17+
public const NS_XS = 'http://www.w3.org/2001/XMLSchema';
18+
19+
/**
20+
* The namespace for XML schema instance.
21+
*/
22+
public const NS_XSI = 'http://www.w3.org/2001/XMLSchema-instance';
23+
}

src/XMLSchema/XML/AbstractXsElement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace SimpleSAML\XMLSchema\XML;
66

77
use SimpleSAML\XML\AbstractElement as BaseElement;
8-
use SimpleSAML\XML\Constants as C;
8+
use SimpleSAML\XMLSchema\Constants as C;
99

1010
/**
1111
* Abstract class to be implemented by all the classes in this namespace

src/XMLSchema/XML/Redefine.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
namespace SimpleSAML\XMLSchema\XML;
66

77
use DOMElement;
8+
use SimpleSAML\XML\Constants as C_XML;
89
use SimpleSAML\XML\Assert\Assert;
9-
use SimpleSAML\XML\Constants as C;
1010
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
11+
use SimpleSAML\XMLSchema\Constants as C_XS;
1112
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, SchemaViolationException};
1213
use SimpleSAML\XMLSchema\Type\{AnyURIValue, IDValue};
1314
use SimpleSAML\XMLSchema\XML\Interface\RedefinableInterface;
@@ -43,7 +44,7 @@ public function __construct(
4344
protected array $redefineElements = [],
4445
array $namespacedAttributes = [],
4546
) {
46-
Assert::maxCount($redefineElements, C::UNBOUNDED_LIMIT);
47+
Assert::maxCount($redefineElements, C_XML::UNBOUNDED_LIMIT);
4748
Assert::allIsInstanceOfAny(
4849
$redefineElements,
4950
[RedefinableInterface::class, Annotation::class],
@@ -124,7 +125,7 @@ public static function fromXML(DOMElement $xml): static
124125
foreach ($xml->childNodes as $node) {
125126
/** @var \DOMElement $node */
126127
if ($node instanceof DOMElement) {
127-
if ($node->namespaceURI === C::NS_XS && array_key_exists($node->localName, $allowed)) {
128+
if ($node->namespaceURI === C_XS::NS_XS && array_key_exists($node->localName, $allowed)) {
128129
$redefineElements[] = $allowed[$node->localName]::fromXML($node);
129130
}
130131
}

src/XMLSchema/XML/Schema.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
namespace SimpleSAML\XMLSchema\XML;
66

77
use DOMElement;
8+
use SimpleSAML\XPath\XPath;
89
use SimpleSAML\XML\Assert\Assert;
9-
use SimpleSAML\XML\Constants as C;
10+
use SimpleSAML\XML\Constants as C_XML;
1011
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
1112
use SimpleSAML\XML\Type\LangValue;
12-
use SimpleSAML\XML\Utils\XPath;
13+
use SimpleSAML\XMLSchema\Constants as C_XS;
1314
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, SchemaViolationException};
1415
use SimpleSAML\XMLSchema\Type\{AnyURIValue, IDValue, StringValue, TokenValue};
1516
use SimpleSAML\XMLSchema\Type\Schema\{BlockSetValue, FormChoiceValue, FullDerivationSetValue};
@@ -32,7 +33,7 @@ final class Schema extends AbstractOpenAttrs implements SchemaValidatableElement
3233

3334
/** The exclusions for the xs:anyAttribute element */
3435
public const XS_ANY_ATTR_EXCLUSIONS = [
35-
[C::NS_XML, 'lang'],
36+
[C_XML::NS_XML, 'lang'],
3637
];
3738

3839

@@ -75,14 +76,14 @@ public function __construct(
7576
protected ?LangValue $lang = null,
7677
array $namespacedAttributes = [],
7778
) {
78-
Assert::maxCount($topLevelElements, C::UNBOUNDED_LIMIT);
79+
Assert::maxCount($topLevelElements, C_XML::UNBOUNDED_LIMIT);
7980
Assert::allIsInstanceOfAny(
8081
$topLevelElements,
8182
[XsInclude::class, Import::class, Redefine::class, Annotation::class],
8283
SchemaViolationException::class,
8384
);
8485

85-
Assert::maxCount($schemaTopElements, C::UNBOUNDED_LIMIT);
86+
Assert::maxCount($schemaTopElements, C_XML::UNBOUNDED_LIMIT);
8687
Assert::allIsInstanceOfAny(
8788
$schemaTopElements,
8889
[
@@ -280,7 +281,7 @@ public static function fromXML(DOMElement $xml): static
280281
foreach ($beforeSchemaTopElements as $node) {
281282
/** @var \DOMElement $node */
282283
if ($node instanceof DOMElement) {
283-
if ($node->namespaceURI === C::NS_XS && array_key_exists($node->localName, $beforeAllowed)) {
284+
if ($node->namespaceURI === C_XS::NS_XS && array_key_exists($node->localName, $beforeAllowed)) {
284285
$topLevelElements[] = $beforeAllowed[$node->localName]::fromXML($node);
285286
}
286287
}
@@ -327,14 +328,14 @@ public static function fromXML(DOMElement $xml): static
327328
foreach ($afterSchemaTopElements as $node) {
328329
/** @var \DOMElement $node */
329330
if ($node instanceof DOMElement) {
330-
if ($node->namespaceURI === C::NS_XS && array_key_exists($node->localName, $afterAllowed)) {
331+
if ($node->namespaceURI === C_XS::NS_XS && array_key_exists($node->localName, $afterAllowed)) {
331332
$schemaTopElements[] = $afterAllowed[$node->localName]::fromXML($node);
332333
}
333334
}
334335
}
335336

336-
$lang = $xml->hasAttributeNS(C::NS_XML, 'lang')
337-
? LangValue::fromString($xml->getAttributeNS(C::NS_XML, 'lang'))
337+
$lang = $xml->hasAttributeNS(C_XML::NS_XML, 'lang')
338+
? LangValue::fromString($xml->getAttributeNS(C_XML::NS_XML, 'lang'))
338339
: null;
339340

340341
return new static(

src/XPath/Constants.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XPath;
6+
7+
/**
8+
* Various XML constants.
9+
*
10+
* @package simplesamlphp/xml-common
11+
*/
12+
class Constants extends \SimpleSAML\XML\Constants
13+
{
14+
/**
15+
* The namespace for the XML Path Language 1.0
16+
*/
17+
public const XPATH10_URI = 'http://www.w3.org/TR/1999/REC-xpath-19991116';
18+
19+
/** @var array<string> */
20+
public const ALL_AXES = [
21+
'ancestor',
22+
'ancestor-or-self',
23+
'attribute',
24+
'child',
25+
'descendant',
26+
'descendant-or-self',
27+
'following',
28+
'following-sibling',
29+
'namespace',
30+
'parent',
31+
'preceding',
32+
'preceding-sibling',
33+
'self',
34+
];
35+
36+
/** @var array<string> */
37+
public const DEFAULT_ALLOWED_AXES = [
38+
'ancestor',
39+
'ancestor-or-self',
40+
'attribute',
41+
'child',
42+
'descendant',
43+
'descendant-or-self',
44+
'following',
45+
'following-sibling',
46+
// 'namespace', // By default, we do not allow using the namespace axis
47+
'parent',
48+
'preceding',
49+
'preceding-sibling',
50+
'self',
51+
];
52+
53+
/** @var array<string> */
54+
public const ALL_FUNCTIONS = [
55+
'boolean',
56+
'ceiling',
57+
'concat',
58+
'contains',
59+
'count',
60+
'false',
61+
'floor',
62+
'id',
63+
'lang',
64+
'last',
65+
'local-name',
66+
'name',
67+
'namespace-uri',
68+
'normalize-space',
69+
'not',
70+
'number',
71+
'position',
72+
'round',
73+
'starts-with',
74+
'string',
75+
'string-length',
76+
'substring',
77+
'substring-after',
78+
'substring-before',
79+
'sum',
80+
'text',
81+
'translate',
82+
'true',
83+
];
84+
85+
86+
/** @var array<string> */
87+
public const DEFAULT_ALLOWED_FUNCTIONS = [
88+
// 'boolean',
89+
// 'ceiling',
90+
// 'concat',
91+
// 'contains',
92+
// 'count',
93+
// 'false',
94+
// 'floor',
95+
// 'id',
96+
// 'lang',
97+
// 'last',
98+
// 'local-name',
99+
// 'name',
100+
// 'namespace-uri',
101+
// 'normalize-space',
102+
'not',
103+
// 'number',
104+
// 'position',
105+
// 'round',
106+
// 'starts-with',
107+
// 'string',
108+
// 'string-length',
109+
// 'substring',
110+
// 'substring-after',
111+
// 'substring-before',
112+
// 'sum',
113+
// 'text',
114+
// 'translate',
115+
// 'true',
116+
];
117+
118+
/** @var int */
119+
public const XPATH_FILTER_MAX_LENGTH = 100;
120+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XPath\Exception;
6+
7+
/**
8+
* Class AxisNotAllowedException
9+
*
10+
* This exception is thrown when a disallowed axis is used in an XPath-expression
11+
*
12+
* @package simplesamlphp/xml-common
13+
*/
14+
class AxisNotAllowedException extends RuntimeException
15+
{
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XPath\Exception;
6+
7+
/**
8+
* Class FunctionNotAllowedException
9+
*
10+
* This exception is thrown when a disallowed function is used in an XPath-expression
11+
*
12+
* @package simplesamlphp/xml-common
13+
*/
14+
class FunctionNotAllowedException extends RuntimeException
15+
{
16+
}

0 commit comments

Comments
 (0)