Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/core/etl/src/Flow/ETL/Hash/NativePHPHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,31 @@
use function in_array;
use function sprintf;

final readonly class NativePHPHash implements Algorithm
final class NativePHPHash implements Algorithm
{
private static ?self $instance = null;
private static ?array $algorithms = null;

/**
* @param array<array-key, mixed> $options
*/
public function __construct(
private string $algorithm = 'xxh128',
private bool $binary = false,
private array $options = [],
private readonly string $algorithm = 'xxh128',
private readonly bool $binary = false,
private readonly array $options = [],
) {
if (!in_array($algorithm, hash_algos(), true)) {
self::$algorithms ??= hash_algos();

if (!in_array($algorithm, self::$algorithms, true)) {
throw new InvalidArgumentException(sprintf('Hashing algorithm "%s" is not supported', $algorithm));
}
}

public static function xxh128(string $string): string
{
return (new self('xxh128'))->hash($string);
self::$instance ??= new self('xxh128');

return self::$instance->hash($string);
}

public function hash(string $value): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Flow\ETL\Hash\NativePHPHash;
use Flow\ETL\Tests\FlowTestCase;
use InvalidArgumentException;

final class NativePHPHashTest extends FlowTestCase
{
Expand All @@ -26,4 +27,11 @@ public function test_support_sha512_hash(): void
(new NativePHPHash('sha512'))->hash('test'),
);
}

public function test_hashing_unknown_algorithm(): void
{
static::expectException(InvalidArgumentException::class);

new NativePHPHash('unknown');
}
}
Loading