Skip to content

Commit

Permalink
Merge pull request #194 from ramchale/AbstractSeparator
Browse files Browse the repository at this point in the history
Remove AbstractSeparator
  • Loading branch information
gsteel authored Nov 8, 2024
2 parents 25d0693 + 813ea51 commit 55ef969
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 158 deletions.
4 changes: 4 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,10 @@ 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.

#### `SeparatorToCamelCase`

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

#### `SeparatorToDash`

The constructor now only accepts an associative array of [documented options](../word.md#separatorToDash).
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 @@ -168,8 +168,7 @@ The following options are supported for `Laminas\Filter\Word\SeparatorToCamelCas
### Basic Usage

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

print $filter->filter('this:is:my:content');
```
Expand Down
36 changes: 3 additions & 33 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<PossiblyInvalidPropertyAssignmentValue>
<code><![CDATA[$this->options]]></code>
</PossiblyInvalidPropertyAssignmentValue>
<PossiblyUnusedMethod>
<code><![CDATA[applyFilterOnlyToStringableValuesAndStringableArrayValues]]></code>
</PossiblyUnusedMethod>
<RedundantConditionGivenDocblockType>
<code><![CDATA[is_object($options)]]></code>
</RedundantConditionGivenDocblockType>
Expand Down Expand Up @@ -577,41 +580,17 @@
<code><![CDATA[is_string($attribute)]]></code>
</RedundantConditionGivenDocblockType>
</file>
<file src="src/Word/AbstractSeparator.php">
<DeprecatedClass>
<code><![CDATA[AbstractFilter]]></code>
</DeprecatedClass>
<DeprecatedMethod>
<code><![CDATA[setSeparator]]></code>
<code><![CDATA[setSeparator]]></code>
</DeprecatedMethod>
<PossiblyInvalidArgument>
<code><![CDATA[$separator]]></code>
</PossiblyInvalidArgument>
<PossiblyInvalidCast>
<code><![CDATA[$separator]]></code>
</PossiblyInvalidCast>
<PossiblyUnusedMethod>
<code><![CDATA[getSeparator]]></code>
</PossiblyUnusedMethod>
</file>
<file src="src/Word/SeparatorToCamelCase.php">
<MissingClosureParamType>
<code><![CDATA[$matches]]></code>
<code><![CDATA[$matches]]></code>
<code><![CDATA[$matches]]></code>
<code><![CDATA[$matches]]></code>
</MissingClosureParamType>
<MixedArgument>
<code><![CDATA[$matches[1]]]></code>
<code><![CDATA[$matches[1]]]></code>
<code><![CDATA[$matches[2]]]></code>
<code><![CDATA[$matches[2]]]></code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$matches[1]]]></code>
<code><![CDATA[$matches[1]]]></code>
<code><![CDATA[$matches[2]]]></code>
<code><![CDATA[$matches[2]]]></code>
</MixedArrayAccess>
</file>
Expand Down Expand Up @@ -963,13 +942,4 @@
<code><![CDATA[returnUnfilteredDataProvider]]></code>
</PossiblyUnusedMethod>
</file>
<file src="test/Word/UnderscoreToCamelCaseTest.php">
<MixedAssignment>
<code><![CDATA[$filtered]]></code>
<code><![CDATA[$filtered]]></code>
<code><![CDATA[$filtered]]></code>
<code><![CDATA[$filtered]]></code>
<code><![CDATA[$filtered]]></code>
</MixedAssignment>
</file>
</files>
55 changes: 0 additions & 55 deletions src/Word/AbstractSeparator.php

This file was deleted.

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

namespace Laminas\Filter\Word;

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

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

public function __invoke(mixed $value): mixed
{
parent::__construct('-');
return $this->filter($value);
}
}
74 changes: 33 additions & 41 deletions src/Word/SeparatorToCamelCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,57 @@

namespace Laminas\Filter\Word;

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

use function mb_strtoupper;
use function preg_quote;
use function preg_replace_callback;
use function strtoupper;

/**
* @psalm-type Options = array{
* separator?: string,
* ...
* }
* @template TOptions of Options
* @extends AbstractSeparator<TOptions>
* @implements FilterInterface<string|array<array-key, string|mixed>>
*/
class SeparatorToCamelCase extends AbstractSeparator
final class SeparatorToCamelCase 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'] ?? ' ';
}

/**
* @param string|string[] $value
* @return string|string[]
*/
private function filterNormalizedValue($value)
public function filter(mixed $value): mixed
{
// a unicode safe way of converting characters to \x00\x00 notation
$pregQuotedSeparator = preg_quote($this->separator, '#');

if (StringUtils::hasPcreUnicodeSupport()) {
$patterns = [
'#(' . $pregQuotedSeparator . ')(\P{Z}{1})#u',
'#(^\P{Z}{1})#u',
];
$replacements = [
static fn($matches): string => mb_strtoupper($matches[2], 'UTF-8'),
static fn($matches): string => mb_strtoupper($matches[1], 'UTF-8'),
];
} else {
$patterns = [
'#(' . $pregQuotedSeparator . ')([\S]{1})#',
'#(^[\S]{1})#',
];
$replacements = [
static fn($matches): string => strtoupper($matches[2]),
static fn($matches): string => strtoupper($matches[1]),
];
}
$patterns = [
'#(' . $pregQuotedSeparator . ')(\P{Z}{1})#u',
'#(^\P{Z}{1})#u',
];
$replacements = [
static fn($matches): string => mb_strtoupper($matches[2], 'UTF-8'),
static fn($matches): string => mb_strtoupper($matches[1], 'UTF-8'),
];

$filtered = $value;
foreach ($patterns as $index => $pattern) {
$filtered = preg_replace_callback($pattern, $replacements[$index], $filtered);
}
return $filtered;
return ScalarOrArrayFilterCallback::applyRecursively(
$value,
function (string $input) use ($patterns, $replacements): string {
$filtered = $input;
foreach ($patterns as $index => $pattern) {
$filtered = preg_replace_callback($pattern, $replacements[$index], $filtered);
}
return $filtered;
}
);
}

public function __invoke(mixed $value): mixed
{
return $this->filter($value);
}
}
22 changes: 11 additions & 11 deletions src/Word/UnderscoreToCamelCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

namespace Laminas\Filter\Word;

/**
* @psalm-type Options = array{
* separator?: string,
* ...
* }
* @template TOptions of Options
* @extends SeparatorToCamelCase<TOptions>
*/
class UnderscoreToCamelCase extends SeparatorToCamelCase
use Laminas\Filter\FilterInterface;

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

public function __invoke(mixed $value): mixed
{
parent::__construct('_');
return $this->filter($value);
}
}
2 changes: 1 addition & 1 deletion src/Word/UnderscoreToStudlyCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function filter(mixed $value): mixed
}

/** @var string|array $value */
$value = (new SeparatorToCamelCase('_'))->filter($value);
$value = (new SeparatorToCamelCase(['separator' => '_']))->filter($value);

return ScalarOrArrayFilterCallback::applyRecursively(
$value,
Expand Down
8 changes: 4 additions & 4 deletions test/Word/SeparatorToCamelCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testFilterSeparatesCamelCasedWordsWithSpacesByDefault(): void
public function testFilterSeparatesCamelCasedWordsWithProvidedSeparator(): void
{
$string = 'camel:-:cased:-:Words';
$filter = new SeparatorToCamelCaseFilter(':-:');
$filter = new SeparatorToCamelCaseFilter(['separator' => ':-:']);
$filtered = $filter($string);

self::assertNotEquals($string, $filtered);
Expand All @@ -36,7 +36,7 @@ public function testFilterSeparatesCamelCasedWordsWithProvidedSeparator(): void
public function testFilterSeparatesUniCodeCamelCasedWordsWithProvidedSeparator(): void
{
$string = 'camel:-:cased:-:Words';
$filter = new SeparatorToCamelCaseFilter(':-:');
$filter = new SeparatorToCamelCaseFilter(['separator' => ':-:']);
$filtered = $filter($string);

self::assertNotEquals($string, $filtered);
Expand All @@ -47,7 +47,7 @@ public function testFilterSeparatesUniCodeCamelCasedWordsWithProvidedSeparator()
public function testFilterSeparatesUniCodeCamelCasedUserWordsWithProvidedSeparator(): void
{
$string = 'test šuma';
$filter = new SeparatorToCamelCaseFilter(' ');
$filter = new SeparatorToCamelCaseFilter(['separator' => ' ']);
$filtered = $filter($string);

self::assertNotEquals($string, $filtered);
Expand All @@ -58,7 +58,7 @@ public function testFilterSeparatesUniCodeCamelCasedUserWordsWithProvidedSeparat
public function testFilterSeparatesCamelCasedNonAlphaWordsWithProvidedSeparator(): void
{
$string = 'user_2_user';
$filter = new SeparatorToCamelCaseFilter('_');
$filter = new SeparatorToCamelCaseFilter(['separator' => '_']);
$filtered = $filter($string);

self::assertNotEquals($string, $filtered);
Expand Down

0 comments on commit 55ef969

Please sign in to comment.