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 UnderscoreToStudlyCase Filter #192

Merged
merged 1 commit into from
Nov 4, 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/word.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,22 @@ print $filter->filter('this_is_my_content');
```

The above example returns `this-is-my-content`.

## UnderscoreToStudlyCase

This filter modifies a given string such that `words_with_underscores` are
converted to `wordsWithUnderscores`.

### Supported Options

There are no additional options for `Laminas\Filter\Word\UnderscoreToStudlyCase`:

### Basic Usage

```php
$filter = new Laminas\Filter\Word\UnderscoreToStudlyCase();

print $filter->filter('this_is_my_content');
```

The above example returns `thisIsMyContent`.
gsteel marked this conversation as resolved.
Show resolved Hide resolved
26 changes: 0 additions & 26 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -635,22 +635,6 @@
<code><![CDATA[$requestedName]]></code>
</UnusedParam>
</file>
<file src="src/Word/UnderscoreToStudlyCase.php">
<MissingClosureParamType>
<code><![CDATA[$value]]></code>
</MissingClosureParamType>
<MissingClosureReturnType>
<code><![CDATA[static function ($value) {]]></code>
</MissingClosureReturnType>
<MixedArgument>
<code><![CDATA[$value]]></code>
<code><![CDATA[$value]]></code>
<code><![CDATA[$value]]></code>
</MixedArgument>
<MixedReturnStatement>
<code><![CDATA[is_array($value) ? array_map($lowerCaseFirst, $value) : $lowerCaseFirst($value)]]></code>
</MixedReturnStatement>
</file>
<file src="test/AllowListTest.php">
<PossiblyUnusedMethod>
<code><![CDATA[defaultTestProvider]]></code>
Expand Down Expand Up @@ -1008,14 +992,4 @@
<code><![CDATA[$filtered]]></code>
</MixedAssignment>
</file>
<file src="test/Word/UnderscoreToStudlyCaseTest.php">
<MixedAssignment>
<code><![CDATA[$filtered]]></code>
<code><![CDATA[$filtered]]></code>
<code><![CDATA[$filtered]]></code>
<code><![CDATA[$filtered]]></code>
<code><![CDATA[$filtered]]></code>
<code><![CDATA[$filtered]]></code>
</MixedAssignment>
</file>
</files>
43 changes: 17 additions & 26 deletions src/Word/UnderscoreToStudlyCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,41 @@

namespace Laminas\Filter\Word;

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

use function array_map;
use function is_array;
use function is_scalar;
use function mb_strlen;
use function mb_strtolower;
use function mb_substr;

/**
* @psalm-type Options = array{
* separator?: string,
* ...
* }
* @template TOptions of Options
* @template-extends UnderscoreToCamelCase<TOptions>
*/
final class UnderscoreToStudlyCase extends UnderscoreToCamelCase
/** @implements FilterInterface<string|array<array-key, string|mixed>> */
final class UnderscoreToStudlyCase implements FilterInterface
{
/**
* Defined by Laminas\Filter\Filter
*
* @psalm-return ($value is scalar ? string : $value is array ? array : mixed)
*/
public function filter(mixed $value): mixed
{
if (! is_scalar($value) && ! is_array($value)) {
return $value;
}

/** @var string|array $value */
$value = parent::filter($value);
$lowerCaseFirst = 'lcfirst';
$value = (new SeparatorToCamelCase('_'))->filter($value);

if (StringUtils::hasPcreUnicodeSupport()) {
$lowerCaseFirst = static function ($value) {
if (0 === mb_strlen($value)) {
return $value;
return ScalarOrArrayFilterCallback::applyRecursively(
$value,
function (string $input): string {
if (0 === mb_strlen($input)) {
return $input;
}

return mb_strtolower(mb_substr($value, 0, 1)) . mb_substr($value, 1);
};
}
return mb_strtolower(mb_substr($input, 0, 1)) . mb_substr($input, 1);
}
);
}

return is_array($value) ? array_map($lowerCaseFirst, $value) : $lowerCaseFirst($value);
public function __invoke(mixed $value): mixed
{
return $this->filter($value);
}
}