From 52e9f2dcc1d8888aad56111aa7698ada5440b395 Mon Sep 17 00:00:00 2001 From: ramchale Date: Wed, 23 Oct 2024 07:51:47 +0100 Subject: [PATCH 1/3] Remove inheritance chain from DashToSeparator Signed-off-by: ramchale --- src/Word/DashToSeparator.php | 54 ++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/src/Word/DashToSeparator.php b/src/Word/DashToSeparator.php index fbb023d8..bd837140 100644 --- a/src/Word/DashToSeparator.php +++ b/src/Word/DashToSeparator.php @@ -4,8 +4,11 @@ namespace Laminas\Filter\Word; -use Closure; +use Laminas\Filter\FilterInterface; +use function array_map; +use function is_array; +use function is_scalar; use function str_replace; /** @@ -14,15 +17,42 @@ * ... * } * @template TOptions of Options - * @extends AbstractSeparator + * @implements FilterInterface */ -final class DashToSeparator extends AbstractSeparator +final class DashToSeparator implements FilterInterface { + protected string $separator = ' '; + + /** + * @param Options|string $separator Space by default + */ + public function __construct(string|array $separator = ' ') + { + if (is_array($separator) && isset($separator['separator'])) { + $this->setSeparator($separator['separator']); + + return; + } + + $this->setSeparator($separator); + } + + public function __invoke(mixed $value): mixed + { + return $this->filter($value); + } + public function filter(mixed $value): mixed { - return self::applyFilterOnlyToStringableValuesAndStringableArrayValues( - $value, - Closure::fromCallable([$this, 'filterNormalizedValue']) + if (! is_array($value)) { + if (! is_scalar($value)) { + return $value; + } + return $this->filterNormalizedValue((string) $value); + } + + return $this->filterNormalizedValue( + array_map(static fn($item) => is_scalar($item) ? (string) $item : $item, $value) ); } @@ -34,4 +64,16 @@ private function filterNormalizedValue($value) { return str_replace('-', $this->separator, $value); } + + /** @return $this */ + public function setSeparator(string $separator): self + { + $this->separator = $separator; + return $this; + } + + public function getSeparator(): string + { + return $this->separator; + } } From 2c06fa5b28bc6101428688e6ece4d4a9e6a3072b Mon Sep 17 00:00:00 2001 From: ramchale Date: Wed, 23 Oct 2024 10:25:36 +0100 Subject: [PATCH 2/3] Reduce code in DashToSeparator filter and add doc changes - Use ScalarOrArrayFilterCallback::applyRecursively - Remove setSeparator and getSeparator - Add to migration doc - Update usage doc to remove construction without Options array Signed-off-by: ramchale --- docs/book/v3/migration/v2-to-v3.md | 12 +++++++ docs/book/v3/word.md | 3 +- src/Word/DashToSeparator.php | 55 +++++------------------------- test/Word/DashToSeparatorTest.php | 2 +- 4 files changed, 23 insertions(+), 49 deletions(-) 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..38826a7a 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 bd837140..9a0453f4 100644 --- a/src/Word/DashToSeparator.php +++ b/src/Word/DashToSeparator.php @@ -5,10 +5,8 @@ namespace Laminas\Filter\Word; use Laminas\Filter\FilterInterface; +use Laminas\Filter\ScalarOrArrayFilterCallback; -use function array_map; -use function is_array; -use function is_scalar; use function str_replace; /** @@ -17,24 +15,16 @@ * ... * } * @template TOptions of Options - * @implements FilterInterface + * @implements FilterInterface> */ final class DashToSeparator implements FilterInterface { - protected string $separator = ' '; + private readonly string $separator; - /** - * @param Options|string $separator Space by default - */ - public function __construct(string|array $separator = ' ') + /** @param Options $options */ + public function __construct(array $options = []) { - if (is_array($separator) && isset($separator['separator'])) { - $this->setSeparator($separator['separator']); - - return; - } - - $this->setSeparator($separator); + $this->separator = $options['separator'] ?? ' '; } public function __invoke(mixed $value): mixed @@ -44,36 +34,9 @@ public function __invoke(mixed $value): mixed public function filter(mixed $value): mixed { - if (! is_array($value)) { - if (! is_scalar($value)) { - return $value; - } - return $this->filterNormalizedValue((string) $value); - } - - return $this->filterNormalizedValue( - array_map(static fn($item) => is_scalar($item) ? (string) $item : $item, $value) + return ScalarOrArrayFilterCallback::applyRecursively( + $value, + fn (string $input): string => str_replace('-', $this->separator, $input) ); } - - /** - * @param string|string[] $value - * @return string|string[] - */ - private function filterNormalizedValue($value) - { - return str_replace('-', $this->separator, $value); - } - - /** @return $this */ - public function setSeparator(string $separator): self - { - $this->separator = $separator; - return $this; - } - - public function getSeparator(): string - { - return $this->separator; - } } 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); From cac8ec8e64b732efb149212c78b3d6b984c3fce3 Mon Sep 17 00:00:00 2001 From: Richard McHale Date: Wed, 23 Oct 2024 11:31:47 +0100 Subject: [PATCH 3/3] Update docs/book/v3/word.md Co-authored-by: George Steel Signed-off-by: Richard McHale --- docs/book/v3/word.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/book/v3/word.md b/docs/book/v3/word.md index 38826a7a..57564a42 100644 --- a/docs/book/v3/word.md +++ b/docs/book/v3/word.md @@ -106,7 +106,7 @@ The following options are supported for `Laminas\Filter\Word\DashToSeparator`: ### Basic Usage ```php -$filter = new Laminas\Filter\Word\DashToSeparator('separator' => '+'); +$filter = new Laminas\Filter\Word\DashToSeparator(['separator' => '+']); print $filter->filter('this-is-my-content'); ```