-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new XMLElementEntry as replacement for XMLNodeEntry
- Loading branch information
Showing
10 changed files
with
320 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/core/etl/src/Flow/ETL/PHP/Type/Logical/XMLElementType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\PHP\Type\Logical; | ||
|
||
use Flow\ETL\Exception\InvalidArgumentException; | ||
use Flow\ETL\PHP\Type\Native\NullType; | ||
use Flow\ETL\PHP\Type\Type; | ||
|
||
final class XMLElementType implements LogicalType | ||
{ | ||
public function __construct(private readonly bool $nullable) | ||
{ | ||
} | ||
|
||
public static function fromArray(array $data) : self | ||
{ | ||
return new self($data['nullable'] ?? false); | ||
} | ||
|
||
public function isEqual(Type $type) : bool | ||
{ | ||
return $type instanceof self; | ||
} | ||
|
||
public function isValid(mixed $value) : bool | ||
{ | ||
if ($this->nullable && $value === null) { | ||
return true; | ||
} | ||
|
||
if ($value instanceof \DOMElement) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function makeNullable(bool $nullable) : self | ||
{ | ||
return new self($nullable); | ||
} | ||
|
||
public function merge(Type $type) : self | ||
{ | ||
if ($type instanceof NullType) { | ||
return $this->makeNullable(true); | ||
} | ||
|
||
if (!$type instanceof self) { | ||
throw new InvalidArgumentException('Cannot merge different types, ' . $this->toString() . ' and ' . $type->toString()); | ||
} | ||
|
||
return new self($this->nullable || $type->nullable()); | ||
} | ||
|
||
public function normalize() : array | ||
{ | ||
return [ | ||
'type' => 'xml_element', | ||
'nullable' => $this->nullable, | ||
]; | ||
} | ||
|
||
public function nullable() : bool | ||
{ | ||
return $this->nullable; | ||
} | ||
|
||
public function toString() : string | ||
{ | ||
return ($this->nullable ? '?' : '') . 'xml_element'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
src/core/etl/src/Flow/ETL/Row/Entry/XMLElementEntry.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Row\Entry; | ||
|
||
use function Flow\ETL\DSL\type_xml_element; | ||
use Flow\ETL\Exception\InvalidArgumentException; | ||
use Flow\ETL\PHP\Type\Logical\XMLElementType; | ||
use Flow\ETL\PHP\Type\Type; | ||
use Flow\ETL\Row\Schema\Definition; | ||
use Flow\ETL\Row\{Entry, Reference}; | ||
|
||
/** | ||
* @implements Entry<?\DOMElement> | ||
*/ | ||
final class XMLElementEntry implements Entry | ||
{ | ||
use EntryRef; | ||
|
||
private readonly XMLElementType $type; | ||
|
||
private readonly ?\DOMElement $value; | ||
|
||
/** | ||
* @param array<int|string, mixed> $attributes | ||
*/ | ||
public function __construct(private readonly string $name, \DOMElement|string|null $value, array $attributes = []) | ||
{ | ||
if (\is_string($value)) { | ||
$this->value = (new \DOMDocument())->createElement($value); | ||
} elseif ($value instanceof \DOMElement) { | ||
$this->value = (new \DOMDocument())->createElement($value->tagName, $value->nodeValue ?: ''); | ||
} else { | ||
$this->value = $value; | ||
} | ||
|
||
if (null !== $this->value) { | ||
foreach ($attributes as $name => $attribute) { | ||
if (!is_scalar($attribute)) { | ||
throw InvalidArgumentException::because('Attribute name must be a scalar, given: ' . \gettype($attribute)); | ||
} | ||
|
||
$this->value->setAttribute((string) $name, (string) $attribute); | ||
} | ||
} | ||
|
||
$this->type = type_xml_element($this->value === null); | ||
} | ||
|
||
public function __serialize() : array | ||
{ | ||
return [ | ||
'name' => $this->name, | ||
'value' => $this->value === null ? null : \base64_encode(\gzcompress($this->toString()) ?: ''), | ||
'type' => $this->type, | ||
]; | ||
} | ||
|
||
public function __toString() : string | ||
{ | ||
if ($this->value === null) { | ||
return ''; | ||
} | ||
|
||
/* @phpstan-ignore-next-line */ | ||
return $this->value->ownerDocument->saveXML($this->value); | ||
} | ||
|
||
public function __unserialize(array $data) : void | ||
{ | ||
$this->name = $data['name']; | ||
$this->type = $data['type']; | ||
|
||
if ($data['value'] === null) { | ||
$this->value = null; | ||
|
||
return; | ||
} | ||
|
||
$element = \gzuncompress(\base64_decode($data['value'], true) ?: '') ?: ''; | ||
|
||
$domDocument = new \DOMDocument(); | ||
@$domDocument->loadXML($element); | ||
|
||
/* @phpstan-ignore-next-line */ | ||
$this->value = (new \DOMDocument())->createElement($domDocument->documentElement->tagName, $domDocument->documentElement->nodeValue); | ||
} | ||
|
||
public function definition() : Definition | ||
{ | ||
return Definition::xml_element($this->ref(), $this->type->nullable()); | ||
} | ||
|
||
public function is(Reference|string $name) : bool | ||
{ | ||
if ($name instanceof Reference) { | ||
return $this->name === $name->name(); | ||
} | ||
|
||
return $this->name === $name; | ||
} | ||
|
||
public function isEqual(Entry $entry) : bool | ||
{ | ||
if (!$entry instanceof self || !$this->is($entry->name())) { | ||
return false; | ||
} | ||
|
||
if (!$this->type->isEqual($entry->type)) { | ||
return false; | ||
} | ||
|
||
return $this->value?->C14N() === $entry->value?->C14N(); | ||
} | ||
|
||
public function map(callable $mapper) : Entry | ||
{ | ||
return new self($this->name, $mapper($this->value())); | ||
} | ||
|
||
public function name() : string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function rename(string $name) : Entry | ||
{ | ||
return new self($name, $this->value); | ||
} | ||
|
||
public function toString() : string | ||
{ | ||
if ($this->value === null) { | ||
return ''; | ||
} | ||
|
||
/* @phpstan-ignore-next-line */ | ||
return $this->value->ownerDocument->saveXML($this->value); | ||
} | ||
|
||
public function type() : Type | ||
{ | ||
return $this->type; | ||
} | ||
|
||
public function value() : ?\DOMElement | ||
{ | ||
return $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.