add optional fixed replacement length

This commit is contained in:
Daniel Seifert 2025-01-07 10:17:32 +01:00
parent 78963a6065
commit fa151ad116
3 changed files with 21 additions and 5 deletions

View File

@ -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/), 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). 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 ## [1.0.1](https://git.d3data.de/D3Public/sensitiveMessageFormatter/compare/1.0.0...1.0.1) - 2025-01-01
### Changed ### Changed

View File

@ -32,7 +32,8 @@ class sensitiveMessageFormatter extends MessageFormatter
public function __construct( public function __construct(
?string $template = self::CLF, ?string $template = self::CLF,
protected array $anonymizations = [], protected array $anonymizations = [],
protected ?string $replaceChar = null protected ?string $replaceChar = null,
protected ?int $fixedReplacementLength = null
) { ) {
$this->createReplacements($this->anonymizations); $this->createReplacements($this->anonymizations);
@ -51,8 +52,14 @@ class sensitiveMessageFormatter extends MessageFormatter
array_map( array_map(
function ($search) use (&$replacements) { function ($search) use (&$replacements) {
$replacements[$search] = str_repeat($this->replaceChar ?? '*', strlen($search)); $replacements[$search] = str_repeat(
$replacements[urlencode($search)] = str_repeat($this->replaceChar ?? '*', strlen($search)); $this->replaceChar ?? '*',
$this->fixedReplacementLength ?? strlen($search)
);
$replacements[urlencode($search)] = str_repeat(
$this->replaceChar ?? '*',
$this->fixedReplacementLength ?? strlen($search)
);
}, },
$search $search
); );

View File

@ -69,7 +69,12 @@ class sensitiveMessageFormatterTest extends ApiTestCase
* @dataProvider createReplacementsDataProvider * @dataProvider createReplacementsDataProvider
* @covers \D3\SensitiveMessageFormatter\sensitiveMessageFormatter::createReplacements * @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) { if ($replacement) {
$this->setValue( $this->setValue(
@ -100,6 +105,7 @@ class sensitiveMessageFormatterTest extends ApiTestCase
yield 'multiple' => [['def', 'def'], ['def' => '***']]; yield 'multiple' => [['def', 'def'], ['def' => '***']];
yield 'urlencoded' => [['1&c'], ['1&c' => '***', '1%26c' => '***' ]]; yield 'urlencoded' => [['1&c'], ['1&c' => '***', '1%26c' => '***' ]];
yield 'different replace char' => [['abcd'], ['abcd' => '####'], '#']; yield 'different replace char' => [['abcd'], ['abcd' => '####'], '#'];
yield 'fixed replacement lenght' => [['abcd'], ['abcd' => '*******'], '*', 7];
} }
/** /**