From 705e3c859a97c0961bf3dc582562ddc6f11e771e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20=C5=81api=C5=84ski?= Date: Sun, 2 Mar 2025 01:29:20 +0100 Subject: [PATCH] Add StringBeforeLast function Fix variable name in two tests --- .../Flow/ETL/Function/ScalarFunctionChain.php | 8 ++ .../Flow/ETL/Function/StringBeforeLast.php | 40 ++++++++++ .../Unit/Function/StringAfterLastTest.php | 4 +- .../Unit/Function/StringBeforeLastTest.php | 75 +++++++++++++++++++ .../Tests/Unit/Function/StringBeforeTest.php | 4 +- 5 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 src/core/etl/src/Flow/ETL/Function/StringBeforeLast.php create mode 100644 src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringBeforeLastTest.php diff --git a/src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php b/src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php index 8d0acf558..65d0e5207 100644 --- a/src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php +++ b/src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php @@ -512,6 +512,14 @@ public function stringBefore(ScalarFunction|string $needle, ScalarFunction|bool return new StringBefore($this, $needle, $includeNeedle); } + /** + * Returns the contents found before the last occurrence of the given string. + */ + public function stringBeforeLast(ScalarFunction|string $needle, ScalarFunction|bool $includeNeedle = false) : self + { + return new StringBeforeLast($this, $needle, $includeNeedle); + } + /** * Returns a string that you can use in case-insensitive comparisons. */ diff --git a/src/core/etl/src/Flow/ETL/Function/StringBeforeLast.php b/src/core/etl/src/Flow/ETL/Function/StringBeforeLast.php new file mode 100644 index 000000000..281b39338 --- /dev/null +++ b/src/core/etl/src/Flow/ETL/Function/StringBeforeLast.php @@ -0,0 +1,40 @@ +string))->asString($row); + + if ($string === null) { + return null; + } + + $needle = (new Parameter($this->needle))->as($row, type_string(), type_list(type_string())); + $includeNeedle = (new Parameter($this->includeNeedle))->asBoolean($row); + + return u($string)->beforeLast($needle, $includeNeedle)->toString(); + } + + public function returns() : Type + { + return type_string(); + } +} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringAfterLastTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringAfterLastTest.php index 5b5d0d3aa..e29c54357 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringAfterLastTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringAfterLastTest.php @@ -14,8 +14,8 @@ final class StringAfterLastTest extends FlowTestCase { public function test_returns_method_returns_string_type() : void { - $stringAfterFunction = new StringAfterLast('test', 'e'); - $returnType = $stringAfterFunction->returns(); + $stringAfterLastFunction = new StringAfterLast('test', 'e'); + $returnType = $stringAfterLastFunction->returns(); self::assertInstanceOf(Type::class, $returnType); diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringBeforeLastTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringBeforeLastTest.php new file mode 100644 index 000000000..3acac801f --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringBeforeLastTest.php @@ -0,0 +1,75 @@ +returns(); + + self::assertInstanceOf(Type::class, $returnType); + + self::assertTrue($returnType->isEqual(type_string())); + } + + public function test_string_before_last() : void + { + self::assertSame( + 'hello w', + ref('str')->stringBeforeLast(ref('needle'))->eval( + row( + str_entry('str', 'hello world'), + str_entry('needle', 'o') + ) + ) + ); + } + + public function test_string_before_last_including_needle() : void + { + self::assertSame( + 'hello wo', + ref('str')->stringBeforeLast(ref('needle'), includeNeedle: true)->eval( + row( + str_entry('str', 'hello world'), + str_entry('needle', 'o') + ) + ) + ); + } + + public function test_string_before_last_returns_empty_string() : void + { + self::assertSame( + '', + ref('str')->stringBeforeLast(ref('needle'))->eval( + row( + str_entry('str', ''), + str_entry('needle', 'o') + ) + ) + ); + } + + public function test_string_before_last_returns_null() : void + { + self::assertNull( + ref('str')->stringBeforeLast(ref('needle'))->eval( + row( + str_entry('str', null), + str_entry('needle', 'o') + ) + ) + ); + } +} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringBeforeTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringBeforeTest.php index 0ba7d5194..b95eb8b39 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringBeforeTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringBeforeTest.php @@ -14,8 +14,8 @@ final class StringBeforeTest extends FlowTestCase { public function test_returns_method_returns_string_type() : void { - $stringTitleFunction = new StringBefore('str', 't', false); - $returnType = $stringTitleFunction->returns(); + $stringBeforeFunction = new StringBefore('str', 't', false); + $returnType = $stringBeforeFunction->returns(); self::assertInstanceOf(Type::class, $returnType);