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];
|
||||
}
|
||||
}
|
@ -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…
Reference in New Issue