Skip to content

Commit

Permalink
Add ByteString IsUTF8 (#1470)
Browse files Browse the repository at this point in the history
* Add ByteString IsUTF8

* Add Test for Return Type of IsUtf8()
  • Loading branch information
f-lapinski authored Feb 12, 2025
1 parent e720fc5 commit 74c2a81
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/core/etl/src/Flow/ETL/Function/IsUtf8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Function;

use function Flow\ETL\DSL\{type_boolean};
use function Symfony\Component\String\{b};
use Flow\ETL\Function\ScalarFunction\TypedScalarFunction;
use Flow\ETL\PHP\Type\Type;
use Flow\ETL\Row;

final class IsUtf8 extends ScalarFunctionChain implements TypedScalarFunction
{
public function __construct(private readonly ScalarFunction|string $string)
{
}

public function eval(Row $row) : mixed
{
$string = (new Parameter($this->string))->asString($row);

if ($string === null) {
return null;
}

return b($string)->isUtf8();
}

public function returns() : Type
{
return type_boolean();
}
}
5 changes: 5 additions & 0 deletions src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ public function isType(string|Type ...$types) : self
return new IsType($this, ...$types);
}

public function isUtf8() : IsUtf8
{
return new IsUtf8($this);
}

public function jsonDecode(ScalarFunction|int $flags = JSON_THROW_ON_ERROR) : self
{
return new JsonDecode($this, $flags);
Expand Down
39 changes: 39 additions & 0 deletions src/core/etl/tests/Flow/ETL/Tests/Unit/Function/IsUtf8Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Tests\Unit\Function;

use function Flow\ETL\DSL\row;
use function Flow\ETL\DSL\{ref, str_entry, type_boolean};
use Flow\ETL\Function\IsUtf8;
use Flow\ETL\PHP\Type\Type;
use Flow\ETL\Tests\FlowTestCase;

final class IsUtf8Test extends FlowTestCase
{
public function test_is_utf_8() : void
{
self::assertTrue(
ref('str')->isUtf8()->eval(
row(str_entry('str', 'Lorem Ipsum'))
)
);

self::assertFalse(
ref('str')->isUtf8()->eval(
row(str_entry('str', "\xc3\x28"))
)
);
}

public function test_returns_method_returns_string_boolean() : void
{
$isUtf8Function = new IsUtf8('Lorem Ipsum');
$returnType = $isUtf8Function->returns();

self::assertInstanceOf(Type::class, $returnType);

self::assertTrue($returnType->isEqual(type_boolean()));
}
}

0 comments on commit 74c2a81

Please sign in to comment.