Skip to content

Commit

Permalink
Rework meta to custom attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Jun 4, 2024
1 parent ee6189f commit 0219cd2
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 197 deletions.
4 changes: 2 additions & 2 deletions src/Schema/Attribute/AbstractAttributeItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/Schema/Attribute/AttributeSingle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
45 changes: 45 additions & 0 deletions src/Schema/CustomAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace GoetasWebservices\XML\XSDReader\Schema;

/**
* Contains the information of additional custom attributes that are not part of the XSD specification.
*
* For example:
* <xsd:element name="myElement" dfdl:encoding="iso-8859-1" />
*/
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;
}
}
18 changes: 18 additions & 0 deletions src/Schema/CustomAttributeContainerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace GoetasWebservices\XML\XSDReader\Schema;

interface CustomAttributeContainerInterface
{
/**
* @param list<CustomAttribute> $customAttributes
*/
public function setCustomAttributes(array $customAttributes): void;

/**
* @return list<CustomAttribute>
*/
public function getCustomAttributes(): array;
}
29 changes: 29 additions & 0 deletions src/Schema/CustomAttributeContainerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace GoetasWebservices\XML\XSDReader\Schema;

trait CustomAttributeContainerTrait
{
/**
* @var list<CustomAttribute>
*/
protected array $customAttributes = [];

/**
* @return list<CustomAttribute>
*/
public function getCustomAttributes(): array
{
return $this->customAttributes;
}

/**
* @param list<CustomAttribute> $customAttributes
*/
public function setCustomAttributes(array $customAttributes): void
{
$this->customAttributes = $customAttributes;
}
}
4 changes: 2 additions & 2 deletions src/Schema/Element/AbstractElementSingle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/Schema/Element/ElementSingle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
65 changes: 0 additions & 65 deletions src/Schema/MetaInformation.php

This file was deleted.

18 changes: 0 additions & 18 deletions src/Schema/MetaInformationContainerInterface.php

This file was deleted.

29 changes: 0 additions & 29 deletions src/Schema/MetaInformationContainerTrait.php

This file was deleted.

30 changes: 8 additions & 22 deletions src/SchemaReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -239,27 +239,26 @@ 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<MetaInformation>
* @return list<CustomAttribute>
*/
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
);
}
}

return $meta;
return $customAttributes;
}

private function loadAttributeOrElementDef(
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down
Loading

0 comments on commit 0219cd2

Please sign in to comment.