|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace a9f\FractorXml\Helper; |
| 6 | + |
| 7 | +trait FlexFormHelperTrait |
| 8 | +{ |
| 9 | + protected function extractDomElementByKey(?\DOMElement $element, string $key): ?\DOMElement |
| 10 | + { |
| 11 | + if (!$element instanceof \DOMElement) { |
| 12 | + return null; |
| 13 | + } |
| 14 | + |
| 15 | + foreach ($element->childNodes as $childNode) { |
| 16 | + if (!$childNode instanceof \DOMElement) { |
| 17 | + continue; |
| 18 | + } |
| 19 | + |
| 20 | + $itemKey = $childNode->nodeName; |
| 21 | + if ($key === $itemKey) { |
| 22 | + return $childNode; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + return null; |
| 27 | + } |
| 28 | + |
| 29 | + protected function hasKey(?\DOMElement $columnItemConfigurationArray, string $configKey): bool |
| 30 | + { |
| 31 | + if (!$columnItemConfigurationArray instanceof \DOMElement) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + foreach ($columnItemConfigurationArray->childNodes as $item) { |
| 36 | + if (!$item instanceof \DOMElement) { |
| 37 | + continue; |
| 38 | + } |
| 39 | + |
| 40 | + if ($this->isValue($item->nodeName, $configKey)) { |
| 41 | + return true; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + private function isConfigType(?\DOMElement $columnItemConfigurationArray, string $type): bool |
| 49 | + { |
| 50 | + if (!$columnItemConfigurationArray instanceof \DOMElement) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + return $this->hasKeyValuePair($columnItemConfigurationArray, 'type', $type); |
| 55 | + } |
| 56 | + |
| 57 | + private function configIsOfRenderType(?\DOMElement $columnItemConfigurationArray, string $expectedRenderType): bool |
| 58 | + { |
| 59 | + if (!$columnItemConfigurationArray instanceof \DOMElement) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + return $this->hasKeyValuePair($columnItemConfigurationArray, 'renderType', $expectedRenderType); |
| 64 | + } |
| 65 | + |
| 66 | + private function hasRenderType(\DOMElement $columnItemConfigurationArray): bool |
| 67 | + { |
| 68 | + $renderTypeItem = $this->extractDomElementByKey($columnItemConfigurationArray, 'renderType'); |
| 69 | + return $renderTypeItem !== null; |
| 70 | + } |
| 71 | + |
| 72 | + private function hasKeyValuePair(\DOMElement $configValueArray, string $configKey, string $expectedValue): bool |
| 73 | + { |
| 74 | + foreach ($configValueArray->childNodes as $configItemValue) { |
| 75 | + if (!$configItemValue instanceof \DOMElement) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + if ($this->isValue($configItemValue->nodeName, $configKey) |
| 80 | + && $this->isValue($configItemValue->textContent, $expectedValue) |
| 81 | + ) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + private function removeChildElementFromDomElementByKey(\DOMElement $configValueArray, string $configKey): bool |
| 90 | + { |
| 91 | + $arrayItemToRemove = $this->extractDomElementByKey($configValueArray, $configKey); |
| 92 | + if ($arrayItemToRemove instanceof \DOMElement && $arrayItemToRemove->parentNode instanceof \DOMElement) { |
| 93 | + $arrayItemToRemove->parentNode->removeChild($arrayItemToRemove); |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + private function isValue(string $element, string $value): bool |
| 101 | + { |
| 102 | + return $element === $value; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @see https://stackoverflow.com/a/21885789 |
| 107 | + */ |
| 108 | + private function changeTagName(\DOMDocument $domDocument, \DOMElement $node, string $elementName): void |
| 109 | + { |
| 110 | + $newNode = $domDocument->createElement($elementName); |
| 111 | + foreach ($node->childNodes as $child) { |
| 112 | + $newNode->appendChild($domDocument->importNode($child, false)); |
| 113 | + } |
| 114 | + |
| 115 | + // We don't need to copy the attributes because TCA doesn't have attributes |
| 116 | + |
| 117 | + if (!$node->parentNode instanceof \DOMNode) { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + $node->parentNode->replaceChild($newNode, $node); |
| 122 | + } |
| 123 | + |
| 124 | + private function changeTcaType(\DOMDocument $domDocument, \DOMElement $configElement, string $newType): void |
| 125 | + { |
| 126 | + $toChangeItem = $this->extractDomElementByKey($configElement, 'type'); |
| 127 | + if ($toChangeItem instanceof \DOMElement) { |
| 128 | + $toChangeItem->nodeValue = ''; |
| 129 | + $toChangeItem->appendChild($domDocument->createTextNode($newType)); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments