|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Flow\ETL\Row\Entry; |
| 6 | + |
| 7 | +use function Flow\ETL\DSL\type_xml_element; |
| 8 | +use Flow\ETL\Exception\InvalidArgumentException; |
| 9 | +use Flow\ETL\PHP\Type\Logical\XMLElementType; |
| 10 | +use Flow\ETL\PHP\Type\Type; |
| 11 | +use Flow\ETL\Row\Schema\Definition; |
| 12 | +use Flow\ETL\Row\{Entry, Reference}; |
| 13 | + |
| 14 | +/** |
| 15 | + * @implements Entry<?\DOMElement> |
| 16 | + */ |
| 17 | +final class XMLElementEntry implements Entry |
| 18 | +{ |
| 19 | + use EntryRef; |
| 20 | + |
| 21 | + private readonly XMLElementType $type; |
| 22 | + |
| 23 | + private readonly ?\DOMElement $value; |
| 24 | + |
| 25 | + /** |
| 26 | + * @param array<int|string, mixed> $attributes |
| 27 | + */ |
| 28 | + public function __construct(private readonly string $name, \DOMElement|string|null $value, array $attributes = []) |
| 29 | + { |
| 30 | + if (\is_string($value)) { |
| 31 | + $this->value = (new \DOMDocument())->createElement($value); |
| 32 | + } elseif ($value instanceof \DOMElement) { |
| 33 | + $this->value = (new \DOMDocument())->createElement($value->tagName, $value->nodeValue ?: ''); |
| 34 | + } else { |
| 35 | + $this->value = $value; |
| 36 | + } |
| 37 | + |
| 38 | + if (null !== $this->value) { |
| 39 | + foreach ($attributes as $name => $attribute) { |
| 40 | + if (!is_scalar($attribute)) { |
| 41 | + throw InvalidArgumentException::because('Attribute name must be a scalar, given: ' . \gettype($attribute)); |
| 42 | + } |
| 43 | + |
| 44 | + $this->value->setAttribute((string) $name, (string) $attribute); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + $this->type = type_xml_element($this->value === null); |
| 49 | + } |
| 50 | + |
| 51 | + public function __serialize() : array |
| 52 | + { |
| 53 | + return [ |
| 54 | + 'name' => $this->name, |
| 55 | + 'value' => $this->value === null ? null : \base64_encode(\gzcompress($this->toString()) ?: ''), |
| 56 | + 'type' => $this->type, |
| 57 | + ]; |
| 58 | + } |
| 59 | + |
| 60 | + public function __toString() : string |
| 61 | + { |
| 62 | + if ($this->value === null) { |
| 63 | + return ''; |
| 64 | + } |
| 65 | + |
| 66 | + /* @phpstan-ignore-next-line */ |
| 67 | + return $this->value->ownerDocument->saveXML($this->value); |
| 68 | + } |
| 69 | + |
| 70 | + public function __unserialize(array $data) : void |
| 71 | + { |
| 72 | + $this->name = $data['name']; |
| 73 | + $this->type = $data['type']; |
| 74 | + |
| 75 | + if ($data['value'] === null) { |
| 76 | + $this->value = null; |
| 77 | + |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + $element = \gzuncompress(\base64_decode($data['value'], true) ?: '') ?: ''; |
| 82 | + |
| 83 | + $domDocument = new \DOMDocument(); |
| 84 | + @$domDocument->loadXML($element); |
| 85 | + |
| 86 | + /* @phpstan-ignore-next-line */ |
| 87 | + $this->value = (new \DOMDocument())->createElement($domDocument->documentElement->tagName, $domDocument->documentElement->nodeValue); |
| 88 | + } |
| 89 | + |
| 90 | + public function definition() : Definition |
| 91 | + { |
| 92 | + return Definition::xml_element($this->ref(), $this->type->nullable()); |
| 93 | + } |
| 94 | + |
| 95 | + public function is(Reference|string $name) : bool |
| 96 | + { |
| 97 | + if ($name instanceof Reference) { |
| 98 | + return $this->name === $name->name(); |
| 99 | + } |
| 100 | + |
| 101 | + return $this->name === $name; |
| 102 | + } |
| 103 | + |
| 104 | + public function isEqual(Entry $entry) : bool |
| 105 | + { |
| 106 | + if (!$entry instanceof self || !$this->is($entry->name())) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + if (!$this->type->isEqual($entry->type)) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + return $this->value?->C14N() === $entry->value?->C14N(); |
| 115 | + } |
| 116 | + |
| 117 | + public function map(callable $mapper) : Entry |
| 118 | + { |
| 119 | + return new self($this->name, $mapper($this->value())); |
| 120 | + } |
| 121 | + |
| 122 | + public function name() : string |
| 123 | + { |
| 124 | + return $this->name; |
| 125 | + } |
| 126 | + |
| 127 | + public function rename(string $name) : Entry |
| 128 | + { |
| 129 | + return new self($name, $this->value); |
| 130 | + } |
| 131 | + |
| 132 | + public function toString() : string |
| 133 | + { |
| 134 | + if ($this->value === null) { |
| 135 | + return ''; |
| 136 | + } |
| 137 | + |
| 138 | + /* @phpstan-ignore-next-line */ |
| 139 | + return $this->value->ownerDocument->saveXML($this->value); |
| 140 | + } |
| 141 | + |
| 142 | + public function type() : Type |
| 143 | + { |
| 144 | + return $this->type; |
| 145 | + } |
| 146 | + |
| 147 | + public function value() : ?\DOMElement |
| 148 | + { |
| 149 | + return $this->value; |
| 150 | + } |
| 151 | +} |
0 commit comments