From fa151ad11688a3bfc663af9eec0e1ade1d58fd45 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Tue, 7 Jan 2025 10:17:32 +0100 Subject: [PATCH] add optional fixed replacement length --- CHANGELOG.md | 5 ++++- src/sensitiveMessageFormatter.php | 13 ++++++++++--- tests/sensitiveMessageFormatterTest.php | 8 +++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f8b793..ada3d85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased](https://git.d3data.de/D3Public/sensitiveMessageFormatter/compare/1.0.1...rel_1.x) +## [Unreleased](https://git.d3data.de/D3Public/sensitiveMessageFormatter/compare/1.1.0...rel_1.x) +## [1.1.0](https://git.d3data.de/D3Public/sensitiveMessageFormatter/compare/1.0.1...1.1.0) - 2025-01-07 +### Added +- optional fixed length replacement ## [1.0.1](https://git.d3data.de/D3Public/sensitiveMessageFormatter/compare/1.0.0...1.0.1) - 2025-01-01 ### Changed diff --git a/src/sensitiveMessageFormatter.php b/src/sensitiveMessageFormatter.php index e779309..c46660c 100644 --- a/src/sensitiveMessageFormatter.php +++ b/src/sensitiveMessageFormatter.php @@ -32,7 +32,8 @@ class sensitiveMessageFormatter extends MessageFormatter public function __construct( ?string $template = self::CLF, protected array $anonymizations = [], - protected ?string $replaceChar = null + protected ?string $replaceChar = null, + protected ?int $fixedReplacementLength = null ) { $this->createReplacements($this->anonymizations); @@ -51,8 +52,14 @@ class sensitiveMessageFormatter extends MessageFormatter array_map( function ($search) use (&$replacements) { - $replacements[$search] = str_repeat($this->replaceChar ?? '*', strlen($search)); - $replacements[urlencode($search)] = str_repeat($this->replaceChar ?? '*', strlen($search)); + $replacements[$search] = str_repeat( + $this->replaceChar ?? '*', + $this->fixedReplacementLength ?? strlen($search) + ); + $replacements[urlencode($search)] = str_repeat( + $this->replaceChar ?? '*', + $this->fixedReplacementLength ?? strlen($search) + ); }, $search ); diff --git a/tests/sensitiveMessageFormatterTest.php b/tests/sensitiveMessageFormatterTest.php index d5ad5f6..c2fa6e6 100644 --- a/tests/sensitiveMessageFormatterTest.php +++ b/tests/sensitiveMessageFormatterTest.php @@ -69,7 +69,12 @@ class sensitiveMessageFormatterTest extends ApiTestCase * @dataProvider createReplacementsDataProvider * @covers \D3\SensitiveMessageFormatter\sensitiveMessageFormatter::createReplacements */ - public function testCreateReplacements(array $input, array $expected, ?string $replacement = null): void + public function testCreateReplacements( + array $input, + array $expected, + ?string $replacement = null, + ?int $fixedReplacementLenth = null + ): void { if ($replacement) { $this->setValue( @@ -100,6 +105,7 @@ class sensitiveMessageFormatterTest extends ApiTestCase yield 'multiple' => [['def', 'def'], ['def' => '***']]; yield 'urlencoded' => [['1&c'], ['1&c' => '***', '1%26c' => '***' ]]; yield 'different replace char' => [['abcd'], ['abcd' => '####'], '#']; + yield 'fixed replacement lenght' => [['abcd'], ['abcd' => '*******'], '*', 7]; } /**