From 0219cd2732607a34384f452fc76c12dd2bcfe28e Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Tue, 4 Jun 2024 08:15:33 +0200 Subject: [PATCH] Rework meta to custom attributes --- .../Attribute/AbstractAttributeItem.php | 4 +- src/Schema/Attribute/AttributeSingle.php | 4 +- src/Schema/CustomAttribute.php | 45 +++++++++++++ .../CustomAttributeContainerInterface.php | 18 +++++ src/Schema/CustomAttributeContainerTrait.php | 29 +++++++++ src/Schema/Element/AbstractElementSingle.php | 4 +- src/Schema/Element/ElementSingle.php | 4 +- src/Schema/MetaInformation.php | 65 ------------------- .../MetaInformationContainerInterface.php | 18 ----- src/Schema/MetaInformationContainerTrait.php | 29 --------- src/SchemaReader.php | 30 +++------ tests/AttributesTest.php | 54 +++++++-------- tests/ElementsTest.php | 51 +++++++-------- 13 files changed, 158 insertions(+), 197 deletions(-) create mode 100644 src/Schema/CustomAttribute.php create mode 100644 src/Schema/CustomAttributeContainerInterface.php create mode 100644 src/Schema/CustomAttributeContainerTrait.php delete mode 100644 src/Schema/MetaInformation.php delete mode 100644 src/Schema/MetaInformationContainerInterface.php delete mode 100644 src/Schema/MetaInformationContainerTrait.php diff --git a/src/Schema/Attribute/AbstractAttributeItem.php b/src/Schema/Attribute/AbstractAttributeItem.php index af874f4..47ad81b 100644 --- a/src/Schema/Attribute/AbstractAttributeItem.php +++ b/src/Schema/Attribute/AbstractAttributeItem.php @@ -5,11 +5,11 @@ namespace GoetasWebservices\XML\XSDReader\Schema\Attribute; use GoetasWebservices\XML\XSDReader\Schema\Item; -use GoetasWebservices\XML\XSDReader\Schema\MetaInformationContainerTrait; +use GoetasWebservices\XML\XSDReader\Schema\CustomAttributeContainerTrait; abstract class AbstractAttributeItem extends Item implements AttributeSingle { - use MetaInformationContainerTrait; + use CustomAttributeContainerTrait; protected ?string $fixed = null; diff --git a/src/Schema/Attribute/AttributeSingle.php b/src/Schema/Attribute/AttributeSingle.php index 883473b..e90449b 100644 --- a/src/Schema/Attribute/AttributeSingle.php +++ b/src/Schema/Attribute/AttributeSingle.php @@ -4,10 +4,10 @@ namespace GoetasWebservices\XML\XSDReader\Schema\Attribute; -use GoetasWebservices\XML\XSDReader\Schema\MetaInformationContainerInterface; +use GoetasWebservices\XML\XSDReader\Schema\CustomAttributeContainerInterface; use GoetasWebservices\XML\XSDReader\Schema\Type\Type; -interface AttributeSingle extends AttributeItem, MetaInformationContainerInterface +interface AttributeSingle extends AttributeItem, CustomAttributeContainerInterface { public const USE_OPTIONAL = 'optional'; diff --git a/src/Schema/CustomAttribute.php b/src/Schema/CustomAttribute.php new file mode 100644 index 0000000..b04833f --- /dev/null +++ b/src/Schema/CustomAttribute.php @@ -0,0 +1,45 @@ + + */ +class CustomAttribute +{ + private string $namespaceURI; + + private string $name; + + private string $value; + + public function __construct( + string $namespaceURI, + string $name, + string $value + ) { + $this->namespaceURI = $namespaceURI; + $this->name = $name; + $this->value = $value; + } + + public function getNamespaceURI(): string + { + return $this->namespaceURI; + } + + public function getName(): string + { + return $this->name; + } + + public function getValue(): string + { + return $this->value; + } +} diff --git a/src/Schema/CustomAttributeContainerInterface.php b/src/Schema/CustomAttributeContainerInterface.php new file mode 100644 index 0000000..a62e329 --- /dev/null +++ b/src/Schema/CustomAttributeContainerInterface.php @@ -0,0 +1,18 @@ + $customAttributes + */ + public function setCustomAttributes(array $customAttributes): void; + + /** + * @return list + */ + public function getCustomAttributes(): array; +} diff --git a/src/Schema/CustomAttributeContainerTrait.php b/src/Schema/CustomAttributeContainerTrait.php new file mode 100644 index 0000000..6df52e5 --- /dev/null +++ b/src/Schema/CustomAttributeContainerTrait.php @@ -0,0 +1,29 @@ + + */ + protected array $customAttributes = []; + + /** + * @return list + */ + public function getCustomAttributes(): array + { + return $this->customAttributes; + } + + /** + * @param list $customAttributes + */ + public function setCustomAttributes(array $customAttributes): void + { + $this->customAttributes = $customAttributes; + } +} diff --git a/src/Schema/Element/AbstractElementSingle.php b/src/Schema/Element/AbstractElementSingle.php index 918f513..4bec7ee 100644 --- a/src/Schema/Element/AbstractElementSingle.php +++ b/src/Schema/Element/AbstractElementSingle.php @@ -5,11 +5,11 @@ namespace GoetasWebservices\XML\XSDReader\Schema\Element; use GoetasWebservices\XML\XSDReader\Schema\Item; -use GoetasWebservices\XML\XSDReader\Schema\MetaInformationContainerTrait; +use GoetasWebservices\XML\XSDReader\Schema\CustomAttributeContainerTrait; class AbstractElementSingle extends Item implements ElementSingle, InterfaceSetAbstract { - use MetaInformationContainerTrait; + use CustomAttributeContainerTrait; protected int $min = 1; diff --git a/src/Schema/Element/ElementSingle.php b/src/Schema/Element/ElementSingle.php index 2e6ba85..9c0066e 100644 --- a/src/Schema/Element/ElementSingle.php +++ b/src/Schema/Element/ElementSingle.php @@ -4,10 +4,10 @@ namespace GoetasWebservices\XML\XSDReader\Schema\Element; -use GoetasWebservices\XML\XSDReader\Schema\MetaInformationContainerInterface; +use GoetasWebservices\XML\XSDReader\Schema\CustomAttributeContainerInterface; use GoetasWebservices\XML\XSDReader\Schema\Type\Type; -interface ElementSingle extends ElementItem, InterfaceSetMinMax, InterfaceSetFixed, InterfaceSetDefault, MetaInformationContainerInterface +interface ElementSingle extends ElementItem, InterfaceSetMinMax, InterfaceSetFixed, InterfaceSetDefault, CustomAttributeContainerInterface { public function getType(): ?Type; diff --git a/src/Schema/MetaInformation.php b/src/Schema/MetaInformation.php deleted file mode 100644 index fe3f617..0000000 --- a/src/Schema/MetaInformation.php +++ /dev/null @@ -1,65 +0,0 @@ -contextSchema = $contextSchema; - $this->namespaceURI = $namespaceURI; - $this->name = $name; - $this->value = $value; - } - - public function getContextSchema(): Schema - { - return $this->contextSchema; - } - - public function getNamespaceURI(): string - { - return $this->namespaceURI; - } - - public function getName(): string - { - return $this->name; - } - - public function getValue(): string - { - return $this->value; - } -} diff --git a/src/Schema/MetaInformationContainerInterface.php b/src/Schema/MetaInformationContainerInterface.php deleted file mode 100644 index b407869..0000000 --- a/src/Schema/MetaInformationContainerInterface.php +++ /dev/null @@ -1,18 +0,0 @@ - $meta - */ - public function setMeta(array $meta): void; - - /** - * @return list - */ - public function getMeta(): array; -} diff --git a/src/Schema/MetaInformationContainerTrait.php b/src/Schema/MetaInformationContainerTrait.php deleted file mode 100644 index fe75463..0000000 --- a/src/Schema/MetaInformationContainerTrait.php +++ /dev/null @@ -1,29 +0,0 @@ - - */ - protected array $meta = []; - - /** - * @return list - */ - public function getMeta(): array - { - return $this->meta; - } - - /** - * @param list $meta - */ - public function setMeta(array $meta): void - { - $this->meta = $meta; - } -} diff --git a/src/SchemaReader.php b/src/SchemaReader.php index 0f2999a..711ae05 100644 --- a/src/SchemaReader.php +++ b/src/SchemaReader.php @@ -35,7 +35,7 @@ use GoetasWebservices\XML\XSDReader\Schema\Inheritance\Restriction; use GoetasWebservices\XML\XSDReader\Schema\Inheritance\RestrictionType; use GoetasWebservices\XML\XSDReader\Schema\Item; -use GoetasWebservices\XML\XSDReader\Schema\MetaInformation; +use GoetasWebservices\XML\XSDReader\Schema\CustomAttribute; use GoetasWebservices\XML\XSDReader\Schema\Schema; use GoetasWebservices\XML\XSDReader\Schema\SchemaItem; use GoetasWebservices\XML\XSDReader\Schema\Type\BaseComplexType; @@ -239,19 +239,18 @@ private function fillAttribute(AttributeSingle $attribute, \DOMElement $node): v $attribute->setUse($node->getAttribute('use')); } - $attribute->setMeta($this->loadMetaAttributesForElement($attribute, $node)); + $attribute->setCustomAttributes($this->loadCustomAttributesForElement($attribute, $node)); } /** - * @return list + * @return list */ - private function loadMetaAttributesForElement(SchemaItem $item, \DOMElement $node): array + private function loadCustomAttributesForElement(SchemaItem $item, \DOMElement $node): array { - $meta = []; + $customAttributes = []; foreach ($node->attributes as $attr) { if (null !== $attr->namespaceURI && self::XSD_NS !== $attr->namespaceURI) { - $meta[] = new MetaInformation( - $this->findSchemaForNamespace($item->getSchema(), $attr->parentNode->namespaceURI), + $customAttributes[] = new CustomAttribute( $attr->namespaceURI, $attr->name, $attr->value @@ -259,7 +258,7 @@ private function loadMetaAttributesForElement(SchemaItem $item, \DOMElement $nod } } - return $meta; + return $customAttributes; } private function loadAttributeOrElementDef( @@ -1117,19 +1116,6 @@ private function findType(Schema $schema, \DOMElement $node, string $typeName): throw new TypeException(sprintf("Can't find %s named {%s}#%s, at line %d in %s ", 'type', $namespace, $name, $node->getLineNo(), $node->ownerDocument->documentURI)); } - public function findSchemaForNamespace(Schema $currentSchema, string $namespace): Schema - { - if ($currentSchema->getTargetNamespace() === $namespace) { - return $currentSchema; - } - - if (array_key_exists($namespace, $this->loadedSchemas) && count($this->loadedSchemas[$namespace]) > 0) { - return $this->loadedSchemas[$namespace][0]; - } - - throw new SchemaException(sprintf("Can't find schema for namespace %s", $namespace)); - } - private function fillItem(Item $element, \DOMElement $node, ?\DOMElement $parentNode = null): void { if ($element instanceof ElementDef) { @@ -1494,7 +1480,7 @@ private function fillElement(AbstractElementSingle $element, \DOMElement $node): } } - $element->setMeta($this->loadMetaAttributesForElement($element, $node)); + $element->setCustomAttributes($this->loadCustomAttributesForElement($element, $node)); } private function addAttributeFromAttributeOrRef( diff --git a/tests/AttributesTest.php b/tests/AttributesTest.php index 2a057fd..90ab0f3 100644 --- a/tests/AttributesTest.php +++ b/tests/AttributesTest.php @@ -11,7 +11,6 @@ use GoetasWebservices\XML\XSDReader\Schema\Attribute\Group; use GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType; use GoetasWebservices\XML\XSDReader\Schema\Type\SimpleType; -use GoetasWebservices\XML\XSDReader\SchemaReader; class AttributesTest extends BaseTest { @@ -146,38 +145,37 @@ public function testAttributeUseOverriding(): void self::assertEquals(AttributeSingle::USE_REQUIRED, $attribute->getUse()); } - public function testMetaInformation(): void + public function testCustomAttributesInformation(): void { $schema = $this->reader->readString( ' - + ' ); $myAttribute = $schema->findAttribute('myAttribute', 'http://www.example.com'); self::assertInstanceOf(AttributeDef::class, $myAttribute); - $meta = $myAttribute->getMeta(); - self::assertCount(1, $meta); - self::assertEquals('meta', $meta[0]->getName()); - self::assertEquals('hello', $meta[0]->getValue()); - self::assertEquals('http://www.example.com', $meta[0]->getNamespaceURI()); - self::assertSame(SchemaReader::XSD_NS, $meta[0]->getContextSchema()->getTargetNamespace()); + $customAttributes = $myAttribute->getCustomAttributes(); + self::assertCount(1, $customAttributes); + self::assertEquals('customAttributes', $customAttributes[0]->getName()); + self::assertEquals('hello', $customAttributes[0]->getValue()); + self::assertEquals('http://www.example.com', $customAttributes[0]->getNamespaceURI()); } - public function testExternalSchemaReferencingMetaInformationPrefixed(): void + public function testExternalSchemaReferencingCustomAttributesInformationPrefixed(): void { $dom = new \DOMDocument(); $dom->loadXML( ' - + - + '); @@ -186,28 +184,27 @@ public function testExternalSchemaReferencingMetaInformationPrefixed(): void $myAttribute = $schema->findAttribute('myAttribute', 'http://www.example.com'); self::assertInstanceOf(AttributeDef::class, $myAttribute); - $meta = $myAttribute->getMeta(); - self::assertCount(1, $meta); - self::assertEquals('metaType', $meta[0]->getName()); - self::assertEquals('xs:string', $meta[0]->getValue()); + $customAttributes = $myAttribute->getCustomAttributes(); + self::assertCount(1, $customAttributes); + self::assertEquals('customAttributesType', $customAttributes[0]->getName()); + self::assertEquals('xs:string', $customAttributes[0]->getValue()); - $refAttr = $schema->findAttribute('metaType', 'http://www.ref.com'); - self::assertSame($refAttr->getSchema()->getTargetNamespace(), $meta[0]->getNamespaceURI()); - self::assertSame(SchemaReader::XSD_NS, $meta[0]->getContextSchema()->getTargetNamespace()); + $refAttr = $schema->findAttribute('customAttributesType', 'http://www.ref.com'); + self::assertSame($refAttr->getSchema()->getTargetNamespace(), $customAttributes[0]->getNamespaceURI()); } - public function testExternalSchemaReferencingMetaInformationUnprefixed(): void + public function testExternalSchemaReferencingCustomAttributesInformationUnprefixed(): void { $dom = new \DOMDocument(); $dom->loadXML( ' - + - + '); @@ -216,14 +213,13 @@ public function testExternalSchemaReferencingMetaInformationUnprefixed(): void $myAttribute = $schema->findAttribute('myAttribute', 'http://www.example.com'); self::assertInstanceOf(AttributeDef::class, $myAttribute); - $meta = $myAttribute->getMeta(); + $customAttributes = $myAttribute->getCustomAttributes(); - self::assertCount(1, $meta); - self::assertEquals('metaType', $meta[0]->getName()); - self::assertEquals('string', $meta[0]->getValue()); + self::assertCount(1, $customAttributes); + self::assertEquals('customAttributesType', $customAttributes[0]->getName()); + self::assertEquals('string', $customAttributes[0]->getValue()); - $refAttr = $schema->findAttribute('metaType', 'http://www.ref.com'); - self::assertSame($refAttr->getSchema()->getTargetNamespace(), $meta[0]->getNamespaceURI()); - self::assertSame(SchemaReader::XSD_NS, $meta[0]->getContextSchema()->getTargetNamespace()); + $refAttr = $schema->findAttribute('customAttributesType', 'http://www.ref.com'); + self::assertSame($refAttr->getSchema()->getTargetNamespace(), $customAttributes[0]->getNamespaceURI()); } } diff --git a/tests/ElementsTest.php b/tests/ElementsTest.php index 3a0a1dc..dda9431 100644 --- a/tests/ElementsTest.php +++ b/tests/ElementsTest.php @@ -274,27 +274,26 @@ public function testSequenceElementDocs(): void self::assertSame('Alone description', $aloneElement->getDoc()); } - public function testMetaInformation(): void + public function testCustomAttributesInformation(): void { $schema = $this->reader->readString( ' - + ' ); $myElement = $schema->findElement('myElement', 'http://www.example.com'); self::assertInstanceOf(ElementDef::class, $myElement); - $meta = $myElement->getMeta(); - self::assertCount(1, $meta); - self::assertEquals('meta', $meta[0]->getName()); - self::assertEquals('hello', $meta[0]->getValue()); - self::assertEquals('http://www.example.com', $meta[0]->getNamespaceURI()); - self::assertSame(SchemaReader::XSD_NS, $meta[0]->getContextSchema()->getTargetNamespace()); + $customAttributes = $myElement->getCustomAttributes(); + self::assertCount(1, $customAttributes); + self::assertEquals('customAttributes', $customAttributes[0]->getName()); + self::assertEquals('hello', $customAttributes[0]->getValue()); + self::assertEquals('http://www.example.com', $customAttributes[0]->getNamespaceURI()); } - public function testDfdlElementMetaInformation(): void + public function testDfdlElementCustomAttributesInformation(): void { $schema = $this->reader->readString( ' @@ -315,28 +314,28 @@ public function testDfdlElementMetaInformation(): void $myElement = $schema->findElement('myElement', 'http://www.example.com'); self::assertInstanceOf(ElementDef::class, $myElement); - $meta = $myElement->getMeta(); + $customAttributes = $myElement->getCustomAttributes(); $namespaceUri = 'http://www.ogf.org/dfdl/dfdl-1.0/extensions'; - self::assertCount(5, $meta); - self::assertEquals($namespaceUri, $meta[0]->getNamespaceURI()); - self::assertEquals('encoding', $meta[0]->getName()); - self::assertEquals('iso-8859-1', $meta[0]->getValue()); + self::assertCount(5, $customAttributes); + self::assertEquals($namespaceUri, $customAttributes[0]->getNamespaceURI()); + self::assertEquals('encoding', $customAttributes[0]->getName()); + self::assertEquals('iso-8859-1', $customAttributes[0]->getValue()); - self::assertEquals($namespaceUri, $meta[1]->getNamespaceURI()); - self::assertEquals('initiator', $meta[1]->getName()); - self::assertEquals('UNA', $meta[1]->getValue()); + self::assertEquals($namespaceUri, $customAttributes[1]->getNamespaceURI()); + self::assertEquals('initiator', $customAttributes[1]->getName()); + self::assertEquals('UNA', $customAttributes[1]->getValue()); - self::assertEquals($namespaceUri, $meta[2]->getNamespaceURI()); - self::assertEquals('length', $meta[2]->getName()); - self::assertEquals('6', $meta[2]->getValue()); + self::assertEquals($namespaceUri, $customAttributes[2]->getNamespaceURI()); + self::assertEquals('length', $customAttributes[2]->getName()); + self::assertEquals('6', $customAttributes[2]->getValue()); - self::assertEquals($namespaceUri, $meta[3]->getNamespaceURI()); - self::assertEquals('lengthKind', $meta[3]->getName()); - self::assertEquals('explicit', $meta[3]->getValue()); + self::assertEquals($namespaceUri, $customAttributes[3]->getNamespaceURI()); + self::assertEquals('lengthKind', $customAttributes[3]->getName()); + self::assertEquals('explicit', $customAttributes[3]->getValue()); - self::assertEquals($namespaceUri, $meta[4]->getNamespaceURI()); - self::assertEquals('terminator', $meta[4]->getName()); - self::assertEquals('%NL;%WSP*; %WSP*;', $meta[4]->getValue()); + self::assertEquals($namespaceUri, $customAttributes[4]->getNamespaceURI()); + self::assertEquals('terminator', $customAttributes[4]->getName()); + self::assertEquals('%NL;%WSP*; %WSP*;', $customAttributes[4]->getValue()); } }