Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed default namespace not working correctly #87

Merged
merged 4 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Schema/CustomAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CustomAttribute
public function __construct(
string $namespaceURI,
string $name,
string $value
string $value,
) {
$this->namespaceURI = $namespaceURI;
$this->name = $name;
Expand Down
4 changes: 2 additions & 2 deletions src/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function findSomethingNoThrow(
string $getter,
string $name,
?string $namespace = null,
array &$calling = []
array &$calling = [],
): ?SchemaItem {
$calling[spl_object_hash($this)] = true;
$cid = "$getter, $name, $namespace";
Expand Down Expand Up @@ -62,7 +62,7 @@ protected function findSomethingNoThrowSchemas(
string $getter,
string $name,
?string $namespace = null,
array &$calling = []
array &$calling = [],
): ?SchemaItem {
foreach ($schemas as $childSchema) {
if (!isset($calling[spl_object_hash($childSchema)])) {
Expand Down
85 changes: 35 additions & 50 deletions src/SchemaReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function addKnownNamespaceSchemaLocation(string $namespace, string $locat

private function loadAttributeGroup(
Schema $schema,
\DOMElement $node
\DOMElement $node,
): \Closure {
$attGroup = new AttributeGroup($schema, $node->getAttribute('name'));
$attGroup->setDoc($this->getDocumentation($node));
Expand Down Expand Up @@ -187,7 +187,7 @@ function (\DOMElement $node, \DOMElement $childNode) use ($schema, $attGroup): v
private function getAttributeFromAttributeOrRef(
\DOMElement $childNode,
Schema $schema,
\DOMElement $node
\DOMElement $node,
): AttributeItem {
if ($childNode->hasAttribute('ref')) {
$attributeDef = $this->findAttributeItem($schema, $node, $childNode->getAttribute('ref'));
Expand Down Expand Up @@ -270,7 +270,7 @@ private function loadAttributeOrElementDef(
Schema $schema,
\DOMElement $node,
\DOMElement $childNode,
bool $isAttribute
bool $isAttribute,
): \Closure {
$name = $childNode->getAttribute('name');
if ($isAttribute) {
Expand Down Expand Up @@ -463,7 +463,7 @@ private function loadSequenceChildNode(
\DOMElement $node,
\DOMElement $childNode,
?int $max,
?int $min = null
?int $min = null,
): void {
switch ($childNode->localName) {
case 'sequence':
Expand Down Expand Up @@ -518,7 +518,7 @@ private function loadSequenceChildNodeLoadElement(
\DOMElement $node,
\DOMElement $childNode,
?int $max,
?int $min
?int $min,
): void {
$schema = $elementContainer->getSchema();
if ($childNode->hasAttribute('ref')) {
Expand Down Expand Up @@ -551,7 +551,7 @@ private function loadSequenceChildNodeLoadAny(
\DOMElement $node,
\DOMElement $childNode,
?int $max,
?int $min
?int $min,
): void {
$schema = $elementContainer->getSchema();
$element = $this->createAnyElement($schema, $childNode);
Expand All @@ -570,7 +570,7 @@ private function resolveSubstitutionGroup(
Schema $schema,
\DOMElement $node,
\DOMElement $childNode,
AbstractElementSingle $element
AbstractElementSingle $element,
): void {
if ($childNode->hasAttribute('substitutionGroup')) {
$substitutionGroup = $childNode->getAttribute('substitutionGroup');
Expand All @@ -583,7 +583,7 @@ private function addGroupAsElement(
Schema $schema,
\DOMElement $node,
\DOMElement $childNode,
ElementContainer $elementContainer
ElementContainer $elementContainer,
): void {
$referencedGroup = $this->findGroup(
$schema,
Expand All @@ -598,7 +598,7 @@ private function addGroupAsElement(
private function loadChoiceWithChildren(
Schema $schema,
\DOMElement $node,
ElementContainer $elementContainer
ElementContainer $elementContainer,
): void {
$choice = $this->createChoice($schema, $node);
$elementContainer->addElement($choice);
Expand Down Expand Up @@ -732,7 +732,7 @@ private function loadComplexTypeFromChildNode(
BaseComplexType $type,
\DOMElement $node,
\DOMElement $childNode,
Schema $schema
Schema $schema,
): void {
switch ($childNode->localName) {
case 'sequence':
Expand Down Expand Up @@ -818,7 +818,7 @@ function (SimpleType $list) use ($type): void {
private function findSomeType(
SchemaItem $fromThis,
\DOMElement $node,
string $attributeName
string $attributeName,
): SchemaItem {
return $this->findSomeTypeFromAttribute(
$fromThis,
Expand All @@ -830,7 +830,7 @@ private function findSomeType(
private function findSomeTypeFromAttribute(
SchemaItem $fromThis,
\DOMElement $node,
string $attributeName
string $attributeName,
): SchemaItem {
return $this->findType(
$fromThis->getSchema(),
Expand Down Expand Up @@ -919,7 +919,7 @@ private function findAndSetSomeBase(Type $type, Base $setBaseOnThis, \DOMElement

private function loadExtensionChildNodes(
BaseComplexType $type,
\DOMElement $node
\DOMElement $node,
): void {
self::againstDOMNodeList(
$node,
Expand All @@ -941,7 +941,7 @@ function (\DOMElement $node, \DOMElement $childNode) use ($type): void {
private function loadChildAttributesAndAttributeGroups(
BaseComplexType $type,
\DOMElement $node,
\DOMElement $childNode
\DOMElement $childNode,
): void {
switch ($childNode->localName) {
case 'attribute':
Expand Down Expand Up @@ -974,7 +974,7 @@ private function loadRestriction(Type $type, \DOMElement $node): void
$node,
function (
\DOMElement $node,
\DOMElement $childNode
\DOMElement $childNode,
) use (
$type,
$restriction
Expand All @@ -995,7 +995,7 @@ function (Type $restType) use ($restriction): void {
private function loadRestrictionChildNodes(
Type $type,
Restriction $restriction,
\DOMElement $node
\DOMElement $node,
): void {
self::againstDOMNodeList(
$node,
Expand Down Expand Up @@ -1032,11 +1032,17 @@ private static function splitParts(\DOMElement $node, string $typeName): array
[$prefix, $name] = explode(':', $typeName);
}

/**
* @psalm-suppress PossiblyNullArgument
*/
// Get namespace URI for prefix. If prefix is null, it will return the default namespace
$namespace = $node->lookupNamespaceUri($prefix);

// If no namespace is found, throw an exception only if a prefix was provided.
// If no prefix was provided and the above lookup failed, this means that there
// was no defalut namespace defined, making the element part of no namespace.
// In this case, we should not throw an exception since this is valid xml.
if (!$namespace && null !== $prefix) {
throw new TypeException(sprintf("Can't find namespace for prefix '%s', at line %d in %s ", $prefix, $node->getLineNo(), $node->ownerDocument->documentURI));
}

return [
$name,
$namespace,
Expand All @@ -1048,11 +1054,6 @@ private function findAttributeItem(Schema $schema, \DOMElement $node, string $ty
{
[$name, $namespace] = self::splitParts($node, $typeName);

/**
* @var string|null $namespace
*/
$namespace = $namespace ?: $schema->getTargetNamespace();

try {
/**
* @var AttributeItem $out
Expand All @@ -1069,11 +1070,6 @@ private function findAttributeGroup(Schema $schema, \DOMElement $node, string $t
{
[$name, $namespace] = self::splitParts($node, $typeName);

/**
* @var string|null $namespace
*/
$namespace = $namespace ?: $schema->getTargetNamespace();

try {
/**
* @var AttributeGroup $out
Expand All @@ -1090,11 +1086,6 @@ private function findElement(Schema $schema, \DOMElement $node, string $typeName
{
[$name, $namespace] = self::splitParts($node, $typeName);

/**
* @var string|null $namespace
*/
$namespace = $namespace ?: $schema->getTargetNamespace();

try {
return $schema->findElement((string) $name, $namespace);
} catch (TypeNotFoundException $e) {
Expand All @@ -1106,11 +1097,6 @@ private function findGroup(Schema $schema, \DOMElement $node, string $typeName):
{
[$name, $namespace] = self::splitParts($node, $typeName);

/**
* @var string|null $namespace
*/
$namespace = $namespace ?: $schema->getTargetNamespace();

try {
/**
* @var Group $out
Expand All @@ -1127,11 +1113,6 @@ private function findType(Schema $schema, \DOMElement $node, string $typeName):
{
[$name, $namespace] = self::splitParts($node, $typeName);

/**
* @var string|null $namespace
*/
$namespace = $namespace ?: $schema->getTargetNamespace();

$tryFindType = static function (Schema $schema, string $name, ?string $namespace): ?SchemaItem {
try {
return $schema->findType($name, $namespace);
Expand Down Expand Up @@ -1199,13 +1180,17 @@ private function fillItemNonLocalType(Item $element, \DOMElement $node): void
*/
$type = $this->findSomeType($element, $node, 'type');
} else {
$prefix = $node->lookupPrefix(self::XSD_NS);
if ($prefix) {
$prefix .= ':';
}
/**
* @var Type
*/
$type = $this->findSomeTypeFromAttribute(
$element,
$node,
$node->lookupPrefix(self::XSD_NS) . ':anyType'
$prefix . 'anyType'
);
}

Expand All @@ -1214,7 +1199,7 @@ private function fillItemNonLocalType(Item $element, \DOMElement $node): void

private function loadImport(
Schema $schema,
\DOMElement $node
\DOMElement $node,
): \Closure {
$namespace = $node->getAttribute('namespace');
$schemaLocation = $node->getAttribute('schemaLocation');
Expand Down Expand Up @@ -1268,7 +1253,7 @@ private function createOrUseSchemaForNs(Schema $schema, string $namespace): Sche
private function loadImportFresh(
string $namespace,
Schema $schema,
string $file
string $file,
): \Closure {
return function () use ($namespace, $schema, $file): void {
$dom = $this->getDOM(
Expand Down Expand Up @@ -1554,7 +1539,7 @@ private function addAttributeFromAttributeOrRef(
BaseComplexType $type,
\DOMElement $childNode,
Schema $schema,
\DOMElement $node
\DOMElement $node,
): void {
$attribute = $this->getAttributeFromAttributeOrRef(
$childNode,
Expand All @@ -1569,7 +1554,7 @@ private function findSomethingLikeAttributeGroup(
Schema $schema,
\DOMElement $node,
\DOMElement $childNode,
AttributeContainer $addToThis
AttributeContainer $addToThis,
): void {
$attribute = $this->findAttributeGroup($schema, $node, $childNode->getAttribute('ref'));
$addToThis->addAttribute($attribute);
Expand Down Expand Up @@ -1600,7 +1585,7 @@ private function setLoadedSchema(string $namespace, Schema $schema): void
private function setSchemaThingsFromNode(
Schema $schema,
\DOMElement $node,
?Schema $parent = null
?Schema $parent = null,
): void {
$schema->setDoc($this->getDocumentation($node));

Expand Down
10 changes: 5 additions & 5 deletions tests/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function testBase(): void
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema targetNamespace="http://www.example.com" xmlns:ex="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:attribute name="myAttribute" type="xs:string"></xs:attribute>
<xs:attribute name="myAttributeOptions" type="xs:string" use="required" nil="true"></xs:attribute>

Expand Down Expand Up @@ -101,22 +101,22 @@ public function testAttributeUseOverriding(): void
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema targetNamespace="http://www.example.com" xmlns:ex="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:attribute name="lang" use="optional" type="xs:language"/>
<xs:element name="Name">
<xs:complexType mixed="true">
<xs:attribute ref="lang" use="required"/>
<xs:attribute ref="ex:lang" use="required"/>
</xs:complexType>
</xs:element>
<xs:complexType name="MyNameType">
<xs:sequence>
<xs:element ref="Name"/>
<xs:element ref="ex:Name"/>
</xs:sequence>
</xs:complexType>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="myName" type="MyNameType"/>
<xs:element name="myName" type="ex:MyNameType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Expand Down
Loading