floor12 /
dalli-api
| 1 | <?php |
||
| 2 | |||
| 3 | |||
| 4 | namespace floor12\DalliApi\Models; |
||
| 5 | |||
| 6 | |||
| 7 | use ReflectionClass; |
||
| 8 | use ReflectionException; |
||
| 9 | use SimpleXMLElement; |
||
| 10 | |||
| 11 | abstract class BaseXmlObject |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @param BaseXmlObject|null $object |
||
| 15 | * @return SimpleXMLElement |
||
| 16 | * @throws ReflectionException |
||
| 17 | */ |
||
| 18 | 7 | public function getAsXmlObject(BaseXmlObject $object = null): SimpleXMLElement |
|
| 19 | { |
||
| 20 | 7 | if (empty($object)) { |
|
| 21 | 7 | $object = $this; |
|
| 22 | } |
||
| 23 | 7 | $className = mb_strtolower((new ReflectionClass($object))->getShortName()); |
|
| 24 | 7 | $mainElement = new SimpleXMLElement("<$className></$className>"); |
|
| 25 | 7 | foreach ($object as $attributeName => $attributeValue) { |
|
| 26 | 7 | $this->processAttributeNameAndValue($mainElement, $attributeName, $attributeValue); |
|
| 27 | } |
||
| 28 | 7 | return $mainElement; |
|
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param $mainElement |
||
| 33 | * @param $attributeName |
||
| 34 | * @param $attributeValue |
||
| 35 | */ |
||
| 36 | 7 | protected function processAttributeNameAndValue($mainElement, $attributeName, $attributeValue): void |
|
| 37 | { |
||
| 38 | 7 | if (empty($attributeValue)) |
|
| 39 | 5 | return; |
|
| 40 | 7 | if (is_array($attributeValue)) { |
|
| 41 | 2 | $this->addValueAsArray($mainElement, $attributeName, $attributeValue); |
|
| 42 | 7 | } elseif (is_object($attributeValue)) { |
|
| 43 | 2 | $this->addValueAsObject($mainElement, $attributeValue); |
|
| 44 | } else { |
||
| 45 | 7 | $this->addValueAsString($mainElement, $attributeName, $attributeValue); |
|
| 46 | } |
||
| 47 | 7 | } |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @param $mainElement |
||
| 51 | * @param $attributeName |
||
| 52 | * @param $attributeValue |
||
| 53 | */ |
||
| 54 | 2 | protected function addValueAsArray($mainElement, $attributeName, $attributeValue) |
|
| 55 | { |
||
| 56 | 2 | $itemsElement = $mainElement->addChild($attributeName); |
|
| 57 | 2 | foreach ($attributeValue as $item) { |
|
| 58 | 2 | $itemElement = $itemsElement->addChild($item->getAsXmlObject()->getName(), $item->getAsXmlObject()); |
|
| 59 | 2 | foreach ($item as $itemAttributeName => $itemAttributeValue) { |
|
| 60 | 2 | $this->addValueAsString($itemElement, $itemAttributeName, $itemAttributeValue); |
|
| 61 | } |
||
| 62 | } |
||
| 63 | 2 | } |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @param $mainElement |
||
| 67 | * @param $attributeValue |
||
| 68 | */ |
||
| 69 | 2 | protected function addValueAsObject($mainElement, $attributeValue) |
|
| 70 | { |
||
| 71 | 2 | $newXmlObject = $mainElement->addChild($attributeValue->getAsXmlObject()->getName()); |
|
| 72 | 2 | foreach ($attributeValue->getAsXmlObject()->children() as $child) |
|
| 73 | 2 | $newXmlObject->addChild($child->getName(), $child); |
|
| 74 | 2 | } |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param SimpleXMLElement $mainElement |
||
| 78 | * @param $attributeName |
||
| 79 | * @param $attributeValue |
||
| 80 | * @return void |
||
| 81 | */ |
||
| 82 | 7 | protected function addValueAsString(SimpleXMLElement $mainElement, $attributeName, $attributeValue): void |
|
| 83 | { |
||
| 84 | 7 | if (empty($attributeValue)) |
|
| 85 | 2 | return; |
|
| 86 | 7 | if ($attributeName === 'title') { |
|
| 87 | 1 | $mainElement[0] = $attributeValue; |
|
| 88 | 7 | } elseif (substr($attributeName, 0, 1) === '_') { |
|
| 89 | 6 | $mainElement->addAttribute(substr($attributeName, 1), $attributeValue); |
|
| 90 | } else { |
||
| 91 | 3 | $mainElement->addChild($attributeName, $attributeValue); |
|
| 92 | } |
||
| 93 | 7 | } |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param BaseXmlObject|null $object |
||
| 97 | * @return string |
||
| 98 | * @throws ReflectionException |
||
| 99 | */ |
||
| 100 | 6 | public function getAsXmlString(BaseXmlObject $object = null): string |
|
| 101 | { |
||
| 102 | 6 | return $this->getAsXmlObject($object)->asXML(); |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 103 | } |
||
| 104 | } |
||
| 105 |