diff --git a/src/EnvReader/XmlStructure/AttributeSingleXmlStructure.php b/src/EnvReader/XmlStructure/AttributeSingleXmlStructure.php deleted file mode 100644 index 4050289..0000000 --- a/src/EnvReader/XmlStructure/AttributeSingleXmlStructure.php +++ /dev/null @@ -1,31 +0,0 @@ -attributeName = $attributeName; - } - - /** - * @inheritDoc - */ - public function parseXml (SimpleXMLElement $xmlNode): string { - return (string)$xmlNode[$this->attributeName]; - } -} \ No newline at end of file diff --git a/src/EnvReader/XmlStructure/AttributeListXmlStructure.php b/src/EnvReader/XmlStructure/AttributesXmlStructure.php similarity index 97% rename from src/EnvReader/XmlStructure/AttributeListXmlStructure.php rename to src/EnvReader/XmlStructure/AttributesXmlStructure.php index ec4f0c3..20bd7cb 100644 --- a/src/EnvReader/XmlStructure/AttributeListXmlStructure.php +++ b/src/EnvReader/XmlStructure/AttributesXmlStructure.php @@ -8,7 +8,7 @@ use SimpleXMLElement; /** * Structure about XML attributes */ -class AttributeListXmlStructure implements IXmlStructure { +class AttributesXmlStructure implements IXmlStructure { /** * @var array Allowed attributes. Empty for all */ diff --git a/src/EnvReader/XmlStructure/ChildrenXmlStructure.php b/src/EnvReader/XmlStructure/ChildrenXmlStructure.php index b081d53..7f8f3c9 100644 --- a/src/EnvReader/XmlStructure/ChildrenXmlStructure.php +++ b/src/EnvReader/XmlStructure/ChildrenXmlStructure.php @@ -10,10 +10,8 @@ use SimpleXMLElement; * Structure about children nodes */ class ChildrenXmlStructure implements IXmlStructure { - /** - * @var AttributeListXmlStructure|null The attribute structure. Null if none - */ - private ?AttributeListXmlStructure $attributesStructure; + use TAttributeListXmlStructure; + /** * @var ArrayClass The children nodes */ @@ -27,22 +25,10 @@ class ChildrenXmlStructure implements IXmlStructure { * Initialize */ public function __construct () { - $this->attributesStructure = null; $this->childrenNodes = new ArrayClass(); $this->mapping = new ArrayClass(); } - /** - * Set the attribute structure - * - * @param AttributeListXmlStructure|null $attributesStructure The new attribute structure. Null if none - * - * @return $this - */ - public function setAttributesStructure (?AttributeListXmlStructure $attributesStructure = null): self { - $this->attributesStructure = $attributesStructure; - return $this; - } /** * Add a child node * @@ -64,12 +50,7 @@ class ChildrenXmlStructure implements IXmlStructure { * @inheritDoc */ public function parseXml (SimpleXMLElement $xmlNode): InsensitiveCaseArrayClass { - if ($this->attributesStructure === null) { - $properties = new InsensitiveCaseArrayClass(); - } - else { - $properties = $this->attributesStructure->parseXml($xmlNode); - } + $properties = $this->parseXmlAttributes($xmlNode); /** @var IXmlStructure $childNodeStructure */ foreach ($this->childrenNodes as $childNodeName => $childNodeStructure) { diff --git a/src/EnvReader/XmlStructure/TAttributeListXmlStructure.php b/src/EnvReader/XmlStructure/TAttributeListXmlStructure.php new file mode 100644 index 0000000..dfe3511 --- /dev/null +++ b/src/EnvReader/XmlStructure/TAttributeListXmlStructure.php @@ -0,0 +1,43 @@ +attributesStructure = $attributesStructure; + return $this; + } + + /** + * Parse the attributes + * + * @param SimpleXMLElement $xmlNode The XML node + * + * @return mixed The extracted attributes + */ + protected function parseXmlAttributes (SimpleXMLElement $xmlNode): InsensitiveCaseArrayClass { + if ($this->attributesStructure === null) { + return new InsensitiveCaseArrayClass(); + } + + return $this->attributesStructure->parseXml($xmlNode); + } +} \ No newline at end of file diff --git a/src/EnvReader/XmlStructure/TextAndAttributesXmlStructure.php b/src/EnvReader/XmlStructure/TextAndAttributesXmlStructure.php new file mode 100644 index 0000000..67e3797 --- /dev/null +++ b/src/EnvReader/XmlStructure/TextAndAttributesXmlStructure.php @@ -0,0 +1,42 @@ +textPropertyName = $textPropertyName; + $this->textXmlStructure = $textXmlStructure ?? new TextXmlStructure(); + } + + /** + * @inheritDoc + */ + public function parseXml (SimpleXMLElement $xmlNode): InsensitiveCaseArrayClass { + $properties = $this->parseXmlAttributes($xmlNode); + $properties->set($this->textPropertyName, $this->textXmlStructure->parseXml($xmlNode)); + return $properties; + } +} \ No newline at end of file