-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add IndexOf Function with Test * Fix $needle type and add offset * Add IgnoreCase option as parameter
- Loading branch information
1 parent
dfde1cc
commit 86a2b44
Showing
3 changed files
with
104 additions
and
0 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
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(); | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
src/core/etl/tests/Flow/ETL/Tests/Unit/Function/IndexOfTest.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,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())); | ||
} | ||
} |