Skip to content

Commit

Permalink
Integration/symfony indexof (#1473)
Browse files Browse the repository at this point in the history
* Add IndexOf Function with Test

* Fix $needle type and add offset

* Add IgnoreCase option as parameter
  • Loading branch information
f-lapinski authored Feb 12, 2025
1 parent dfde1cc commit 86a2b44
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/core/etl/src/Flow/ETL/Function/IndexOf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Function;

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

final class IndexOf extends ScalarFunctionChain implements TypedScalarFunction
{
public function __construct(
private readonly ScalarFunction|string $string,
private readonly ScalarFunction|string $needle,
private readonly ScalarFunction|bool $ignoreCase = false,
private readonly ScalarFunction|int $offset = 0,
) {
}

public function eval(Row $row) : int|false|null
{
$string = (new Parameter($this->string))->asString($row);
$needle = (new Parameter($this->needle))->asString($row);
$offset = (new Parameter($this->offset))->as($row, type_int());
$ignoreCase = (new Parameter($this->ignoreCase))->asBoolean($row);

if ($string === null || $needle === null) {
return false;
}

if ($ignoreCase) {
return u($string)->ignoreCase()->indexOf($needle, $offset);
}

return u($string)->indexOf($needle, $offset);
}

public function returns() : Type
{
return type_int();
}
}
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 @@ -259,6 +259,11 @@ public function hash(Algorithm $algorithm = new NativePHPHash()) : self
return new Hash($this, $algorithm);
}

public function indexOf(ScalarFunction|string $needle, ScalarFunction|bool $ignoreCase = false, ScalarFunction|int $offset = 0) : self
{
return new IndexOf($this, $needle, $ignoreCase, $offset);
}

public function isEven() : self
{
return new Equals(new Mod($this, lit(2)), lit(0));
Expand Down
54 changes: 54 additions & 0 deletions src/core/etl/tests/Flow/ETL/Tests/Unit/Function/IndexOfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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_int};
use Flow\ETL\Function\IndexOf;
use Flow\ETL\PHP\Type\Type;
use Flow\ETL\Tests\FlowTestCase;

final class IndexOfTest extends FlowTestCase
{
public function test_index_of() : void
{
self::assertSame(
5,
ref('str')->indexOf('x', offset: 5)->eval(
row(str_entry('str', 'AbBAsxa'))
)
);

self::assertSame(
0,
ref('str')->indexOf('A', ignoreCase: true)->eval(
row(str_entry('str', 'abbbbb'))
)
);

self::assertSame(
5,
ref('str')->indexOf('x', offset: 5)->eval(
row(str_entry('str', 'AbBAsxa'))
)
);

self::assertNull(
ref('str')->indexOf('x', offset: 2)->eval(
row(str_entry('str', 'Abba'))
)
);
}

public function test_returns_method_returns_string_int() : void
{
$indexOf = new IndexOf('Abba', 'b', offset: 3);
$returnType = $indexOf->returns();

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

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

0 comments on commit 86a2b44

Please sign in to comment.