Skip to content

Commit

Permalink
Merge pull request #184 from ramchale/DashToSeparator
Browse files Browse the repository at this point in the history
Refactor `DashToSeparator` Filter
  • Loading branch information
gsteel authored Oct 23, 2024
2 parents beb5aee + cac8ec8 commit dbad5e6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
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

0 comments on commit dbad5e6

Please sign in to comment.