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 SeparatorToSeparator Filter #193

Merged
merged 1 commit into from
Nov 6, 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
19 changes: 19 additions & 0 deletions docs/book/v3/migration/v2-to-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ Additionally, `$options['pattern']` _must_ be provided at construction time or a

Exceptions for invalid or empty patterns are now thrown during construct rather than when the filter is invoked.

#### `SeparatorToDash`

The constructor now only accepts an associative array of [documented options](../word.md#separatorToDash).
gsteel marked this conversation as resolved.
Show resolved Hide resolved

#### `SeparatorToSeparator`

The following methods have been removed:

- `setSearchSeparator`
- `getSearchSeparator`
- `setReplacementSeparator`
- `getReplacementSeparator`

The constructor now only accepts an associative array of [documented options](../word.md#separatorToSeparator).

#### `StringPrefix`

The following methods have been removed:
Expand Down Expand Up @@ -177,6 +192,10 @@ The following methods have been removed:

The constructor now only accepts an associative array of [documented options](../standard-filters.md#tonull).

#### `UnderscoreToSeparator`

The constructor now only accepts an associative array of [documented options](../standard-filters.md#underscoreToSeparator).

## Removed Filters

The following filters were deprecated in the 2.0.x series of releases and have now been removed:
Expand Down
8 changes: 3 additions & 5 deletions docs/book/v3/word.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ The following options are supported for `Laminas\Filter\Word\SeparatorToDash`:
### Basic Usage

```php
$filter = new Laminas\Filter\Word\SeparatorToDash(':');
// or new Laminas\Filter\Word\SeparatorToDash(array('separator' => ':'));
$filter = new Laminas\Filter\Word\SeparatorToDash(['separator' => ':']);

print $filter->filter('this:is:my:content');
```
Expand Down Expand Up @@ -236,7 +235,7 @@ The following options are supported for `Laminas\Filter\Word\SeparatorToSeparato
### Basic Usage

```php
$filter = new Laminas\Filter\Word\SeparatorToSeparator(':', '+');
$filter = new Laminas\Filter\Word\SeparatorToSeparator(['search_separator' => ':', 'replacement_separator' => '+']);

print $filter->filter('this:is:my:content');
```
Expand Down Expand Up @@ -287,8 +286,7 @@ The following options are supported for `Laminas\Filter\Word\UnderscoreToSeparat
### Basic Usage

```php
$filter = new Laminas\Filter\Word\UnderscoreToSeparator('+');
// or new Laminas\Filter\Word\CamelCaseToSeparator(array('separator' => '+'));
$filter = new Laminas\Filter\Word\UnderscoreToSeparator(['separator' => '+']));

print $filter->filter('this_is_my_content');
```
Expand Down
20 changes: 0 additions & 20 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -615,26 +615,6 @@
<code><![CDATA[$matches[2]]]></code>
</MixedArrayAccess>
</file>
<file src="src/Word/SeparatorToSeparator.php">
<DeprecatedClass>
<code><![CDATA[AbstractFilter]]></code>
</DeprecatedClass>
<DocblockTypeContradiction>
<code><![CDATA[$this->searchSeparator === null]]></code>
</DocblockTypeContradiction>
<MixedReturnStatement>
<code><![CDATA[self::applyFilterOnlyToStringableValuesAndStringableArrayValues(
$value,
Closure::fromCallable([$this, 'filterNormalizedValue'])
)]]></code>
</MixedReturnStatement>
</file>
<file src="src/Word/Service/SeparatorToSeparatorFactory.php">
<UnusedParam>
<code><![CDATA[$container]]></code>
<code><![CDATA[$requestedName]]></code>
</UnusedParam>
</file>
<file src="test/AllowListTest.php">
<PossiblyUnusedMethod>
<code><![CDATA[defaultTestProvider]]></code>
Expand Down
2 changes: 1 addition & 1 deletion src/FilterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ final class FilterPluginManager extends AbstractPluginManager
Word\DashToUnderscore::class => InvokableFactory::class,
Word\SeparatorToCamelCase::class => InvokableFactory::class,
Word\SeparatorToDash::class => InvokableFactory::class,
Word\SeparatorToSeparator::class => Word\Service\SeparatorToSeparatorFactory::class,
Word\SeparatorToSeparator::class => InvokableFactory::class,
Word\UnderscoreToCamelCase::class => InvokableFactory::class,
Word\UnderscoreToStudlyCase::class => InvokableFactory::class,
Word\UnderscoreToDash::class => InvokableFactory::class,
Expand Down
23 changes: 11 additions & 12 deletions src/Word/DashToUnderscore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@

namespace Laminas\Filter\Word;

/**
* @psalm-type Options = array{
* search_separator?: string,
* replacement_separator?: string,
* ...
* }
* @template TOptions of Options
* @template-extends SeparatorToSeparator<TOptions>
*/
final class DashToUnderscore extends SeparatorToSeparator
use Laminas\Filter\FilterInterface;

/** @implements FilterInterface<string|array<array-key, string|mixed>> */
final class DashToUnderscore implements FilterInterface
{
public function __construct()
public function filter(mixed $value): mixed
{
return (new SeparatorToSeparator(['search_separator' => '-', 'replacement_separator' => '_']))->filter($value);
}

public function __invoke(mixed $value): mixed
{
parent::__construct('-', '_');
return $this->filter($value);
}
}
32 changes: 22 additions & 10 deletions src/Word/SeparatorToDash.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@

namespace Laminas\Filter\Word;

use Laminas\Filter\FilterInterface;

/**
* @psalm-type Options = array{
* search_separator?: string,
* replacement_separator?: string,
* ...
* separator?: string,
* }
* @template TOptions of Options
* @template-extends SeparatorToSeparator<TOptions>
* @implements FilterInterface<string|array<array-key, string|mixed>>
*/
final class SeparatorToDash extends SeparatorToSeparator
final class SeparatorToDash implements FilterInterface
{
/**
* @param string $searchSeparator Separator to search for change
*/
public function __construct($searchSeparator = ' ')
private readonly string $separator;

/** @param Options $options */
public function __construct(array $options = [])
{
$this->separator = $options['separator'] ?? ' ';
}

public function filter(mixed $value): mixed
{
return (new SeparatorToSeparator(
['search_separator' => $this->separator, 'replacement_separator' => '-']
))->filter($value);
}

public function __invoke(mixed $value): mixed
{
parent::__construct($searchSeparator, '-');
return $this->filter($value);
}
}
102 changes: 18 additions & 84 deletions src/Word/SeparatorToSeparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

namespace Laminas\Filter\Word;

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

use function preg_quote;
use function preg_replace;
Expand All @@ -15,101 +14,36 @@
* @psalm-type Options = array{
* search_separator?: string,
* replacement_separator?: string,
* ...
* }
* @template TOptions of Options
* @template-extends AbstractFilter<TOptions>
* @implements FilterInterface<string|array<array-key, string|mixed>>
*/
class SeparatorToSeparator extends AbstractFilter
final class SeparatorToSeparator implements FilterInterface
{
/** @var string */
protected $searchSeparator;
/** @var string */
protected $replacementSeparator;
private readonly string $searchSeparator;
private readonly string $replacementSeparator;

/**
* @param string $searchSeparator Separator to search for
* @param string $replacementSeparator Separator to replace with
*/
public function __construct($searchSeparator = ' ', $replacementSeparator = '-')
/** @param Options $options */
public function __construct(array $options = [])
{
$this->setSearchSeparator($searchSeparator);
$this->setReplacementSeparator($replacementSeparator);
$this->searchSeparator = $options['search_separator'] ?? ' ';
$this->replacementSeparator = $options['replacement_separator'] ?? '-';
}

/**
* Sets a new separator to search for
*
* @param string $separator Separator to search for
* @return self
*/
public function setSearchSeparator($separator)
{
$this->searchSeparator = $separator;
return $this;
}

/**
* Returns the actual set separator to search for
*
* @return string
*/
public function getSearchSeparator()
{
return $this->searchSeparator;
}

/**
* Sets a new separator which replaces the searched one
*
* @param string $separator Separator which replaces the searched one
* @return self
*/
public function setReplacementSeparator($separator)
{
$this->replacementSeparator = $separator;
return $this;
}

/**
* Returns the actual set separator which replaces the searched one
*
* @return string
*/
public function getReplacementSeparator()
{
return $this->replacementSeparator;
}

/**
* Defined by Laminas\Filter\Filter
*
* Returns the string $value, replacing the searched separators with the defined ones
*
* @psalm-return ($value is string ? string : mixed)
*/
public function filter(mixed $value): mixed
{
return self::applyFilterOnlyToStringableValuesAndStringableArrayValues(
return ScalarOrArrayFilterCallback::applyRecursively(
$value,
Closure::fromCallable([$this, 'filterNormalizedValue'])
fn (string $input): string => preg_replace(
'#' . preg_quote($this->searchSeparator, '#') . '#',
$this->replacementSeparator,
$input
)
);
}

/**
* @param string $value
* @return string
*/
private function filterNormalizedValue($value)
public function __invoke(mixed $value): mixed
{
if ($this->searchSeparator === null) {
throw new Exception\RuntimeException('You must provide a search separator for this filter to work.');
}

return preg_replace(
'#' . preg_quote($this->searchSeparator, '#') . '#',
$this->replacementSeparator,
$value
);
return $this->filter($value);
}
}
33 changes: 0 additions & 33 deletions src/Word/Service/SeparatorToSeparatorFactory.php

This file was deleted.

23 changes: 11 additions & 12 deletions src/Word/UnderscoreToDash.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@

namespace Laminas\Filter\Word;

/**
* @psalm-type Options = array{
* search_separator?: string,
* replacement_separator?: string,
* ...
* }
* @template TOptions of Options
* @template-extends SeparatorToSeparator<TOptions>
*/
final class UnderscoreToDash extends SeparatorToSeparator
use Laminas\Filter\FilterInterface;

/** @implements FilterInterface<string|array<array-key, string|mixed>> */
final class UnderscoreToDash implements FilterInterface
{
public function __construct()
public function filter(mixed $value): mixed
{
return (new UnderscoreToSeparator(['separator' => '-']))->filter($value);
}

public function __invoke(mixed $value): mixed
{
parent::__construct('_', '-');
return $this->filter($value);
}
}
Loading