diff --git a/docs/book/v3/migration/v2-to-v3.md b/docs/book/v3/migration/v2-to-v3.md index 03f79fb8..66f78a78 100644 --- a/docs/book/v3/migration/v2-to-v3.md +++ b/docs/book/v3/migration/v2-to-v3.md @@ -48,6 +48,18 @@ The impact of the removal of these aliases will not affect you if you use a FQCN ### Changes to Individual Filters +#### `DashToSeparator` + +The following methods have been removed: + +- `setOptions` +- `getOptions` +- `isOptions` +- `setSeparator` +- `getSeparator` + +The constructor now only accepts an associative array of [documented options](../word.md#dashToSeparator). + #### `DenyList` The following methods have been removed: diff --git a/docs/book/v3/word.md b/docs/book/v3/word.md index 7e1f6540..57564a42 100644 --- a/docs/book/v3/word.md +++ b/docs/book/v3/word.md @@ -106,8 +106,7 @@ The following options are supported for `Laminas\Filter\Word\DashToSeparator`: ### Basic Usage ```php -$filter = new Laminas\Filter\Word\DashToSeparator('+'); -// or new Laminas\Filter\Word\CamelCaseToSeparator(array('separator' => '+')); +$filter = new Laminas\Filter\Word\DashToSeparator(['separator' => '+']); print $filter->filter('this-is-my-content'); ``` diff --git a/src/Word/DashToSeparator.php b/src/Word/DashToSeparator.php index fbb023d8..9a0453f4 100644 --- a/src/Word/DashToSeparator.php +++ b/src/Word/DashToSeparator.php @@ -4,7 +4,8 @@ namespace Laminas\Filter\Word; -use Closure; +use Laminas\Filter\FilterInterface; +use Laminas\Filter\ScalarOrArrayFilterCallback; use function str_replace; @@ -14,24 +15,28 @@ * ... * } * @template TOptions of Options - * @extends AbstractSeparator + * @implements FilterInterface> */ -final class DashToSeparator extends AbstractSeparator +final class DashToSeparator implements FilterInterface { - public function filter(mixed $value): mixed + private readonly string $separator; + + /** @param Options $options */ + public function __construct(array $options = []) { - return self::applyFilterOnlyToStringableValuesAndStringableArrayValues( - $value, - Closure::fromCallable([$this, 'filterNormalizedValue']) - ); + $this->separator = $options['separator'] ?? ' '; + } + + public function __invoke(mixed $value): mixed + { + return $this->filter($value); } - /** - * @param string|string[] $value - * @return string|string[] - */ - private function filterNormalizedValue($value) + public function filter(mixed $value): mixed { - return str_replace('-', $this->separator, $value); + return ScalarOrArrayFilterCallback::applyRecursively( + $value, + fn (string $input): string => str_replace('-', $this->separator, $input) + ); } } diff --git a/test/Word/DashToSeparatorTest.php b/test/Word/DashToSeparatorTest.php index ea67f77f..cf806a6c 100644 --- a/test/Word/DashToSeparatorTest.php +++ b/test/Word/DashToSeparatorTest.php @@ -24,7 +24,7 @@ public function testFilterSeparatesDashedWordsWithDefaultSpaces(): void public function testFilterSeparatesDashedWordsWithSomeString(): void { $string = 'dash-separated-words'; - $filter = new DashToSeparatorFilter(':-:'); + $filter = new DashToSeparatorFilter(['separator' => ':-:']); $filtered = $filter($string); self::assertNotEquals($string, $filtered);