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 1 commit
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' => '+');
ramchale marked this conversation as resolved.
Show resolved Hide resolved

print $filter->filter('this-is-my-content');
```
Expand Down
55 changes: 9 additions & 46 deletions src/Word/DashToSeparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -17,24 +15,16 @@
* ...
* }
* @template TOptions of Options
* @implements FilterInterface<mixed>
* @implements FilterInterface<string|array<array-key, string|mixed>>
*/
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
Expand All @@ -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;
}
}
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