diff --git a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/ArrayCastingHandler.php b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/ArrayCastingHandler.php index 8046cac08..7ac885ced 100644 --- a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/ArrayCastingHandler.php +++ b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/ArrayCastingHandler.php @@ -20,14 +20,14 @@ public function supports(Type $type) : bool public function value(mixed $value, Type $type, Caster $caster) : mixed { try { - if (\is_string($value) && (\str_starts_with($value, '{') || \str_starts_with($value, '['))) { - return \json_decode($value, true, 512, \JSON_THROW_ON_ERROR); - } - if (\is_array($value)) { return $value; } + if (\is_string($value) && (\str_starts_with($value, '{') || \str_starts_with($value, '['))) { + return \json_decode($value, true, 512, \JSON_THROW_ON_ERROR); + } + if ($value instanceof \DOMDocument) { return (new XMLConverter())->toArray($value); } diff --git a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/BooleanCastingHandler.php b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/BooleanCastingHandler.php index 8517fa010..3af29cbfe 100644 --- a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/BooleanCastingHandler.php +++ b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/BooleanCastingHandler.php @@ -18,6 +18,10 @@ public function supports(Type $type) : bool public function value(mixed $value, Type $type, Caster $caster) : mixed { + if (\is_bool($value)) { + return $value; + } + if (\is_string($value)) { if (\in_array(\mb_strtolower($value), ['true', '1', 'yes', 'on'], true)) { return true; diff --git a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/EnumCastingHandler.php b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/EnumCastingHandler.php index 01164dd47..44cc721dc 100644 --- a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/EnumCastingHandler.php +++ b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/EnumCastingHandler.php @@ -18,6 +18,11 @@ public function supports(Type $type) : bool public function value(mixed $value, Type $type, Caster $caster) : mixed { + /** @var EnumType $type */ + if ($value instanceof $type->class) { + return $value; + } + try { /** @var EnumType $type */ $enumClass = $type->class; diff --git a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/FloatCastingHandler.php b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/FloatCastingHandler.php index c3a2fbebd..48edae6a1 100644 --- a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/FloatCastingHandler.php +++ b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/FloatCastingHandler.php @@ -18,6 +18,10 @@ public function supports(Type $type) : bool public function value(mixed $value, Type $type, Caster $caster) : mixed { + if (\is_float($value)) { + return $value; + } + if ($value instanceof \DateTimeImmutable) { return (float) $value->format('Uu'); } diff --git a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/IntegerCastingHandler.php b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/IntegerCastingHandler.php index bec3778aa..59e79ac36 100644 --- a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/IntegerCastingHandler.php +++ b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/IntegerCastingHandler.php @@ -18,6 +18,10 @@ public function supports(Type $type) : bool public function value(mixed $value, Type $type, Caster $caster) : mixed { + if (\is_int($value)) { + return $value; + } + if ($value instanceof \DateTimeImmutable) { return (int) $value->format('Uu'); } diff --git a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/ObjectCastingHandler.php b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/ObjectCastingHandler.php index bc75439dd..57d0c3490 100644 --- a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/ObjectCastingHandler.php +++ b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/ObjectCastingHandler.php @@ -19,6 +19,10 @@ public function supports(Type $type) : bool public function value(mixed $value, Type $type, Caster $caster) : mixed { + if (\is_object($value)) { + return $value; + } + /** @var ObjectType $type */ try { $object = (object) $value; diff --git a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/StringCastingHandler.php b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/StringCastingHandler.php index 2b16ee7f5..190017b39 100644 --- a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/StringCastingHandler.php +++ b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/StringCastingHandler.php @@ -18,10 +18,6 @@ public function supports(Type $type) : bool public function value(mixed $value, Type $type, Caster $caster) : mixed { - if ($value === null) { - return null; - } - if (\is_string($value)) { return $value; } diff --git a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/StructureCastingHandler.php b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/StructureCastingHandler.php index eb08140f9..0b4c69d1c 100644 --- a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/StructureCastingHandler.php +++ b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/StructureCastingHandler.php @@ -18,10 +18,6 @@ public function supports(Type $type) : bool public function value(mixed $value, Type $type, Caster $caster) : mixed { - if ($value === null && !$type->nullable()) { - throw new CastingException($value, $type); - } - /** @var StructureType $type */ try { if (\is_string($value) && (\str_starts_with($value, '{') || \str_starts_with($value, '['))) { diff --git a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/UuidCastingHandler.php b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/UuidCastingHandler.php index 853f035c5..32fb9c635 100644 --- a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/UuidCastingHandler.php +++ b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/UuidCastingHandler.php @@ -19,6 +19,10 @@ public function supports(Type $type) : bool public function value(mixed $value, Type $type, Caster $caster) : mixed { + if ($value instanceof Uuid) { + return $value; + } + if (\is_string($value)) { return new Uuid($value); } diff --git a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/XMLCastingHandler.php b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/XMLCastingHandler.php index b8ca7d3bb..ad2afe3c4 100644 --- a/src/core/etl/src/Flow/ETL/PHP/Type/Caster/XMLCastingHandler.php +++ b/src/core/etl/src/Flow/ETL/PHP/Type/Caster/XMLCastingHandler.php @@ -4,6 +4,7 @@ namespace Flow\ETL\PHP\Type\Caster; +use function Flow\ETL\DSL\type_string; use function Flow\ETL\DSL\type_xml; use Flow\ETL\Exception\CastingException; use Flow\ETL\PHP\Type\Caster; @@ -19,6 +20,10 @@ public function supports(Type $type) : bool public function value(mixed $value, Type $type, Caster $caster) : mixed { + if ($value instanceof \DOMDocument) { + return $value; + } + if (\is_string($value)) { $doc = new \DOMDocument(); @@ -29,10 +34,18 @@ public function value(mixed $value, Type $type, Caster $caster) : mixed return $doc; } - if ($value instanceof \DOMDocument) { - return $value; - } + try { + $stringValue = $caster->to(type_string())->value($value); - throw new CastingException($value, $type); + $doc = new \DOMDocument(); + + if (!@$doc->loadXML($stringValue)) { + throw new CastingException($stringValue, type_xml()); + } + + return $doc; + } catch (CastingException $e) { + throw new CastingException($value, type_xml(), $e); + } } }