Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DashToSeparator Filter #184

Merged
merged 3 commits into from
Oct 23, 2024
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
12 changes: 12 additions & 0 deletions docs/book/v3/migration/v2-to-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions docs/book/v3/word.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
```
Expand Down
33 changes: 19 additions & 14 deletions src/Word/DashToSeparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace Laminas\Filter\Word;

use Closure;
use Laminas\Filter\FilterInterface;
use Laminas\Filter\ScalarOrArrayFilterCallback;

use function str_replace;

Expand All @@ -14,24 +15,28 @@
* ...
* }
* @template TOptions of Options
* @extends AbstractSeparator<TOptions>
* @implements FilterInterface<string|array<array-key, string|mixed>>
*/
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)
);
}
}
2 changes: 1 addition & 1 deletion test/Word/DashToSeparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down