Skip to content

Commit

Permalink
Simplify casting entries to strings (#582)
Browse files Browse the repository at this point in the history
* Simplify casting entries to strings

* Update src/core/etl/src/Flow/ETL/Row/Reference/Expression/Cast.php

Co-authored-by: Joseph Bielawski <[email protected]>

---------

Co-authored-by: Joseph Bielawski <[email protected]>
  • Loading branch information
norberttech and stloyd authored Oct 13, 2023
1 parent c14e250 commit 90fab6a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/core/etl/src/Flow/ETL/Row/Entry/Type/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\Exception\RuntimeException;

final class Uuid
final class Uuid implements \Stringable
{
/**
* This regexp is a port of the Uuid library,
Expand Down Expand Up @@ -44,6 +44,11 @@ public static function fromString(string $value) : self
return new self($value);
}

public function __toString() : string
{
return $this->toString();
}

public function isEqual(self $type) : bool
{
return $this->toString() === $type->toString();
Expand Down
40 changes: 35 additions & 5 deletions src/core/etl/src/Flow/ETL/Row/Reference/Expression/Cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ public function eval(Row $row) : mixed
'int', 'integer' => (int) $value,
/** @phpstan-ignore-next-line */
'float', 'double', 'real' => (float) $value,
'string' => match (\gettype($value)) {
'object', 'array' => \json_encode($value, JSON_THROW_ON_ERROR),
/** @phpstan-ignore-next-line */
default => (string) $value
},
'string' => $this->toString($value),
'bool', 'boolean' => (bool) $value,
'array' => $this->toArray($value),
'object' => (object) $value,
Expand All @@ -68,6 +64,40 @@ private function toArray(mixed $data) : array
return (array) $data;
}

private function toString(mixed $value) : ?string
{
if ($value === null) {
return null;
}

if (\is_string($value)) {
return $value;
}

if (\is_bool($value)) {
return $value ? 'true' : 'false';
}

if (\is_array($value)) {
return \json_encode($value, JSON_THROW_ON_ERROR);
}

if ($value instanceof \DateTimeInterface) {
return $value->format(\DateTimeInterface::RFC3339);
}

if ($value instanceof \Stringable) {
return (string) $value;
}

if ($value instanceof \DOMDocument) {
return $value->saveXML() ?: null;
}

/** @phpstan-ignore-next-line */
return (string) $value;
}

private function toXML(mixed $value) : null|\DOMDocument
{
if (\is_string($value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use function Flow\ETL\DSL\cast;
use function Flow\ETL\DSL\ref;
use Flow\ETL\Row;
use Flow\ETL\Row\Entry\Type\Uuid;
use Flow\ETL\Row\Factory\NativeEntryFactory;
use PHPUnit\Framework\TestCase;

Expand All @@ -17,6 +18,12 @@ public static function cast_provider() : array
$xml = new \DOMDocument();
$xml->loadXML($xmlString = '<root><foo baz="buz">bar</foo></root>');

$fullXMLString = <<<'XML'
<?xml version="1.0"?>
<root><foo baz="buz">bar</foo></root>

XML;

return [
'invalid' => [null, 'int', null],
'int' => ['1', 'int', 1],
Expand All @@ -34,6 +41,10 @@ public static function cast_provider() : array
'json_pretty' => [[1], 'json_pretty', "[\n 1\n]"],
'xml_to_array' => [$xml, 'array', ['root' => ['foo' => ['@attributes' => ['baz' => 'buz'], '@value' => 'bar']]]],
'string_to_xml' => [$xmlString, 'xml', $xml],
'xml_to_string' => [$xml, 'string', $fullXMLString],
'datetime' => [new \DateTimeImmutable('2023-01-01 00:00:00 UTC'), 'string', '2023-01-01T00:00:00+00:00'],
'uuid' => [Uuid::fromString('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'), 'string', 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'],
'bool_to_string' => [true, 'string', 'true'],
];
}

Expand Down

0 comments on commit 90fab6a

Please sign in to comment.