From 9c2f16ab1bd1cb788b7effbae5800ce1fd9d7224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Mleczko?= Date: Sat, 20 Jul 2024 14:24:16 +0200 Subject: [PATCH] Add static helper function for hashing --- src/core/etl/src/Flow/ETL/Function/Hash.php | 5 ++- .../etl/src/Flow/ETL/Hash/NativePHPHash.php | 5 +++ .../Flow/ETL/Tests/Unit/Function/HashTest.php | 2 +- .../ETL/Tests/Unit/Hash/NativePHPHashTest.php | 36 +++++++++++++++++++ 4 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 src/core/etl/tests/Flow/ETL/Tests/Unit/Hash/NativePHPHashTest.php diff --git a/src/core/etl/src/Flow/ETL/Function/Hash.php b/src/core/etl/src/Flow/ETL/Function/Hash.php index 2d43b0bf1..530d0fb1f 100644 --- a/src/core/etl/src/Flow/ETL/Function/Hash.php +++ b/src/core/etl/src/Flow/ETL/Function/Hash.php @@ -4,16 +4,15 @@ namespace Flow\ETL\Function; -use Flow\ETL\Hash\Algorithm; +use Flow\ETL\Hash\{Algorithm, NativePHPHash}; use Flow\ETL\Row; final class Hash extends ScalarFunctionChain { public function __construct( private readonly ScalarFunction $ref, - private readonly Algorithm $algorithm, + private readonly Algorithm $algorithm = new NativePHPHash(), ) { - } public function eval(Row $row) : ?string diff --git a/src/core/etl/src/Flow/ETL/Hash/NativePHPHash.php b/src/core/etl/src/Flow/ETL/Hash/NativePHPHash.php index 7e7ee2947..646368912 100644 --- a/src/core/etl/src/Flow/ETL/Hash/NativePHPHash.php +++ b/src/core/etl/src/Flow/ETL/Hash/NativePHPHash.php @@ -13,6 +13,11 @@ public function __construct(private string $algorithm = 'xxh128', private bool $ } } + public static function xxh128(string $string) : string + { + return (new self('xxh128'))->hash($string); + } + public function hash(string $value) : string { return \hash($this->algorithm, $value, $this->binary, $this->options); diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/HashTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/HashTest.php index e8d6786a2..44ac9a4d1 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/HashTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/HashTest.php @@ -22,7 +22,7 @@ public function test_hashing_array_value() : void public function test_hashing_concat() : void { self::assertSame( - (new NativePHPHash('xxh128'))->hash('test_test'), + NativePHPHash::xxh128('test_test'), hash(concat(ref('value'), lit('_'), ref('value')), new NativePHPHash('xxh128'))->eval(Row::create(str_entry('value', 'test'))) ); } diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Hash/NativePHPHashTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Hash/NativePHPHashTest.php new file mode 100644 index 000000000..fa9bd11e8 --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Hash/NativePHPHashTest.php @@ -0,0 +1,36 @@ +hash('test') + ); + + } +}