Add TextAndAttributesXmlStructure

2.x 2.3.1
Julien Rosset 3 years ago
parent e9f05334fc
commit 44672f1f70

@ -1,31 +0,0 @@
<?php
namespace jrosset\EnvReader\XmlStructure;
use SimpleXMLElement;
/**
* Structure about a single attribute
*/
class AttributeSingleXmlStructure implements IXmlStructure {
/**
* @var string The attribute name
*/
private string $attributeName;
/**
* Initialize
*
* @param string $attributeName The attribute name
*/
public function __construct (string $attributeName) {
$this->attributeName = $attributeName;
}
/**
* @inheritDoc
*/
public function parseXml (SimpleXMLElement $xmlNode): string {
return (string)$xmlNode[$this->attributeName];
}
}

@ -8,7 +8,7 @@ use SimpleXMLElement;
/** /**
* Structure about XML attributes * Structure about XML attributes
*/ */
class AttributeListXmlStructure implements IXmlStructure { class AttributesXmlStructure implements IXmlStructure {
/** /**
* @var array Allowed attributes. Empty for all * @var array Allowed attributes. Empty for all
*/ */

@ -10,10 +10,8 @@ use SimpleXMLElement;
* Structure about children nodes * Structure about children nodes
*/ */
class ChildrenXmlStructure implements IXmlStructure { class ChildrenXmlStructure implements IXmlStructure {
/** use TAttributeListXmlStructure;
* @var AttributeListXmlStructure|null The attribute structure. Null if none
*/
private ?AttributeListXmlStructure $attributesStructure;
/** /**
* @var ArrayClass The children nodes * @var ArrayClass The children nodes
*/ */
@ -27,22 +25,10 @@ class ChildrenXmlStructure implements IXmlStructure {
* Initialize * Initialize
*/ */
public function __construct () { public function __construct () {
$this->attributesStructure = null;
$this->childrenNodes = new ArrayClass(); $this->childrenNodes = new ArrayClass();
$this->mapping = 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 * Add a child node
* *
@ -64,12 +50,7 @@ class ChildrenXmlStructure implements IXmlStructure {
* @inheritDoc * @inheritDoc
*/ */
public function parseXml (SimpleXMLElement $xmlNode): InsensitiveCaseArrayClass { public function parseXml (SimpleXMLElement $xmlNode): InsensitiveCaseArrayClass {
if ($this->attributesStructure === null) { $properties = $this->parseXmlAttributes($xmlNode);
$properties = new InsensitiveCaseArrayClass();
}
else {
$properties = $this->attributesStructure->parseXml($xmlNode);
}
/** @var IXmlStructure $childNodeStructure */ /** @var IXmlStructure $childNodeStructure */
foreach ($this->childrenNodes as $childNodeName => $childNodeStructure) { foreach ($this->childrenNodes as $childNodeName => $childNodeStructure) {

@ -0,0 +1,43 @@
<?php
namespace jrosset\EnvReader\XmlStructure;
use jrosset\ArrayClasses\InsensitiveCaseArrayClass;
use SimpleXMLElement;
/**
* Implementation for {@see AttributesXmlStructure} property
*/
trait TAttributeListXmlStructure {
/**
* @var AttributesXmlStructure|null The attribute structure. Null if none
*/
private ?AttributesXmlStructure $attributesStructure = null;
/**
* Set the attribute structure
*
* @param AttributesXmlStructure|null $attributesStructure The new attribute structure. Null if none
*
* @return $this
*/
public function setAttributesStructure (?AttributesXmlStructure $attributesStructure = null): self {
$this->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);
}
}

@ -0,0 +1,42 @@
<?php
namespace jrosset\EnvReader\XmlStructure;
use jrosset\ArrayClasses\InsensitiveCaseArrayClass;
use SimpleXMLElement;
/**
* Structure for XML node with text __and__ attributes
*/
class TextAndAttributesXmlStructure implements IXmlStructure {
use TAttributeListXmlStructure;
/**
* @var string The property name for node's text
*/
private string $textPropertyName;
/**
* @var TextXmlStructure The internal node's text
*/
private TextXmlStructure $textXmlStructure;
/**
* Initialize
*
* @param string $textPropertyName The property name for node's text
* @param TextXmlStructure|null $textXmlStructure The node's text structure. Null if default
*/
public function __construct (string $textPropertyName, ?TextXmlStructure $textXmlStructure = null) {
$this->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;
}
}
Loading…
Cancel
Save