|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Flow\ETL\Row\Entry; |
| 6 | + |
| 7 | +use function Flow\Types\DSL\{type_equals, type_html_element, type_instance_of, type_optional}; |
| 8 | +use Dom\{HTMLDocument, HTMLElement}; |
| 9 | +use Flow\ETL\Row\{Entry, Reference}; |
| 10 | +use Flow\ETL\Schema\{Definition, Metadata}; |
| 11 | +use Flow\Types\Type; |
| 12 | + |
| 13 | +/** |
| 14 | + * @implements Entry<?HTMLElement> |
| 15 | + */ |
| 16 | +final class HTMLElementEntry implements Entry |
| 17 | +{ |
| 18 | + use EntryRef; |
| 19 | + |
| 20 | + private Metadata $metadata; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var Type<HTMLElement> |
| 24 | + */ |
| 25 | + private readonly Type $type; |
| 26 | + |
| 27 | + private readonly ?HTMLElement $value; |
| 28 | + |
| 29 | + public function __construct( |
| 30 | + private readonly string $name, |
| 31 | + HTMLElement|string|null $value, |
| 32 | + ?Metadata $metadata = null, |
| 33 | + ) { |
| 34 | + if (\is_string($value)) { |
| 35 | + $document = HTMLDocument::createFromString($value, \LIBXML_HTML_NOIMPLIED | \LIBXML_NOERROR); |
| 36 | + |
| 37 | + $value = $document->documentElement; |
| 38 | + } |
| 39 | + |
| 40 | + $this->metadata = $metadata ?: Metadata::empty(); |
| 41 | + $this->value = $value; |
| 42 | + $this->type = type_html_element(); |
| 43 | + } |
| 44 | + |
| 45 | + public function __toString() : string |
| 46 | + { |
| 47 | + if ($this->value === null) { |
| 48 | + return ''; |
| 49 | + } |
| 50 | + |
| 51 | + return $this->toString(); |
| 52 | + } |
| 53 | + |
| 54 | + public function definition() : Definition |
| 55 | + { |
| 56 | + return new Definition($this->name, $this->type, $this->value === null, $this->metadata); |
| 57 | + } |
| 58 | + |
| 59 | + public function duplicate() : self |
| 60 | + { |
| 61 | + return new self($this->name, type_optional(type_instance_of(HTMLElement::class))->assert($this->value ? $this->value->cloneNode(true) : null), $this->metadata); |
| 62 | + } |
| 63 | + |
| 64 | + public function is(Reference|string $name) : bool |
| 65 | + { |
| 66 | + if ($name instanceof Reference) { |
| 67 | + return $this->name === $name->name(); |
| 68 | + } |
| 69 | + |
| 70 | + return $this->name === $name; |
| 71 | + } |
| 72 | + |
| 73 | + public function isEqual(Entry $entry) : bool |
| 74 | + { |
| 75 | + if (!$entry instanceof self || !$this->is($entry->name())) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + if (!type_equals($this->type, $entry->type)) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + return $this->value?->C14N() === $entry->value?->C14N(); |
| 84 | + } |
| 85 | + |
| 86 | + public function map(callable $mapper) : self |
| 87 | + { |
| 88 | + $mappedValue = $mapper($this->value()); |
| 89 | + $mappedValue = type_optional(type_instance_of(HTMLElement::class))->assert($mappedValue); |
| 90 | + |
| 91 | + return new self($this->name, $mappedValue); |
| 92 | + } |
| 93 | + |
| 94 | + public function name() : string |
| 95 | + { |
| 96 | + return $this->name; |
| 97 | + } |
| 98 | + |
| 99 | + public function rename(string $name) : self |
| 100 | + { |
| 101 | + return new self($name, $this->value); |
| 102 | + } |
| 103 | + |
| 104 | + public function toString() : string |
| 105 | + { |
| 106 | + if ($this->value === null) { |
| 107 | + return ''; |
| 108 | + } |
| 109 | + |
| 110 | + /* @phpstan-ignore-next-line */ |
| 111 | + return $this->value->innerHTML; |
| 112 | + } |
| 113 | + |
| 114 | + public function type() : Type |
| 115 | + { |
| 116 | + return $this->type; |
| 117 | + } |
| 118 | + |
| 119 | + public function value() : ?HTMLElement |
| 120 | + { |
| 121 | + return $this->value; |
| 122 | + } |
| 123 | + |
| 124 | + public function withValue(mixed $value) : self |
| 125 | + { |
| 126 | + return new self($this->name, type_optional($this->type())->assert($value), $this->metadata); |
| 127 | + } |
| 128 | +} |
0 commit comments