Skip to content

Commit

Permalink
Refactor UnderscoreToStudlyCase Filter
Browse files Browse the repository at this point in the history
Signed-off-by: ramchale <[email protected]>
  • Loading branch information
ramchale committed Nov 4, 2024
1 parent b57f096 commit 1255d1c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 52 deletions.
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`.
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);
}
}

0 comments on commit 1255d1c

Please sign in to comment.